示例#1
0
        //public static string[] SplitWords(string content)
        //{
        //    List<string> strList = new List<string>();
        //    Analyzer analyzer = new PanGuAnalyzer();//指定使用盘古 PanGuAnalyzer 分词算法
        //    TokenStream tokenStream = analyzer.TokenStream("", new StringReader(content));

        //    Lucene.Net.Analysis.Token token = null;
        //    while ((token = tokenStream.Next()) != null)
        //    { //Next继续分词 直至返回null
        //        strList.Add(token.TermText()); //得到分词后结果
        //    }
        //    return strList.ToArray();
        //}

        #region 分词测试
        /// <summary>
        /// 分词测试
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public string Token(string keyword)
        {
            string ret = "";
            System.IO.StringReader reader = new System.IO.StringReader(keyword);
            Analyzer analyzer = new PanGuAnalyzer();//指定使用盘古 PanGuAnalyzer 分词算法
            TokenStream ts = analyzer.TokenStream(keyword, reader);
            bool hasNext = ts.IncrementToken();
            Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
            while (hasNext)
            {
                ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
                ret += ita.Term + "|";
                hasNext = ts.IncrementToken();
            }
            ts.CloneAttributes();
            reader.Close();
            analyzer.Close();
            return ret;
        }
示例#2
0
        /// <summary>
        /// 生成LUCENE索引数据
        /// </summary>
        /// <param name="indexDocList">索引数据文档列表</param>
        /// <param name="directoryPath">索引文件路径</param>
        /// <param name="callback">回调方法</param>
        public static void MakeIndex(List<Document> indexDocList, string directoryPath, Action<Document> callback)
        {
            try
            {
                PanGuAnalyzer analyzer = new PanGuAnalyzer(true);
                string textIndexDir = directoryPath;
                if (!System.IO.Directory.Exists(textIndexDir))
                {
                    System.IO.Directory.CreateDirectory(textIndexDir);
                }

                Lucene.Net.Store.Directory indexDirectory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(textIndexDir), new NativeFSLockFactory());
                if (IndexReader.IndexExists(indexDirectory))
                {
                    if (IndexWriter.IsLocked(indexDirectory))
                    {
                        IndexWriter.Unlock(indexDirectory);
                    }
                }
                IndexWriter indexWriter = new IndexWriter(indexDirectory, analyzer, true, Lucene.Net.Index.IndexWriter.MaxFieldLength.LIMITED);
                if (indexDocList != null && indexDocList.Count > 0)
                {
                    foreach (var item in indexDocList)
                    {
                        indexWriter.AddDocument(item, analyzer);

                        if (callback != null)
                        {
                            callback(item);
                        }
                    }
                }

                indexWriter.Optimize();
                indexWriter.Close();

                analyzer.Close();
            }
            catch (Exception ex)
            {
                LogHelper.Info(typeof(LuceneManager), ex.ToString());

            }
        }