中文分词器解析hanlp分词器接口设计:提供外部接口:分词器封装为静态工具类,并提供了简单的接口标准分词标准分词是最常用的分词器,基于HMM-Viterbi实现,开启了中国人名识别和音译人名识别,调用方法如下:ListTermtermList=HanLP.segment(商品和服务);System.out.println(termList);HanLP.segment其实是对StandardTokenizer.segment的包装。/***分词**@paramtext文本*@return切分后的单词*/publicstaticListTermsegment(Stringtext){returnStandardTokenizer.segment(text.toCharArray());}/***创建一个分词器br*这是一个工厂方法br*与直接new一个分词器相比,使用本方法的好处是,以后HanLP升级了,总能用上最合适的分词器*@return一个分词器*/publicstaticSegmentnewSegment(){returnnewViterbiSegment();//Viterbi分词器是目前效率和效果的最佳平衡}publicclassStandardTokenizer{/***预置分词器*/publicstaticfinalSegmentSEGMENT=HanLP.newSegment();/***分词*@paramtext文本*@return分词结果*/publicstaticListTermsegment(Stringtext){returnSEGMENT.seg(text.toCharArray());}/***分词*@paramtext文本*@return分词结果*/publicstaticListTermsegment(char[]text){returnSEGMENT.seg(text);}/***切分为句子形式*@paramtext文本*@return句子列表*/publicstaticListListTermseg2sentence(Stringtext){returnSEGMENT.seg2sentence(text);}}publicstaticSegmentnewSegment(){returnnewViterbiSegment();//Viterbi分词器是目前效率和效果的最佳平衡}/***Viterbi分词器br*也是最短路分词,最短路求解采用Viterbi算法**@authorhankcs*/publicclassViterbiSegmentextendsWordBasedGenerativeModelSegmentNLP分词NLP分词NLPTokenizer会执行全部命名实体识别和词性标注。,调用方法如下:ListTermtermList=NLPTokenizer.segment(中国科学院计算技术研究所的宗成庆教授正在教授自然语言处理课程);System.out.println(termList);NLP分词NLPTokenizer会执行全部命名实体识别和词性标注。所以速度比标准分词慢,并且有误识别的情况。publicclassNLPTokenizer{/***预置分词器*/publicstaticfinalSegmentSEGMENT=HanLP.newSegment().enableNameRecognize(true).enableTranslatedNameRecognize(true).enableJapaneseNameRecognize(true).enablePlaceRecognize(true).enableOrganizationRecognize(true).enablePartOfSpeechTagging(true);publicstaticListTermsegment(Stringtext){returnSEGMENT.seg(text);}/***分词*@paramtext文本*@return分词结果*/publicstaticListTermsegment(char[]text){returnSEGMENT.seg(text);}/***切分为句子形式*@paramtext文本*@return句子列表*/publicstaticListListTermseg2sentence(Stringtext){returnSEGMENT.seg2sentence(text);}}索引分词索引分词IndexTokenizer是面向搜索引擎的分词器,能够对长词全切分,另外通过term.offset可以获取单词在文本中的偏移量。调用方法如下:ListTermtermList=IndexTokenizer.segment(主副食品);for(Termterm:termList){System.out.println(term+[+term.offset+:+(term.offset+term.word.length())+]);}publicclassIndexTokenizer{/***预置分词器*/publicstaticfinalSegmentSEGMENT=HanLP.newSegment().enableIndexMode(true);publicstaticListTermsegment(Stringtext){returnSEGMENT.seg(text);}/***分词*@paramtext文本*@return分词结果*/publicstaticListTermsegment(char[]text){returnSEGMENT.seg(text);}/***切分为句子形式*@paramtext文本*@return句子列表*/publicstaticListListTermseg2sentence(Stringtext){returnSEGMENT.seg2sentence(text);}}繁体分词繁体分词TraditionalChineseTokenizer可以直接对繁体进行分词,输出切分后的繁体词语。调用方法如下:ListTermtermList=TraditionalChineseTokenizer.segment(大衛貝克漢不僅僅是名著名球員,球場以外,其妻為前辣妹合唱團成員維多利亞·碧咸,亦由於他擁有突出外表、百變髮型及正面的形象,以至自己品牌的男士香水等商品,及長期擔任運動品牌Adidas的代言人,因此對大眾傳播媒介和時尚界等方面都具很大的影響力,在足球圈外所獲得的認受程度可謂前所未見。);System.out.println(termList);/***繁体中文分词器**@authorhankcs*/publicclassTraditionalChineseTokenizer{/***预置分词器*/publicstaticSegmentSEGMENT=HanLP.newSegment();privatestaticListTermsegSentence(Stringtext){if(text.length()==0)returnCollections.emptyList();LinkedListResultTermStringtsList=CommonAhoCorasickSegmentUtil.segment(text,TraditionalChineseDictionary.trie);StringBuildersbSimplifiedChinese=newStringBuilder(text.length());booleanequal=true;for(ResultTermStringterm:tsList){if(term.label==null)term.label=term.word;elseif(term.label.length()!=term.word.length())equal=false;sbSimplifiedChinese.append(term.label);}StringsimplifiedChinese=sbSimplifiedChinese.toString();ListTermtermList=SEGMENT.seg(simplifiedChinese);if(equal){intoffset=0;for(Termterm:termList){term.word=text.substring(offset,offset+term.length());term.offset=offset;offset+=term.length();}}else{IteratorTermtermIterator=termList.iterator();IteratorResultTermStringtsIterator=tsList.iterator();ResultTermStringtsTerm=tsIterator.next();intoffset=0;while(termIterator.hasNext()){Termterm=termIterator.next();term.offset=offset;if(offsettsTerm.offset+tsTerm.word.length())tsTerm=tsIterator.next();if(offset==tsTerm.offset&&term.length()==tsTerm.label.length()){term.word=tsTerm.word;}elseterm.word=SimplifiedChineseDictionary.convertToTraditionalChinese(term.word);offset+=term.length();}}returntermList;}publicstaticListTermsegment(Stringtext){ListTermtermList=newLinkedListTerm();for(Stringsentence:SentencesUtil.toSentenceList(text)){termList.addAll(segSentence(sentence));}returntermList;}/***分词**@paramtext文本*@return分词结果*/publicstaticListTermsegment(char[]text){returnsegment(CharTable.convert(text));}/***切分为句子形式**@paramtext文本*@return句子列表*/publicstaticListListTermseg2sentence(Stringtext){ListListTermresultList=newLinkedListListTerm();{for(Stringsentence:SentencesUtil.toSentenceList(text)){resultList.add(segment(sentence));}}returnresultList;}}极速词典分词极速分词是词典最长分词,速度极其快,精度一般。调用方法如下:Stringtext=江西鄱阳湖干枯,中国最大淡水湖变成大草原;System.out.println(SpeedTokenizer.segment(text));longstart=System.currentTimeMillis();intpressure=1000000;for(inti=0;ipressure;++i){SpeedTokeni