盘古分词平台兼容性

在使用Lucece.net,需要一个中文的分词组件,比较好的是盘古分词,但是我希望能够在mono的环境下运行,就使用moma检查了一下盘古分词

Assembly Version Missing Not Implemented Todo P/Invoke

PanGu.dll 2.3.1.0 3 0 5 0

Calling Method Method Missing from Mono

WordInfo> PreSegment (string) string Strings.StrConv (string, VbStrConv, int)

WordInfo> PreSegment (string) string Strings.StrConv (string, VbStrConv, int)

WordInfo> PreSegment (string) string Strings.StrConv (string, VbStrConv, int)

Calling Method Method with [MonoTodo] Reason

string ReadFileToString (string, Encoding) int Marshal.GetHRForException (Exception) SetErrorInfo

MemoryStream ReadFileToStream (string) int Marshal.GetHRForException (Exception) SetErrorInfo

void WriteStream (string, MemoryStream) int Marshal.GetHRForException (Exception) SetErrorInfo

void WriteLine (string, string) int Marshal.GetHRForException (Exception) SetErrorInfo

void WriteString (string, string, Encoding) int Marshal.GetHRForException (Exception) SetErrorInfo

  1. 是StrConv这个函数没有检查通过,发现作者使用了 Microsoft.VisualBasic.Strings.StrConv 这个类库来处理简体和繁体转换的问题,这个类用到了windows平台的特性,所以没有编译通过,于是写了个简单的简体繁体转换的类库:简体中文与繁体相互转换.

  2. 是一个Marshal.GetHRForException 用来做io处理出错的时候判断的,代码如下大概是出现了ERR_PROCESS_CANNOT_ACCESS_FILE错误时让线程稍等。代码如下

    uint hResult = (uint)System.Runtime.InteropServices.Marshal.GetHRForException(e);

    if (hResult == ERR_PROCESS_CANNOT_ACCESS_FILE)

    {

    if (times > 10)

    {

    //Maybe another program has some trouble with file

    //We must exit now

    throw e;

    }

     System.Threading.Thread.Sleep(200);
    times++;

    }

    else

    {

    throw e;

    }

查找MSDN在Marshal,如何:映射 HRESULT 和异常 里面有一句话 :

COM 方法通过返回 HRESULT 来报告错误;.NET 方法则通过引发异常来报告错误。 运行时将处理这两者之间的转换。 .NET Framework 中的每个异常类都会映射到一个 HRESULT。

也就是说HRESULT是给com来报告错误的,.net本身通过引发异常来处理错误,而这个IOException对应出来的hresult是

文件“xxxx”正由另一进程使用,因此该进程无法访问此文件。

但是引起这个错误的异常根源就是IOException,内部有个私有的HResult无法访问。只能修改为所有的错都等待10次,10次之后抛出异常,所以把外面的if之外的代码都注释掉变成

if (times > 10)
{
//Maybe another program has some trouble with file
//We must exit now
throw e;
} System.Threading.Thread.Sleep(200);
times++;

这样两个不兼容的问题就解决了。

盘古分词Lucene3.03支持

现在盘古分词给出的Lucene的示例是2.x的版本,而我希望能够用3.0的版本来集成,其中的区别是PanGuTokenizer.cs里面集成的时候会提示Next()方法没有找到合适的类来重写。原因是lucene中next()方法已改成incrementToken()方法.lucene的文档:

This method is called for every token of a document, so an efficient implementation is crucial for good performance. To avoid calls to AttributeSource.addAttribute(Class) and AttributeSource.getAttribute(Class), references to all AttributeImpls that this stream uses should be retrieved during instantiation.

所以只需要修改这个方法,改为使用addAttribute来实现:

这里有一个已经修改好了的pangu分词的版本https://github.com/JimLiu/Lucene.Net.Analysis.PanGu .主要代码如下,其中next还是原来的方法,去掉override.

添加属性

private ITermAttribute termAtt;
private IOffsetAttribute offsetAtt;
private IPositionIncrementAttribute posIncrAtt;
private ITypeAttribute typeAtt; termAtt = AddAttribute<ITermAttribute>();
offsetAtt = AddAttribute<IOffsetAttribute>();
posIncrAtt = AddAttribute<IPositionIncrementAttribute>();
typeAtt = AddAttribute<ITypeAttribute>();

方法实现

public override bool IncrementToken()
{
ClearAttributes();
Token word = Next();
if (word != null)
{
termAtt.SetTermBuffer(word.Term);
offsetAtt.SetOffset(word.StartOffset, word.EndOffset);
typeAtt.Type = word.Type;
return true;
}
End();
return false;
}

以上就是让lucene.net在mono下使用盘古分词出现的一些问题。

盘古分词修改支持mono和lucene.net3.03的更多相关文章

  1. lucene.net helper类 【结合盘古分词进行搜索的小例子(分页功能)】

      转自:http://blog.csdn.net/pukuimin1226/article/details/17558247 添加:2013-12-25 更新:2013-12-26 新增分页功能. ...

  2. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)

    lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能)   添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...

  3. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)

    转自:http://blog.csdn.net/pukuimin1226/article/details/17558247 添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新 ...

  4. Lucene.Net+盘古分词->开发自己的搜索引擎

    //封装类 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Lucene. ...

  5. 让盘古分词支持最新的Lucene.Net 3.0.3

    原文:让盘古分词支持最新的Lucene.Net 3.0.3 好多年没升级过的Lucene.Net最近居然升级了,到了3.0.3后接口发生了很大变化,原来好多分词库都不能用了,所以上次我把MMSeg给修 ...

  6. 支持Mono的盘古分词(PanGu)

    不废话,直接上下载地址http://files.cnblogs.com/files/RainbowInTheSky/PanGu.rar 群友在做项目移植到Mono+jexus上时遇到了问题,盘古分词无 ...

  7. Lucene.Net3.0.3+盘古分词器学习使用

    一.Lucene.Net介绍 Lucene.net是Lucene的.net移植版本,是一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索 ...

  8. Lucene.Net+盘古分词

    前言 各位朋友,谢谢大家的支持,由于文件过大,有考虑到版权的问题,故没有提供下载,本人已建立一个搜索技术交流群:77570783,源代码已上传至群共享,需要的朋友,请自行下载! 首先自问自答几个问题, ...

  9. lucene+盘古分词

    一般的网站都会有都会有搜索的功能,一般实现搜索主要有三种方案 第一种是最差的,也是最不推荐的,使用数据库的模糊查询例如select * form table where 字段 like XXX,这种查 ...

随机推荐

  1. Mac OS X 下安装MySQL 5.7

    下载安装包 官网下载安装包 选择相应的版本和格式,有 .dmg 和压缩包两种. 这里选择简单直接的 .dmg安装包,下载的时候可以将下载地址直接贴到迅雷,速度比较快. 安装 安装很简单,直接双击下好的 ...

  2. VCL 中的 Windows API 函数(1): AbortDoc

    AbortDoc: 该函数终止当前打印作业并删除最好一次调用 StartDoc 函数写入的所有信息. 该函数在 Printers 单元的应用:AbortDoc(Canvas.Handle);

  3. vue学习起步,vue环境安装

    vue安装的前提是安装了nodejs 安装淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 安装webpack c ...

  4. 6 Django系列之关于models的sql语句日常用法总结

    preface Django提供了强大的ORM,我们可以通过ORM快速的写出我们想要对数据做什么样操作的代码.下面就说说我在日常工作中的用法: 外键关联精确查询 应用场景:表A host字段关联到了表 ...

  5. Linux的时间设置与同步

    http://www.cnblogs.com/liuyou/archive/2012/07/29/2614338.html

  6. springboot+elasticsearch配置实现

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. c++浅拷贝和深拷贝---14

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 1.什么是拷贝构造函数: 拷贝构造函数,又称复制构造函数,是一种特殊的构造函数,它由编译器调用来 ...

  8. JQuery Mobile 简单入门引导

    看了慕课网的jqm视频(http://www.imooc.com/learn/207),觉的不错,简单截几个图,做一下备忘:

  9. 一段代码让DedeCMS完美兼容PHP5.4

    DedeCMS V5.7版本,在本地部署后,正确登录后台的情况下页面没有任何输出和显示(错误登录或密码错误时才有显示),也没有报错.进到脚本调试,发现问题出在userLogin类所在的脚本userlo ...

  10. atom中vue高亮支持emmet语法

    vue高亮插件: language-vue 支持emmet语法: 文件>用户键盘映射>keymap.cson添加: 'atom-text-editor[data-grammar~=&quo ...