
Untitled
By: a guest on
May 5th, 2012 | syntax:
None | size: 0.54 KB | hits: 10 | expires: Never
def toNGrams1(lst: List[String], n: Int): List[List[String]] = {
List.range(0, lst.length - n).map(i => lst.slice(i, i+n))
}
def toNGrams2(lst: List[String], n: Int): List[List[String]] = {
@tailrec
def toNGramAcc(acc: List[List[String]], lst: List[String]): List[List[String]] = {
if (lst.length <= n) acc
else toNGramAcc(lst.take(n) :: acc, lst.drop(1))
}
toNGramAcc(Nil, lst)
}
def toNGrams3(lst: List[String], n: Int) = {
for (i <- List.range(0, lst.length - n) ) yield lst.slice(i, i+5)
}