使用Map<String,Integer>从最高到最低的java排序
我对如何从任何文本文件输入中对单词的频率进行排序有点困惑。我能够对每个代码的频率进行编码,但我不确定如何从最常出现的单词中进行分类
这就是我的频率
例如。我有一个文件文本可以读取
This is a test run.
I come with no wrapping or pretty pink bows.
I am who I am from my head to my toes.
I tend to get loud when speaking my mind.
Even a little crazy some of the time.
I. am. who. I. am.
此代码的输出为
2 a
2 am
2 am.
1 bows.
1 come
1 crazy
1 even
1 from
1 get
1 head
4 i
2 i.
1 is
1 little
1 loud
1 mind.
3 my
1 no
1 of
1 or
1 pink
1 pretty
1 run.
1 some
1 speaking
1 tend
1 test
1 the
1 this
1 time.
2 to
1 toes.
1 when
1 who
1 who.
1 with
1 wrapping
我还想知道我怎么能忽略句号,因为一些相同的单词因为句号而被高估了
Scanner input;
try {
input = new Scanner(file);
Map<String, Integer> wordCounts = new TreeMap<String, Integer>();
while (input.hasNext()) {
String next = input.next().toLowerCase();
if (!wordCounts.containsKey(next)) {
wordCounts.put(next, 1);
} else {
wordCounts.put(next, wordCounts.get(next) + 1);
}
}
// report frequencies
for (String word : wordCounts.keySet()) {
int count = wordCounts.get(word);
System.out.println(count + "\t" + word);
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
共 (0) 个答案