1

最後のキーと値のペアが「テキスト」->ドキュメントのテキストである Map[String,String] があります。ドキュメント内の各単語の数を計算したいのですが、各ドキュメントの単語数を含む別のマップを作成することを考えていました。Map("id"->12,"text"->"The dog likes the cat") のようなマップがあり、Map("The"->2,"dog"- >1,"likes"->1,"cat"->1) 次のコードがあります:

val Counts = mutable.Map[String, Int]().withDefault(x=>0)
var tfCounts:Map[String,Int]()
for(i<-1 to newsMap.size){
    val tfMap = newsMap.get("newsText").slice(i-1,i).map(x => x.split("\\s+")).toList
    for(token<-tfMap)
        counts(token) +=1 
    tfCounts = tfCounts++ counts
}

ドキュメントごとに個別の単語数が必要なため、カウント マップをリセットする方法がわかりません。

4

1 に答える 1

3
scala> val document = Map("id"->12,"text"->"The dog likes the cat")
document: scala.collection.immutable.Map[String,Any] = Map(id -> 12, text -> The dog likes the cat)

scala> document("text").asInstanceOf[String].split(" ").groupBy(_.toLowerCase).mapValues(_.size)
res3: scala.collection.immutable.Map[String,Int] = Map(cat -> 1, dog -> 1, likes -> 1, the -> 2)
于 2013-03-24T08:10:45.860 に答える