Đây là một cách tiếp cận dựa trên suối. Điều này sẽ không đòi hỏi quá nhiều bộ nhớ trong khi tính toán n-gram.
object ngramstream extends App {
def process(st: Stream[Array[String]])(f: Array[String] => Unit): Stream[Array[String]] = st match {
case x #:: xs => {
f(x)
process(xs)(f)
}
case _ => Stream[Array[String]]()
}
def ngrams(n: Int, words: Array[String]) = {
// exclude 1-grams
(2 to n).map { i => words.sliding(i).toStream }
.foldLeft(Stream[Array[String]]()) {
(a, b) => a #::: b
}
}
val words = "the bee is the bee of the bees"
val n = 4
val ngrams2 = ngrams(n, words.split(" "))
process(ngrams2) { x =>
println(x.toList)
}
}
OUTPUT:
List(the, bee)
List(bee, is)
List(is, the)
List(the, bee)
List(bee, of)
List(of, the)
List(the, bees)
List(the, bee, is)
List(bee, is, the)
List(is, the, bee)
List(the, bee, of)
List(bee, of, the)
List(of, the, bees)
List(the, bee, is, the)
List(bee, is, the, bee)
List(is, the, bee, of)
List(the, bee, of, the)
List(bee, of, the, bees)
Tôi không biết những gì một n-gram là. Bạn có chọn từ ngẫu nhiên không? Hoặc có một số logic? – santiagobasulto
@santiagobasulto Wikipedia là bạn của bạn: http://en.wikipedia.org/wiki/N-gram –
Đây có phải là do bất kỳ cơ hội nào liên quan đến http://stackoverflow.com/questions/8256830/how-to-make-string hậu quả-trong-scala? –