Пусть эти мои документы:Как извлечь биграммы и триграммы в Scala?
very pleased product . phone lightweight comfortable sound quality good house yard .
quality construction phone base unit good . ample supply cable adapter . plug computer soundcard .
shop unit mail rebate . unit battery pack hold play time strap carr headphone adapter cable perfect digital copy optical. component micro plug stereo connector cable micro plug rca cable .
unit primarily record guitar jam session . input plug provide power plug microphone . decent stereo mic need digital recording performance . mono mode double recording time .
admit like new electronic toy . digital camera not impress .
, и я хочу, чтобы извлечь все биграмм и триграмм с их числом вхождений от каждого предложения, в каждом документе.
и я попытался:
case class trigram(first: String, second: String,third: String) {
def mkReplacement(s: String) = s.replaceAll(first + " " + second + " " + third, first + "-" + second + "-" + third)
}
def stringToTrigrams(s: String) = {
val words = s.split(".")
if (words.size >= 3) {
words.sliding(3).map(a => tigram(a(0),a(1),a(2)))
}
else
Iterator[tigram]()
}
val conf = new SparkConf()
val sc = new SparkContext(conf)
val data = sc.textFile("docs")
val trigrams = data.flatMap {
stringToTrigrams
}.collect()
val trigramCounts = trigrams.groupBy(identity).mapValues(_.size)
но не показывает триграмма?
Хотя приятно видеть, что мой код повторно используется, подтверждение будет приятным (http://stackoverflow.com/a/30681833/21755) –