Lucene_demo02_分詞

1863 ワード

Lucene_demo02_分詞

/**
 *    
 */
public class AnalyzerTest {

	/**
	 *  :(Lucene )
	 * @throws Exception
	 */
	@Test
	public void testEN() throws Exception {
		String text = "Creates a searcher searching the index in the named directory";
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
		this.testAnalyzer(analyzer, text);
	}

	/**
	 *  : (Lucene )
	 * @throws Exception
	 */
	@Test
	public void testCH1() throws Exception {
		String text = "LBJ 2013 NBA ?";
		Analyzer analyzer = new ChineseAnalyzer();
		this.testAnalyzer(analyzer, text);
	}

	/**
	 *  : (Lucene )
	 * @throws Exception
	 */
	@Test
	public void testCH2() throws Exception {
		String text = "LBJ 2013 NBA ";
		Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_30);
		this.testAnalyzer(analyzer, text);
	}

	/**
	 *  :IK (Lucene )
	 * @throws Exception
	 */
	@Test
	public void testCH3() throws Exception {
		String text = "fasd";
		Analyzer analyzer = new IKAnalyzer();
		this.testAnalyzer(analyzer, text);
	}

	/**
	 *  
	 * @param analyzer
	 * @param text
	 * @throws Exception
	 */
	private void testAnalyzer(Analyzer analyzer, String text) throws Exception {
		TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
		tokenStream.addAttribute(TermAttribute.class);
		while (tokenStream.incrementToken()) {
			TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class);
			System.out.println(termAttribute.term());
		}
	}
}