In this version: we use Lucene 3.6, IKAnalyzer 3.2. For built-in analyzers such as simple, standard analyzers, this method also can be applied.
- package com.segment;
- import java.io.StringReader;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.Token;
- import org.apache.lucene.analysis.TokenStream;
- import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
- import org.apache.lucene.analysis.tokenattributes.TermAttribute;
- import org.apache.lucene.util.AttributeImpl;
- import org.wltea.analyzer.lucene.IKAnalyzer;
- public class Segment {
- public static String show(Analyzer a, String s) throws Exception {
- StringReader reader = new StringReader(s);
- TokenStream ts = a.tokenStream(s, reader);
- String s1 = "", s2 = "";
- boolean hasnext= ts.incrementToken();
- //Token t = ts.next();
- while (hasnext) {
- //AttributeImpl ta = new AttributeImpl();
- CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class);
- //TermAttribute ta = ts.getAttribute(TermAttribute.class);
- s2 = ta.toString() + " ";
- s1 += s2;
- hasnext = ts.incrementToken();
- }
- return s1;
- }
- public String segment(String s) throws Exception {
- Analyzer a = new IKAnalyzer();
- return show(a, s);
- }
- public static void main(String args[])
- {
- String name = "some test strings here!!!";
- Segment s = new Segment();
- String test = "";
- try {
- System.out.println(test+s.segment(name));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
No comments:
Post a Comment