原文地址http://blog.csdn.net/bitcarmanlee/article/details/53607776
最近需要使用分詞搜索,在網上查找了許多,看了很多分詞工具的對比,感覺這個還是非常不錯的,相比較與ik更適合我們使用。
在網上查找了許多配置都不能用,大多數都是因爲jar包版本不匹配,在這裏我使用了兩個jar包
ansj_seg 5.0.4 http://mvnrepository.com/artifact/org.ansj/ansj_seg/5.1.4
nlp-lang 1.7.3 http://mvnrepository.com/artifact/org.nlpcn/nlp-lang/1.7.3 注意這個包必須要最新的!!!!
1.給ansj來個硬廣
項目的github地址:https://github.com/NLPchina/ansj_seg
git文檔地址(最新)https://github.com/NLPchina/ansj_seg/wiki
項目的文檔地址(有點舊):http://nlpchina.github.io/ansj_seg/
2.配置maven
在maven項目的pom中配置ansj的dependency:
<!-- https://mvnrepository.com/artifact/org.nlpcn/nlp-lang --> <dependency> <groupId>org.nlpcn</groupId> <artifactId>nlp-lang</artifactId> <version>1.7.3</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.ansj/ansj_seg --> <dependency> <groupId>org.ansj</groupId> <artifactId>ansj_seg</artifactId> <version>5.1.3</version> </dependency>
3.使用實例
先不說那麼多的理論,直接上可以run起來的代碼。畢竟在工作過程中,解決問題是第一位的。只有解決完問題以後,我們纔有功夫來慢慢研究其中的門道。
對於分詞來說,最重要的任務無非就是拿到切分以後的結果(詞)。直接看代碼:
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import java.util.*;
public class AnsjTest {
public static void test() {
//只關注這些詞性的詞
Set<String> expectedNature = new HashSet<String>() {{
add("n");add("v");add("vd");add("vn");add("vf");
add("vx");add("vi");add("vl");add("vg");
add("nt");add("nz");add("nw");add("nl");
add("ng");add("userDefine");add("wh");
}};
String str = "歡迎使用ansj_seg,(ansj中文分詞)在這裏如果你遇到什麼問題都可以聯繫我.我一定盡我所能.幫助大家.ansj_seg更快,更準,更自由!" ;
Result result = ToAnalysis.parse(str); //分詞結果的一個封裝,主要是一個List<Term>的terms
System.out.println(result.getTerms());
List<Term> terms = result.getTerms(); //拿到terms
System.out.println(terms.size());
for(int i=0; i<terms.size(); i++) {
String word = terms.get(i).getName(); //拿到詞
String natureStr = terms.get(i).getNatureStr(); //拿到詞性
if(expectedNature.contains(natureStr)) {
System.out.println(word + ":" + natureStr);
}
}
}
public static void main(String[] args) {
test();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
將代碼run起來:
歡迎/v, 使用/v, ansj/en, _, seg/en, ,, (, ansj/en, 中文/nz, 分詞/n, ), 在/p, 這裏/r, 如果/c, 你/r, 遇到/v, 什麼/r, 問題/n, 都/d, 可以/v, 聯繫/v, 我/r, ./m, 我/r, 一定/d, 盡/v, 我/r, 所/u, 能/v, ./m, 幫助/v, 大家/r, ./m, ansj/en, _, seg/en, 更/d, 快/a, ,, 更/d, 準/a, ,, 更/d, 自由/a, !]
45
歡迎:v
使用:v
中文:nz
分詞:n
遇到:v
問題:n
可以:v
聯繫:v
盡:v
能:v
幫助:v
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
上面的代碼就拿到了我們想要的分詞結果!
4.自定義詞典
dic=library/car.dic
#set real name
isRealName=true
#isNameRecognition default true
isNameRecognition=true
#isNumRecognition default true
isNumRecognition=true
#digital quantifier merge default true
isQuantifierRecognition=true
isSkipUserDefine=true
Forest forest = Library.makeForest("library/car.dic");
MyStaticValue.putLibrary(DicLibrary.DEFAULT,"library/car.dic",forest);
需要實例化Forest對象 ,使用MyStaticValue的方法會加載進去