当前位置: 动力学知识库 > 问答 > 编程问答 >

java - N-Gram Creator Not Retrieving Final Word

问题描述:

I am doing a project where I need to create 'n-grams' for analysis. I currently have a method that does this for int 'n' and collates all the terms together, except for the last word in the ArrayList, which it totally ignores and I am unsure why? Here is the output...

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]

As you can see, it skips the last word pancakeday and I am unsure why.

Here is the method...

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

}

Thanks for all the help guys, totally appreciate it.

网友答案:

Since your bagOfWords contains n elements, you should iterate the whole list. The following code should do the trick.

if (int i = 0 <= bagOfWords.size() - n) {
    // The rest ngrams implementation is correct
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: