0

我正在进行一个项目,我正在分析“ngrams”。我的程序中有一个方法可以创建二元组和三元组。但是,它们只会将连续的相邻单词放在一起,我希望它可以得到所有单词的组合......

例如,

 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"

但我希望它得到字符串中所有单词的组合。例如

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..."

我怎样才能修改我的方法来产生这样的二元组?

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

非常感谢您提供的任何帮助。

4

1 回答 1

0

如果我正确理解任务,它应该很简单

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));
    }
}
于 2016-02-24T19:45:34.340 回答