[英]Scoring sentences in a text file using word repitition
I am making a Java program which scores sentences and display sentences with higher scores. 我正在制作一个Java程序,该程序对句子进行评分并显示得分更高的句子。 What I am trying to do is read the text file, split information in the text file into separate sentences, calculate the number of words repeated inside the text file, give a score to the sentences that consist of repeated words, then finally display the sentences with scores more than 1. 我要做的是读取文本文件,将文本文件中的信息拆分为单独的句子,计算文本文件中重复的单词数,对包含重复单词的句子评分,然后最终显示句子分数超过1。
So far I have managed to split each sentence and now I am trying to give a score to each repeated word. 到目前为止,我已经设法拆分了每个句子,现在我试图为每个重复的单词打分。 Following code shows my progress. 以下代码显示了我的进度。
However, the for loop in the above code doesn't seem to work. 但是,以上代码中的for循环似乎不起作用。
What am I doing wrong here? 我在这里做错了什么?
You can use a map for your problem actualy, mapping the words to count as this Then for your scoring function 您可以使用地图来解决您的问题,将单词映射为以此为计分函数
private static void score(String sentence){
Map<String,Integer> score = new HashMap<String,Integer>();
for(String key : sentence.split(" ")){
if(score.get(key) == null) score.put(key,0);
score.put(key,score.get(key)+1);
}
//Printing scores for your keys, you can add the case where your key is= to 'a , the ,and ...'
for (String key : score .keySet()) {
System.out.println("Key = " + key+" Score : "+score.get(key));
}
}
Inside your for loop you can do : 在您的for循环中,您可以执行以下操作:
for (int i = 0; i < sentenceList.size(); i++) {
score(sentenceList.get(i));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.