Search code examples
javan-gramcollocation

N-Gram with ArrayList


I am undergoing a project where I am analysing 'ngrams'. I have a method in my program that creates bigrams and trigrams. However, they only get the consecutive adjacent words together where I want it to get all combinations of words...

For example,

 Original String - "chilli, start, day, suffer, raynaud, check, raynaudsuk, great, tip, loveyourglov, ram"
 Bigram - "chilli start, start day, day suffer, suffer raynaud, raynaud check, check raynaudsuk, raynaudsuk great, great tip, tip loveyourglov, loveyourglov ram"

But I want it to get a combination of ALL the words in the String. For example

Expected Bigram - "chilli start,1, chilli day,2, chilli suffer,3, chilli raynaud,4, chilli check,5, chilli raynaudsuk,6, chilli great,7, chilli tip,8, chilli loveyourglov,9, chilli ram,10, start day,1, etc..."

How can I amend my method to produce a bigram like this?

public ArrayList<String> bigramList;
ArrayList<String> fullBagOfWords = new ArrayList<String>();


public void bigramCreator(){
    int i = 0;
    bigramList = new ArrayList<String>();
    for(String bi : fullBagOfWords){
        int n = 2;
        if (i <= fullBagOfWords.size() - n) {
            String bigram = "";
            for (int j = 0; j < n-1; j++)
            bigram += fullBagOfWords.get(i + j) + " ";
            bigram += fullBagOfWords.get(i + n - 1);
            bigramList.add(bigram);
            i++;
        }
    }
}

Thanks very much for any help given.


Solution

  • If I understand the task correctly, it should be very simple

    for (int i = 0; i < fullBagOfWords.size() - 1; i++) {
        for (int j = i + 1; j < fullBagOfWords.size(); j++) {
            bigramList.add(fullBagOfWords.get(i) + " " + fullBagOfWords.get(j) + ", " + (j - i));
        }
    }