How to use a Lucene Analyzer to tokenize a String?

后端 未结 4 1322
抹茶落季
抹茶落季 2020-12-04 16:38

Is there a simple way I could use any subclass of Lucene\'s Analyzer to parse/tokenize a String?

Something like:

String to_         


        
4条回答
  •  余生分开走
    2020-12-04 17:14

    The latest best practices, as another Stack Overflow answer indicates, seems to be to add an attribute to the token stream and later access that attribute, rather than getting an attribute directly from the token stream. And for good measure you can make sure the analyzer gets closed. Using the very latest Lucene (currently v8.6.2) the code would look like this:

    String text = "foo bar";
    String fieldName = "myField";
    List tokens = new ArrayList();
    try (Analyzer analyzer = new StandardAnalyzer()) {
      try (final TokenStream tokenStream = analyzer.tokenStream(fieldName, text)) {
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();
        while(tokenStream.incrementToken()) {
          tokens.add(charTermAttribute.toString());
        }
        tokenStream.end();
      }
    }
    

    After that code is finished, tokens will contain a list of parsed tokens.

    See also: Lucene Analysis Overview.

    Caveat: I'm just starting to write Lucene code, so I don't have a lot of Lucene experience. I have taken the time to research the latest documentation and related posts, however, and I believe that the code I've placed here follows the latest recommended practices slightly better than the current answers.

提交回复
热议问题