2012-11-17 16 views
12

Próbuję tokenize ciągi w ngrams. O dziwo w dokumentacji dla NGramTokenizer nie widzę metody, która zwróci pojedyncze ngramy, które zostały zgeometryzowane. W rzeczywistości widzę tylko dwie metody w klasie NGramTokenizer, które zwracają obiekty typu String.Java Lucene NGramTokenizer

Oto kod, który mam:

Reader reader = new StringReader("This is a test string"); 
NGramTokenizer gramTokenizer = new NGramTokenizer(reader, 1, 3); 
  1. Gdzie są ngrams które tokenized?
  2. Jak uzyskać dane wyjściowe w łańcuchach/słowach?

Chcę, aby moje wyniki były jak: To, jest, a, test, ciąg, To jest, jest, test, ciąg testowy, To jest, jest test, ciąg testowy.

Odpowiedz

18

Nie sądzę, że znajdziesz to, czego szukasz, aby znaleźć metody zwracające ciąg. Musisz poradzić sobie z Attribute s.

Powinna działać coś takiego:

Reader reader = new StringReader("This is a test string"); 
NGramTokenizer gramTokenizer = new NGramTokenizer(reader, 1, 3); 
CharTermAttribute charTermAttribute = gramTokenizer.addAttribute(CharTermAttribute.class); 
gramTokenizer.reset(); 

while (gramTokenizer.incrementToken()) { 
    String token = charTermAttribute.toString(); 
    //Do something 
} 
gramTokenizer.end(); 
gramTokenizer.close(); 

Koniecznie reset() tokenizera to czy musi być ponownie wykorzystane po to, choć.


tokenizing zgrupowanie słowy, zamiast znaków, za komentarze:

Reader reader = new StringReader("This is a test string"); 
TokenStream tokenizer = new StandardTokenizer(Version.LUCENE_36, reader); 
tokenizer = new ShingleFilter(tokenizer, 1, 3); 
CharTermAttribute charTermAttribute = tokenizer.addAttribute(CharTermAttribute.class); 

while (tokenizer.incrementToken()) { 
    String token = charTermAttribute.toString(); 
    //Do something 
} 
+0

Co mogę zrobić z ciągami znaków zamiast pod względem atrybutów? Tak więc moje wyniki mogłyby wyglądać następująco: This, is, a, test, string, This is, is, test, ... ciąg testowy. – CodeKingPlusPlus

+1

Okay, to nie jest to, do czego służy sterownik NGramToken. Myślę, że to, czego będziesz chciał użyć, to ShingleFilter w połączeniu z StandardTokenizer. Zaktualizuję moją odpowiedź, łatwiej ją wyrazić ... – femtoRgon

+0

Czy znasz jakieś filtry słów przystankowych, których mogę użyć w procesie tokjonowania? – CodeKingPlusPlus

0

Bez tworzenia programu testowego, przypuszczam, że incrementToken() zwraca następny token, który będzie jednym z ngramów.

na przykład za pomocą długości Ngram 1-3 z łańcucha 'A B C D' NGramTokenizer może powrócić:

a 
a b 
a b c 
b 
b c 
b c d 
c 
c d 
d 

gdzie 'a', 'A B', itd. Są otrzymane ngrams.

[Edytuj]

Można również zajrzeć do Querying lucene tokens without indexing, jak mówi o wgląd do tokenu strumienia.

+1

Problemem jest incrementToken() zwraca wartość logiczną ... – CodeKingPlusPlus

1

Dla najnowszej wersji Lucene (4.2.1), jest to czysty kod, który działa. Przed wykonaniem tego kodu, trzeba importować pliki jar 2:

  • Lucene-core-4.2.1.jar
  • Lucene-analuzers-common-4.2.1.jar

znaleźć te pliki pod numerem http://www.apache.org/dyn/closer.cgi/lucene/java/4.2.1

//LUCENE 4.2.1 
Reader reader = new StringReader("This is a test string");  
NGramTokenizer gramTokenizer = new NGramTokenizer(reader, 1, 3); 

CharTermAttribute charTermAttribute = gramTokenizer.addAttribute(CharTermAttribute.class); 

while (gramTokenizer.incrementToken()) { 
    String token = charTermAttribute.toString(); 
    System.out.println(token); 
} 
0

pakiet ngramalgoimpl; import java.util.*;

public class NGR {

public static List<String> n_grams(int n, String str) { 
    List<String> n_grams = new ArrayList<String>(); 
    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(); 
    } 
} 

}

+0

proszę podać kontekst, co robi ten kod i jak zapewnia odpowiedź na pytanie? –

+0

@KevinKloet zobacz pytanie i podaną odpowiedź –