I have a Map[String,String] wherein the last key,value pair is "Text"->The text of the documents. I wish to calculate the count of each word in the document and I was thinking of having another map which has a count of words in each document. I have a map like Map("id"->12,"text"->"The dog likes the cat") and I am trying to get another map which is Map("The"->2,"dog"->1,"likes"->1,"cat"->1) I have the following code:
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
}
I don't know how to reset the counts map because I want separate counts of words for each document.