我正在尝试制作一个脚本,它将采用一组单词(自定义类),按字母顺序将它们按文本值组织成一个数组(这部分可以工作)。从这里开始,我将计算它前面有多少项是相同的,这将是所有这些类似术语的频率。然后它继续这样做,直到为阵列中的每个元素分配了一个频率。从这里开始,它将元素重新排序回原始位置,前提是保存原始元素顺序的预存变量。这是代码:
public void setFrequencies() {
List<Word> dupeWordList;
dupeWordList = new ArrayList<>(wordList);
dupeWordList.removeAll(Collections.singleton(null));
Collections.sort(dupeWordList, (Word one, Word other) -> one.getValue().compareTo(other.getValue()));
int count;
int currElement;
for(currElement = 0; currElement < dupeWordList.size(); currElement++) {
count = 1;
Word tempWord = dupeWordList.get(currElement);
tempWord.setFrequency(count);
if(currElement+1 <= dupeWordList.size() - 1) {
Word nextWord = dupeWordList.get(currElement+1);
while(tempWord.getValue().equals(nextWord.getValue())) {
count++;
currElement++;
tempWord.setFrequency(count);
for(int e = 0; e < count - 1; e++) {
Word middleWord = new Word();
if(currElement-count+2+e < dupeWordList.size() - 1) {
middleWord = dupeWordList.get(currElement-count+2+e);
}
middleWord.setFrequency(count);
}
if(currElement+1 <= dupeWordList.size() - 1) {
nextWord = dupeWordList.get(currElement+1);
} else {
break;
}
}
break;
}
}
List<Word> reSortedList = new ArrayList<>(wordList);
Word fillWord = new Word();
fillWord.setFrequency(0);
fillWord.setValue(null);
Collections.fill(reSortedList, fillWord);
for(int i = 0; i < dupeWordList.size(); i++) {
Word word = dupeWordList.get(i);
int wordOrder = word.getOrigOrder();
reSortedList.set(wordOrder, word);
}
System.out.println(Arrays.toString(DebugFreq(reSortedList)));
setWordList(reSortedList);
}
public int[] DebugFreq(List<Word> rSL) {
int[] results = new int[rSL.size()];
for(int i=0; i < results.length; i++) {
results[i] = rSL.get(i).getFrequency();
}
return results;
}
如您所见,我在底部设置了一个小调试方法。当我运行此方法时,显示每个单词的频率为1.我无法在代码中看到问题,也不会出现任何错误。请记住,我已经让它显示了排序的dupeWordList,并且它正确地按字母顺序排列,并且它们是连续的重复元素,所以这不应该发生。
答案 0 :(得分:2)
所以如果我理解正确的话......下面的代码就是你的解决方案。
好的,你有一个列表,其中包含按字母顺序排序的字符串(术语或单词)。
// Okay the below list is already sorted in alphabetical order.
List<String> dupeWordList = new ArrayList<>(wordList);
要计算列表中单词的频率,Map<String, Integer>
可能会对您有所帮助,如下所示。
//Take a Map with Integer as value and String as key.
Map<String,Integer> result = new HashMap<String,Integer> ();
//Iterate your List
for(String s : dupeWordList)
{
if(map.containskey(s))
{
map.put(s,map.get(s)+1);
// Please consider casting here.
}else
{
map.put(s,1);
}
}
现在我们有一个map
,其中您的字词或字词的频率就是地图中的值。
希望它有所帮助。