Java Lucene NGramTokenizer

后端 未结 4 1208
梦毁少年i
梦毁少年i 2021-01-04 05:31

I am trying tokenize strings into ngrams. Strangely in the documentation for the NGramTokenizer I do not see a method that will return the individual ngrams that were tokeni

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 06:05

    package ngramalgoimpl;
    import java.util.*;
    
    public class ngr {
    
        public static List n_grams(int n, String str) {
            List n_grams = new ArrayList();
            String[] words = str.split(" ");
            for (int i = 0; i < words.length - n + 1; i++)
                n_grams.add(concatination(words, i, i+n));
            return n_grams;
        }
         /*stringBuilder is used to cancatinate mutable sequence of characters*/
        public static String concatination(String[] words, int start, int end) {
            StringBuilder sb = new StringBuilder();
            for (int i = start; i < end; i++)
                sb.append((i > start ? " " : "") + words[i]);
            return sb.toString();
        }
    
        public static void main(String[] args) {
            for (int n = 1; n <= 3; n++) {
                for (String ngram : n_grams(n, "This is my car."))
                    System.out.println(ngram);
                System.out.println();
            }
        }
    }
    

提交回复
热议问题