《Lucene47IKAnalyzer中文分词入门教程.docx》由会员分享,可在线阅读,更多相关《Lucene47IKAnalyzer中文分词入门教程.docx(22页珍藏版)》请在第壹文秘上搜索。
1、1.ucene4.7+IKAnaIyZer中文分词入门教程作者:大明日期:2019-4-1环境:1. IUCene版本:4.7下载地址:下载页面中的2. IKAnaIyZer版本:IKAnalyer2019-FFHotfix1下载地址:选择最上面的源码包。目前只有这个源码包才能支持Iucene的4.7版本。3. 参考文章:此篇文章作者用的是IUCCne3.5版本,我做了肯定幅度的修改,使其在4.7中可用。下面进入正式部署阶段:1 .在eelipse中新建一个java项目:Iucenetest2 .解压3 .进入到解压后的书目,搜寻*.jar4 .搜寻出来的结果是lucene4.7供应的全部的j
2、ar包,依据须要拷贝不同的jar包到自己的工程。本例子须要如下几个jar包:5 .将以上几个Jar包拷贝到Iucenetest下的Iib文件夹下,假如没有lib,则新建。6 .在eclipse中,打开lib,选中这儿个jar包,点右键一buildparhaddtobuildpath7,解压下载的IKAnalyzer2019FF_hfl_source.rar8 .进入到IKAnalyzer2019FFIf1.SOUrCesrc将如下全部的文件拷贝到IUCCneteslsrc下9 .刷新eclipse工程。假如源码报错,可能是编码格式的问题。解决方法:在Iuccnetest上点右键,选择最终一项P
3、roperties0如下图所示选择Utf-8,点确定10 .至此,环境已经搭建完毕。假如嫌ik的源码烦,可以自行打成jar包。目前eclipse项目的结构图如下:11 .加入测试的例子。在src新建ik包,新建下面3个Java文件(I)DataFactorypackageik:importjava.util.Array1.ist;importjava.util.1.ist;publicclassDataFactoryprivatestaticDataFactorydataFactory=newDataFactoryO;privateDataFactoryOpublic1.istgetDataO
4、1.istlist=newArray1.ist();list.add(newMedicine。,”银花感冒颗粒,功能主治:银花感冒颗粒,头痛,清热,解表,利咽.”);list,add(newMedicine(2,感冒止咳糖浆功能主治:感冒止咳糖浆,解表清热,止咳化痰。”);list,add(newMedicine(3,感冒灵颗粒功能主治:解热镇痛。头痛,清热。”);list.add(newMedicine(4,感冒灵胶奏功能主治:银花感冒颗粒,头痛,清热,解表,利咽。”);list.add(ne睛MediCine(5,仁和感冒颗粒功能主治:疏风清热,宣肺止咳,解表清热,止咳化痰);return
5、list;)publicstaticDataFactorygetInstanceOreturndataEactory;(2)1.uceneIKUtilpackageik:importjava.io.FiIe;importjava.io.IOException;importjava,uti1.Array1.ist;importjava.util.1.ist:importorg.apache.Iucene.analysis.Analyzer:importorg.apache.Iucene.document.Document;importorg.apache.Iucene.document.Fiel
6、d;importorg.apache.Iucene.document.FieldType;importorg.apache.Iucene.document.TextField;importorg.apache.Iucene.index.DirectoryReader;importorg.apache.Iucene.index.IndexReader:importorg.apache.Iucene.index.IndexWriter;importorg.apache,lcene.index.IndexWriterConfig;importorg.apache.Iucene.index.Term;
7、import0rg.apache.Iucene.queryparser,classic.MultiFieldQueryParser;importorg.apache.Iucene.search.IndexSearcher;importorg.apache.Iucene.search.Query;importorg.apache.Iucene.search.ScoreDoc;importorg.apache.Iucene.searchDocs:importorg.apache,ucene.search,highlight.Formatter;importorg.apache.Iucene.sea
8、rch,highlight.Fragmenter:importorg.apache,ucene.search,highlight.Highlighter;importorg.apache.Iucene.search,highlight.QueryScorer;importorg.apache,uce11e.search,highlight.Scorer;importorg.apache.Iucene.search,highlight.SimpleFragmcnler;importorg.apache.Iucene.search,highlight.SimpleHTU1.Formatter;im
9、portorg.apache.Iucene.store.Directory;importorg.apache,ucene.store.FSDirectory;importorg.apache.Iucene.util.Version;importorg.wltea.analyzer.Iuccnc.TKAnalyzer;publicclass1.uceneIKUtilprivateDirectorydirectory:privateAnalyzeranalyzer;*带参数构造,参数用来指定索引文件书目pubic1.uceneIKUtil(StringindexFiIcPath)try(direc
10、tory=FSDirectory.open(newFile(indexFiIePath);analyzer=newIKAnalyzerO:catch(IOExceptione)e.PrintStackTraceO;)* 创建索引* Description:* authordennisitQ163Apr3,2019* throwsException* /publicvoidCreateIndex()throwsExceptionIndexWriterConfigindexWr1.terConfig=newIndexWriterConfig(Version.1.UCENE_47,analyzer)
11、;IndexWriterindexWriter=newIndexWriter(directory,indexWriterConfig);indexWriter.deletellO;1.istlistDataFactory.getInstanceO.getDataO;for(inti=0;ilist.size();i+)Medicinemedicine=list.get(i);DocumentdocumentaddDocument(medicine,getIclO,medicine.getName(),medicine.gctFunction();/indexWriter.addDocument
12、(document);假如用添加,每次都是新的。所以须要用UPdaIeindexWriter.UpdateDocument(newTenn(id,medicine,getld().IoString(),document);indexWriter.closeO;I* Description:* 触IUIhordennisit0163Apr3,2019* paramid* paramcontent* return* /publicDocumentaddDocument(Integerid,Stringname,Stringfunction)(Documentdoc=newDocumentO;/Fi
13、eld.Index.NO表示不索引/Field.Index.ANA1.YZED表示分词且索引/Field.Index.NOT-N1.YZED表示不分词但索引/doc.add(newField(*id*,String,valuef(id),Field.Store.YES,Field.Index.NOT_ANA1.YZED);/doc.add(newField(name”,name,Field.Store.YES,Field.Index.ANA1.YZED)/doc.add(newField(function”,function,Field.Store.YES,Field.Index.AN1.YZ
14、ED);FieldTypeft=newFieldTypeO:ft.SetStored(true);ft.SetIndexed(true):ft.setTokenized(false);doc.add(newField(id”,String.VaIUeOf(id),ft);doc.add(newField(name”,name,TextField.TYPE_STORED);doc.add(newField(*function*,function,TextField.TYPE_STORED):returndoc:)* Description:更新索引* authordennisit163Apr3,
15、2019* Wparamid* paramtitle* Paramcontent* /publicvoidupdate(Integerid,Stringtitle,Stringcontent)try(IndexWriterConfigindexWriterConfig=new1ndexWriterConfig(Version.1.UCENE_47,analyzer);IndexWriter(directory,indexWriterConfig);Documentdocument=addDocumcnt(id,title,content);Termterm=newTerm(id”,String,valuef(id);indexWriter.UpdateDocument(te