標籤:lucene.net ictclas 站內搜尋 ictclas2014
分詞系統建立完畢,這是基礎也是核心,後面我們建立索引要用到分詞系統。
下面依次講解索引的建立,索引的尋找。
分詞系統建立完畢,這是基礎也是核心,後面我們建立索引要用到分詞系統。下面依次講解索引的建立,索引的尋找。
索引的建立採用的是倒排序,原理就是遍曆所有的文本,對其進行分詞,然後把分的詞彙建立索引表。形式類似如下:
詞彙 出現詞彙的篇章1,篇章2,篇章3……
建立索引的時候要注意這樣的Document,Field這倆術語。Document代表的是一個文檔,它裡麵包含一個或者多個Filed,Field表示的就是一種域,你可以在一個Document裡面添加各種各樣的域,名字自己起,但是關於文檔的內容一定要加進去,方式如下所示:
doc.Add(new Field("contents", str, Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
整個索引的建立如下所示:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;using Lucene.Net.Analysis;using Lucene.Net.Analysis.Standard;using Lucene.Net.Index;using Lucene.Net.Documents;using Lucene.Net.Search;using Lucene.Net.Analysis.DChinese;using Version = Lucene.Net.Util.Version;using FSDirectory = Lucene.Net.Store.FSDirectory;using NativeFSLockFactory = Lucene.Net.Store.NativeFSLockFactory;namespace WebApplication6{ public class IndexFiles { public static bool CreateIndexFromFile(DirectoryInfo docDir, DirectoryInfo IndexDir) { string strUserDicPath = System.AppDomain.CurrentDomain.BaseDirectory; string strTestDic = strUserDicPath; HashSet<string> lstStopWords = new HashSet<string>(); strUserDicPath = strUserDicPath + "UserDictionary\\StopWords.txt"; string[] strs = null; StreamWriter sw = new StreamWriter(strTestDic + "UserDictionary\\StopTest.txt"); using (StreamReader strReader = new StreamReader(strUserDicPath)) { string strLine; while ((strLine = strReader.ReadLine()) != null) { strLine = strLine.Trim(); strs = strLine.Split(); foreach (string str in strs) { lstStopWords.Add(str); sw.WriteLine(str); } } strReader.Close(); sw.Close(); } bool bExist = File.Exists(docDir.FullName) || Directory.Exists(docDir.FullName); if (!bExist) { return false; } //using (IndexWriter writer = new IndexWriter(FSDirectory.Open(IndexDir), new DChineseAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED) ) //IndexWriter writer = new IndexWriter(fsDirrctory, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED); FSDirectory fsDirrctory = FSDirectory.Open(IndexDir, new NativeFSLockFactory()); Analyzer analyzer = new DChineseAnalyzer(Version.LUCENE_30,lstStopWords); IndexWriter writer = new IndexWriter(fsDirrctory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED); try { IndexDirectory(writer, docDir); writer.Optimize(); writer.Commit(); } finally { writer.Dispose(); fsDirrctory.Dispose(); } return true; } internal static void IndexDirectory(IndexWriter writer, DirectoryInfo directory) { foreach (var subDirectory in directory.GetDirectories()) IndexDirectory(writer, subDirectory); foreach (var file in directory.GetFiles()) IndexDocs(writer, file); } internal static void IndexDocs(IndexWriter writer, FileInfo file) { Console.Out.WriteLine("adding " + file); try { writer.AddDocument(Document(file)); } catch (FileNotFoundException) { // At least on Windows, some temporary files raise this exception with an // "access denied" message checking if the file can be read doesn't help. } catch (UnauthorizedAccessException) { // Handle any access-denied errors that occur while reading the file. } catch (IOException) { // Generic handler for any io-related exceptions that occur. } } public static Document Document(FileInfo f) { // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a field that is // indexed (i.e. searchable), but don't tokenize the field into words. doc.Add(new Field("path", f.FullName, Field.Store.YES, Field.Index.NOT_ANALYZED)); // Add the last modified date of the file a field named "modified". Use // a field that is indexed (i.e. searchable), but don't tokenize the field // into words. doc.Add(new Field("modified", DateTools.TimeToString(f.LastWriteTime.Millisecond, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in the system's default encoding. // If that's not the case searching for special characters will fail. string str = File.ReadAllText(f.FullName); //doc.Add(new Field("contents", new StreamReader(f.FullName, System.Text.Encoding.UTF8))); doc.Add(new Field("contents", str, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); // return the document return doc; } }}
尋找的實現:
Lucene.net中有多種多樣的尋找類,但是如果要實現多條件查詢就要使用PhraseQuery
類。通過搜尋函數把搜尋結果放到容器裡面。
最後結果的呈現時候,我們把搜尋結果放到列表裡面,如果還要顯示關鍵詞加亮,那麼就需要做一點額外的工作。在這裡我是通過ColorWord這個類實現的。具體的搜尋代碼如下所示:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;using Lucene.Net.Analysis;using Lucene.Net.Analysis.DChinese;using Lucene.Net.Documents;using Lucene.Net.QueryParsers;using Lucene.Net.Index;using Lucene.Net.Search;using FSDirectory = Lucene.Net.Store.FSDirectory;using NoLockFactory = Lucene.Net.Store.NoLockFactory;using Version = Lucene.Net.Util.Version;namespace WebApplication6{ public static class SearchFiles { public static List<ItemList> SearchIndex(DirectoryInfo dirIndex, List<string> termList) { FSDirectory dirFS = FSDirectory.Open(dirIndex, new NoLockFactory()); IndexReader reader = IndexReader.Open(dirFS,true); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new DChineseAnalyzer(Version.LUCENE_30); PhraseQuery query = new PhraseQuery(); foreach (string word in termList) { query.Add( new Term("contents",word) ); } query.Slop = 100; TopScoreDocCollector collector = TopScoreDocCollector.Create(1000, true); searcher.Search(query,collector); ScoreDoc[] hits = collector.TopDocs().ScoreDocs; List<ItemList> lstResult = new List<ItemList>(); for (int i = 0; i < hits.Length; i++) { Document doc = new Document(); doc = searcher.Doc(hits[i].Doc); ItemList item = new ItemList(); //item.ItemContent = doc.Get("contents"); item.ItemContent = ColorWord.addColor(doc.Get("contents"),termList); item.ItemPath = doc.Get("path"); lstResult.Add(item); } return lstResult; } }}最終的效果展示如下所示:
最終的代碼:下載