0
假設這些都是我的文檔:如何提取Scala中的bigrams和trigrams?
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) –