usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingLucene.Net.Analysis;usingLucene.Net.Analysis.PanGu;usingLucene.Net.Analysis.Standard;usingLucene.Net.Documents;usingLucene.Net.Index;usingLucene.Net.Search;usingLucene.Net.Store;usingNSHARP.SEARCHENGINE.LUCENE.ANALYSIS.CJK;usingpangu.dict;namespacey_lucen.net{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } /// <summary> ///One dollar participle/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton1_Click (Objectsender, EventArgs e) { //the base class of the word segmentation algorithmAnalyzer analyzer=NewStandardAnalyzer ();//Specify a unary word-breaker algorithmTokenstream Tokenstream = Analyzer. Tokenstream ("",NewStringReader ("Beijing, Hi, welcome all of you."));//ReadLucene.Net.Analysis.Token token=NULL; while((Token=tokenstream.next ())! =NULL{listBox1.Items.Add (token). Termtext ()); } } /// <summary> ///two Yuan participle/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton2_Click (Objectsender, EventArgs e) {Analyzer Analyzer=NewCjkanalyzer ();//specify two-dollar word segmentation algorithmTokenstream Tokenstream = Analyzer. Tokenstream ("",NewStringReader ("Beijing, Hi, welcome all of you."));//ReadLucene.Net.Analysis.Token Token =NULL; while(token = Tokenstream.next ())! =NULL{listBox1.Items.Add (token). Termtext ()); } } /// <summary> ///Pangu Participle/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton3_Click (Objectsender, EventArgs e) {Analyzer Analyzer=NewPanguanalyzer ();//Specify Pangu segmentation algorithmTokenstream Tokenstream = Analyzer. Tokenstream ("",NewStringReader ("Beijing, hi welcome all of you, public places, the master said when issued security monitoring room love is very understanding is a waste love"));//ReadLucene.Net.Analysis.Token Token =NULL; while(token = Tokenstream.next ())! =NULL{listBox1.Items.Add (token). Termtext ()); } //worddictionary word=new worddictionary (); //Word. Insertword (,); } Private voidButton4_Click (Objectsender, EventArgs e) { //Specify the directory lucene.net to store stringIndexpath =@"D:\lucenedir";//Note the case is the same as the folder on the disk, or you will get an error. Place the word breaker that you created in the directory. //Open the directory used to store the dataFsdirectory Directory=fsdirectory.open (NewDirectoryInfo (Indexpath));//Specify index file (open index directory) FS is worth it is filesystem//read the data in the directory BOOLIsupdate = indexreader.indexexists (directory);//Indexreader: The class that reads the index, the function of the statement: determine if the index Library folder exists and therefore the signature file exists if(isupdate) {//only one piece of code can write to the index library at the same time. When you open Directory with IndexWriter, the index library files are automatically locked. //if the index directory is locked (such as an unexpected program exit during indexing), unlock it first (hint: If I am now writing a lock, but have not finished writing, this time another request, so long do not understand the lock? This problem will be resolved later) if(indexwriter.islocked (directory)) {Indexwriter.unlock (directory); } } //lucene.net file, Word segmentation algorithm, whether the database exists does not exist on the creationIndexWriter writer=NewIndexWriter (Directory,NewPanguanalyzer (), isupdate,lucene.net.index.indexwriter.maxfieldlength.unlimited);//writes an index to the index library. It's locked in here . for(inti =1; I < +Ten; i++) { stringtxt = file.readalltext (@"D:\abc\aa\ test file \"+ i +". txt", System.Text.Encoding.Default);//note The code for this placeDocument document=NewDocument ();//represents a document//Field.Sote.YES: Indicates whether the original value is stored. Doc.get ("number") can be taken out of the value only when Field.Stord.YES is behind, Field.Index.NOT_ANALYZED: No word-breaker is saved.Document. ADD (NewField (" Number", i.ToString (), field.store.yes,field.index.not_analyzed)); //Field.Index.ANALYZED: To do word-breaker: that is, to make a full-text field to set the word breaker (because you want to make a fuzzy query)//Lucene.Net.Documents.Field.TermVector.WITH_POSTITIONS_OFFSETS: Not only save the word, but also save the distance. Document. ADD (NewField ("Body", txt,field.store.yes,field.index.analyzed,lucene.net.documents.field.termvector.with_positions_offsets)); Writer. Adddocument (document); } writer. Close ();//will automatically unlockDirectory. Close ();//do not forget close, otherwise the index results cannot be searched } Private voidButton5_click (Objectsender, EventArgs e) { stringIndexpath =@"D:\lucenedir"; stringKW ="Object Oriented";//Search for keywords//Open FileFsdirectory Directory=fsdirectory.open (NewDirectoryInfo (Indexpath),Newnolockfactory ()); //Open FileIndexreader Reader=indexreader.open (Directory,true); Indexsearcher Searcher=NewIndexsearcher (reader); //Search CriteriaPhrasequery query=NewPhrasequery (); //foreach (string word in kw. Split ("))//first with a space, let users go to participle, the space is separated by the word "computer professional"//{ //query. ADD (New term ("body", word)); //} //query. ADD (New term ("body", "language"));//You can add a query condition, two are add relationships, the order is not related//query. ADD (New term ("Body", "university Student" ));Query. ADD (NewTerm ("Body", kw));//articles in body containing KWQuery. Setslop ( -);//the maximum distance between multiple query criteria. It is meaningless to be too far in the article. (for example, "college student" This query condition and "resume" this query condition if the interval of the word too much is meaningless),//TopScoreDocCollector is a container for the query structureTopScoreDocCollector Collector=topscoredoccollector.create ( +,true); Searcher. Search (Query,NULL, collector);//Query based on query criteria, query structure into collector containerscoredoc[] Docs = Collector. Topdocs (0, collector. Gettotalhits ()). Scoredocs;//get the document that is known as the query result, gettotalhits (); Indicates the total number of bars Topdocs (300,20); Indicates the document content of 300 (starting from 300) to 320 (end)//can be used to achieve paging functionality This. ListBox1.Items.Clear (); for(inti =0; I < Docs. Length; i++) { intDocId = Docs[i].doc;//gets the ID of the query result document (Lucene internally assigned ID)Document doc =Searcher. Doc (DOCID); This. LISTBOX1.ITEMS.ADD (Doc. Get (" Number") +"\ r \ n"); This. LISTBOX1.ITEMS.ADD (Doc. Get ("Body") +"\ r \ n"); This. LISTBOX1.ITEMS.ADD ("-------------\ r \ n"); } } }}
16-luncec.net usage