我正在做一个我需要创建&n-gram'用于分析。我目前有一种方法可以为int
' n'并将所有术语组合在一起,除了ArrayList中的最后一个单词,它完全忽略了,我不确定为什么?这是输出......
Original: Making pancakes today? Need a recipe? Check https://t.co/lsrRy8CW22 #PancakeDay https://t.co/WiPX4joM4v
Bag of Words: [make, pancak, today, recip, check, pancakeday]
2-gram: [make pancak, pancak today, today recip, recip check]
3- gram: [make pancak today, pancak today recip, today recip check]
正如您所看到的,它跳过了最后一个单词pancakeday
,我不确定为什么。
这是方法......
public void ngramCreator(int n){
ngramList = new ArrayList<String>();
for(String word : bagOfWords){
if (int i = 0 < bagOfWords.size() - n) {
String ngram = "";
for (int j = 0; j < n-1; j++)
ngram += bagOfWords.get(i + j) + " ";
ngram += bagOfWords.get(i + n - 1);
ngramList.add(ngram);
i++;
}
}
System.out.println(ngramList);
}
感谢所有帮助人员,非常感谢。
答案 0 :(得分:0)
由于你的bagOfWords包含n个元素,你应该迭代整个列表。 以下代码应该可以解决问题。
if (int i = 0 <= bagOfWords.size() - n) {
// The rest ngrams implementation is correct
}