如何在java中查找文档中短语(多个令牌字符串)的频率



我想查找文档中多个令牌字符串或短语的频率。它不是我要找的单词/单术语频率,它总是多术语,术语的数量是动态的…

示例:在文档中搜索"words with friends"的频率!

任何帮助/指示将不胜感激。

谢谢Debjani

您可以使用Buffered Reader逐行读取文档,然后使用split函数获取word/token

的频率。
int count=0;
while ((strLine = br.readLine()) != null)   {
     count+ = (strLine.split("words with friends").length-1);     
}
return count;

编辑:如果想执行不区分大小写的搜索,那么可以使用

Pattern myPattern = Pattern.compile("words with friends", Pattern.CASE_INSENSITIVE);
int count=0;
while ((strLine = br.readLine()) != null)   {
     count+ = (myPattern.split(strLine).length-1);    
}
return count;

为什么不使用正则表达式?Regex是为这类任务优化的。

http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html

最新更新