anlp在功能上的擴(kuò)展主要體現(xiàn)在以下幾個(gè)方面:
?關(guān)鍵詞提取?
?自動(dòng)摘要
?短語提取?
?拼音轉(zhuǎn)換
?簡(jiǎn)繁轉(zhuǎn)換
?文本推薦
下面是?hanLP分詞器的代碼
注:使用maven依賴?
<dependency> ?
? ?<groupId>com.hankcs</groupId> ?
? ?<artifactId>hanlp</artifactId> ?
? ?<version>portable-1.3.4</version> ?
</dependency>?
使用了java8進(jìn)行處理
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.Dijkstra.DijkstraSegment;
import com.hankcs.hanlp.seg.NShort.NShortSegment;
import com.hankcs.hanlp.tokenizer.IndexTokenizer;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import com.hankcs.hanlp.tokenizer.SpeedTokenizer;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
public class HanLPTokenizer {
private static final Segment N_SHORT_SEGMENT = new NShortSegment().enableCustomDictionary(false)
.enablePlaceRecognize(true).enableOrganizationRecognize(true);
private static final Segment DIJKSTRA_SEGMENT = new DijkstraSegment().enableCustomDictionary(false)
.enablePlaceRecognize(true).enableOrganizationRecognize(true);
/**
* 標(biāo)準(zhǔn)分詞
* @param text
* @return
*/
public static List<String> standard(String text) {
List<String> list = new ArrayList<String>();
StandardTokenizer.segment(text).forEach(term -> {
if (StringUtils.isNotBlank(term.word)) {
list.add(term.word);
}
});
return list.stream().distinct().collect(Collectors.toList());
}
/**
* NLP分詞
* @param text
* @return
*/
public static List<String> nlp(String text) {
List<String> list = new ArrayList<String>();
NLPTokenizer.segment(text).forEach(term -> {
if (StringUtils.isNotBlank(term.word)) {
list.add(term.word);
}
});
return list.stream().distinct().collect(Collectors.toList());
}
/**
* 索引分詞
* @param text
* @return
*/
public static List<String> index(String text) {
List<String> list = new ArrayList<String>();
IndexTokenizer.segment(text).forEach(term -> {
if (StringUtils.isNotBlank(term.word)) {
list.add(term.word);
}
});
return list.stream().distinct().collect(Collectors.toList());
}
/**
* 極速詞典分詞
* @param text
* @return
*/
public static List<String> speed(String text) {
List<String> list = new ArrayList<String>();
SpeedTokenizer.segment(text).forEach(term -> {
if (StringUtils.isNotBlank(term.word)) {
list.add(term.word);
}
});
return list;
}
/**
* N-最短路徑分詞
* @param text
* @return
*/
public static List<String> nShort(String text) {
List<String> list = new ArrayList<String>();
N_SHORT_SEGMENT.seg(text).forEach(term -> {
if (StringUtils.isNotBlank(term.word)) {
list.add(term.word);
}
});
return list.stream().distinct().collect(Collectors.toList());
}
/**
* 最短路徑分詞
* @param text
* @return
*/
public static List<String> shortest(String text) {
List<String> list = new ArrayList<String>();
DIJKSTRA_SEGMENT.seg(text).forEach(term -> {
if (StringUtils.isNotBlank(term.word)) {
list.add(term.word);
}
});
return list.stream().distinct().collect(Collectors.toList());
}
public static void main(String[] args) {
String text = "測(cè)試勿動(dòng)12";
System.out.println("標(biāo)準(zhǔn)分詞:" + standard(text));
System.out.println("NLP分詞:" + nlp(text));
System.out.println("索引分詞:" + index(text));
System.out.println("N-最短路徑分詞:" + nShort(text));
System.out.println("最短路徑分詞分詞:" + shortest(text));
System.out.println("極速詞典分詞:" + speed(text));
}
}
文章來源于猴德華的博客