N-Gram Creator 未检索到最终词
N-Gram Creator Not Retrieving Final Word
我正在做一个项目,我需要创建 'n-grams' 进行分析。我目前有一种方法可以为 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);
}
感谢大家的帮助,非常感谢。
由于您的 bagOfWords 包含 n 个元素,您应该迭代整个列表。
下面的代码应该可以解决问题。
if (int i = 0 <= bagOfWords.size() - n) {
// The rest ngrams implementation is correct
}
我正在做一个项目,我需要创建 'n-grams' 进行分析。我目前有一种方法可以为 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);
}
感谢大家的帮助,非常感谢。
由于您的 bagOfWords 包含 n 个元素,您应该迭代整个列表。 下面的代码应该可以解决问题。
if (int i = 0 <= bagOfWords.size() - n) {
// The rest ngrams implementation is correct
}