Apply a Frequency to an Element in an Array

103 Views Asked by At

I am trying to make a script that will take a set of Words (custom class), organize them alphabetically into an array by their text value (this part works). From here I was going to count how many terms ahead of it are the same as it, and that will be the frequency for all those similar terms. Then it continues to do this till each element in the array has been assigned a frequency. From here it re sorts the elements back into their original position provided a pre stored variable that holds their original element order. Here is the code:

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;
}

As you can see I set up a little debug method at the bottom. When I run this method is shows that every word was given a frequency of 1. I cant see the issue in my code, nor does it get any errors. Keep in mind I have had it display the sorted dupeWordList and it does correctly alphabetize and their are consecutive duplicate elements in it so this should not be happening.

1

There are 1 best solutions below

0
On BEST ANSWER

So If I understand you correctly.. below code would be your solution.

Okay You have a list which is having a strings (terms or words) which are sorted in alphabetical Order.

  // Okay the below list is already sorted in alphabetical order.
  List<String>  dupeWordList  = new ArrayList<>(wordList);

To count the Frequency of words in your list, Map<String, Integer> might help you as below.

   //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);
           }


       }

Okay now we have a map which is having the frequency of your words or terms as value in your map.

Hope it helps.