Lucene.Net的语言处理包中Lucene.Net.Analysis.Cn的Bug

    最近在对博客园的搜索程序进行改进。博客园的搜索功能用的是Lucene.net搜索引擎,当时博客园增加搜索功能时,Lucene.net还不支持中文分词, 后来得到http://www.cnblogs.com/yuhen/的帮助才解决这个问题。(最近博客园的搜索程序出了问题, 暂时改用google)。
    现在,Lucene.net中支持分词功能, 我下载了语言处理包(Lucene.Net NLS Pack),用其中的Lucene.Net.Analysis.Cn.ChineseAnalyzer建立索引,可是每次建立索引就出现死锁现象。看了sf.net上的这篇文章CJK Analysis maybe cause dead lock,我想到可能是Lucene.Net.Analysis.Cn中代码有Bug,查看了Lucene.Net.Analysis.Cn中的代码,果然有问题,在ChineseTokenizer的第148行,将dataLen == -1改为dataLen == 0, 死锁的问题就解决了。
    后来, 发现这篇文章http://ms.mblogger.cn/yysun/posts/6092.aspx中也讲到了Lucene.Net.Analysis.Cn中的Bug。
    我将两个bug改了一下,放在博客园上给需要者下载。虽然只改了两行代码,但我想改好了放在这,对一些初次使用者还是有点帮助的。
    Bin: http://files.cnblogs.com/dudu/Lucene.Net.Analysis.Cn.rar
    Src: http://files.cnblogs.com/dudu/Lucene.Net.NLS.rar
posted @ 2004-06-22 12:59 dudu 阅读(5086) 评论(19)  编辑 收藏 网摘 所属分类: 博客园文章

  回复  引用  查看    
#1楼 2004-06-22 14:05 | 3188.NET      
搜索好像还是不能用吧?
  回复  引用  查看    
#2楼 2004-06-22 14:20 | dudu      
搜索功能正在改进之中, 还没完成, 请稍候。
  回复  引用  查看    
#3楼 2004-06-22 20:10 | unruledboy(灵感之源)      
继续努力!
  回复  引用    
#4楼 2004-07-30 14:16 | 仪表 [未注册用户]
using System;
using System.IO;
using Lucene.Net.Index;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Analysis.Cn;

namespace CreateIndex
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
DateTime start = DateTime.Now;
IndexWriter writer = new IndexWriter("index", new ChineseAnalyzer(), true);
indexDocs(writer, new DirectoryInfo(@"E:\18show"));

writer.Optimize();
writer.Close();

DateTime end = DateTime.Now;

Console.Write(end.Millisecond - start.Millisecond);
Console.WriteLine(" total milliseconds");
Console.ReadLine();
}

public static void indexDocs(IndexWriter writer, DirectoryInfo m_DirectoryInfo)
{
foreach(FileInfo m_OneFileInfo in m_DirectoryInfo.GetFiles())
{
writer.AddDocument(FileDocument.Document(m_OneFileInfo));
}
}
}

public class FileDocument 
{
public static Document Document(FileInfo f)
{
Document doc = new Document();
doc.Add(Field.Text("path", f.FullName));
doc.Add(Field.Keyword("modified",f.LastWriteTime.ToString()));

TextReader m_TextReader=File.OpenText(f.FullName);

doc.Add(Field.Text("contents", m_TextReader));

return doc;
}
}
}

  回复  引用    
#5楼 2004-07-30 14:19 | 仪表 [未注册用户]
using System;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.Cn;

namespace TestQuery
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Searcher searcher = new IndexSearcher(@"index的路径");
Analyzer analyzer = new ChineseAnalyzer();

while (true) 
{
Console.WriteLine("Query: ");
String line = Console.ReadLine();

if (line.Length == -1)
break;

Query query = QueryParser.Parse(line, "contents", analyzer);
Console.WriteLine("Searching for: " + query.ToString("contents"));

Hits hits = searcher.Search(query);
Console.WriteLine(hits.Length() + " total matching documents");

const int HITS_PER_PAGE = 10;
for (int start = 0; start < hits.Length(); start += HITS_PER_PAGE)
{
int end = Math.Min(hits.Length(), start + HITS_PER_PAGE);
for (int i = start; i < end; i++) 
{
Document doc = hits.Doc(i);
String path = doc.Get("path");
if (path != null) 
{
Console.WriteLine(i + ". " + path);

else 
{
String url = doc.Get("url");
if (url != null) 
{
Console.WriteLine(i + ". " + url);
Console.WriteLine("   - " + doc.Get("title"));

else 
{
Console.WriteLine(i + ". " + "No path nor URL for this document");
}
}
}

if (hits.Length() > end) 
{
Console.WriteLine("more (y/n) ? ");
line = Console.ReadLine();
if (line.Length == 0 || line.ToCharArray()[0] == 'n')
break;
}
}
}
searcher.Close();
}
}
}

  回复  引用    
#6楼 2004-08-02 23:24 | 4111Y80Y [未注册用户]
原来是编码问题,抱歉打搅您了。
  回复  引用    
#7楼 2004-09-29 14:32 | study [未注册用户]
如何修改上面错误?编码问题如何修改
  回复  引用    
#8楼 2005-07-12 15:30 | 无名 [未注册用户]
有没有人安装过luncene.net?

怎么安装呀?
  回复  引用    
#9楼 2006-05-14 16:18 | RoyYan [未注册用户]
帖易(search.teein.com)-中文社区搜索引擎招聘lucene相关开发人员,诚邀熟悉dotlucene结构,对分布式搜索引擎有一定研究的TX加盟。

联系MSN:haoyan#teein.com (替换#成@)
工作地点在上海,要求全职。
  回复  引用  查看    
#10楼 2006-05-31 21:04 | 徐灿钊Asp.net专栏      
嘻嘻,dudu又帮我解决了大问题。
  回复  引用  查看    
#11楼 2006-08-11 14:45 | 有容乃大      
dudu,能不能给个用Lucene.Net搜索sql server数据的例子啊,我搜遍整个网站,也没有找到,只是看到一些搜索本地文件的例子,郁闷啊。
  回复  引用  查看    
#12楼 [楼主]2006-08-11 15:27 | dudu      
@有容乃大
你可以下载CNBlogs Dottext 1.0 Beta 2 看看,下载地址:http://downloads.cnblogs.com/Dottext/CNBlogsDottext10Beta2.rar 。
  回复  引用  查看    
#13楼 2007-03-30 11:12 | 没剑      
请问这个中文分词怎么不支持数字?也就是我要用电话号码来搜索,结果搜索不到...
  回复  引用    
#14楼 2007-08-01 15:38 | Batista [未注册用户]
为什么我引用不到Lucene.Net.Search.HighLight???
  回复  引用    
#15楼 2007-08-01 17:42 | Batista [未注册用户]
上面的类库找到了~
请问Lucene.Net.Analysis.Cn用这个做分析器建立的索引,只支持中文的搜索吗?其他的英文或数字都不支持吗?
  回复  引用    
#16楼 2007-11-25 23:47 | king2003 [未注册用户]
解析器有问题换一个应可以了
  回复  引用    
#17楼 2008-01-11 10:57 | chinabbcsfasdf [未注册用户]
我的也是,测试了一下,

其他的英文或数字都不支持




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-08-11 16:11 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

相关文章:

相关链接: