---
title: "IKAnalyzer和Ansj切词Demo"
author: "小网客"
date: "2013-03-28T17:50:58Z"
tags: []
categories: ["开源软件"]
slug: "ikanalyzer-ansj-demo"
draft: false
lastmod: "2013-03-28T17:51:56Z"
id: 1838171
url: "https://snv.iteye.com/blog/1838171"
---

IKAnalyzer

        IKAnalyzer是一个开源的，基于java语言开发的轻量级的中文分词工具包。切词Demo代码如下：

```
public static void ikSeg() throws Throwable {
	String content = "Java编程思想(第4版)";
	IKSegmentation ikSeg = new IKSegmentation(new StringReader(content), true);

	Lexeme l = null;
	while ((l = ikSeg.next()) != null) {
		String word = l.getLexemeText();
		int wordType = l.getLexemeType();
		System.out.println(wordType + "->" + word);
	}
}
```

 

 Ansj：

        Ansj中文分词这是一个ictclas的java实现.基本上重写了所有的数据结构和算法.词典是用的开源版的ictclas所提供的.切词Demo代码如下：

```
public static void ansjSeg() throws Throwable {
	String content = "Java编程思想(第4版)";
	Analysis udf = new ToAnalysis(new StringReader(content));
	Term term = null;
	while ((term = udf.next()) != null) {
		TermNature[] termNatures = term.getTermNatures().termNatures;
		String wordType = termNatures[0].nature.natureStr;
		String word = term.getName();
		System.out.println(wordType + "->" + word);

	}

}
```