def ngrams(n: Int, words: Array[String]) = {
// exclude 1-grams
(1 to n).map { i => words.sliding(i).toStream }
.foldLeft(Stream[Array[String]]()) {
(a, b) => a #::: b
} }
scala> val op2 = ngrams(3, "how are you".split(" ")).foreach { x => println((x.mkString(" ")))}
Output as :
how
are
you
how are
are you
how are you
op2: Unit = ()
How to avoid the above Unit value , actually i wants to convert them to Set, because of the Unit=(), it's failing . So can you please help in output should be Set(how,are,you,how are,are you,how are you) Thanks for the post -- How to generate n-grams in scala?.