请教“尝试读取或写入受保护的内存”,调用海量分词研究版组件
/// <summary>/// 初始化海量分词系统,加载分词用数据。
/// </summary>
/// <param name="lpszDataFilePath">
/// 分词数据文件路径(不包括文件名)。当lpszDataFilePath为NULL时,
/// 先搜索内存中是否存在分词数据字典,若存在不再加载;若不存在,加载DLL目录下的字典文件。
/// </param>
/// <returns></returns>
[DllImport("HLSSplit.dll", EntryPoint = "HLSplitInit")]
public static extern bool HLSplitInit(string lpszDataFilePath);
/// <summary>
/// 卸载海量自动中文分词系统,释放分词系统所占资源。
/// </summary>
[DllImport("HLSSplit.dll", EntryPoint = "HLFreeSplit")]
public static extern void HLFreeSplit();
/// <summary>
/// 创建自动中文分词结果句柄
/// </summary>
/// <returns></returns>
[DllImport("HLSSplit.dll", EntryPoint = "HLOpenSplit")]
public static extern uint HLOpenSplit();
/// <summary>
/// 关闭分词结果句柄, 释放分词结果所占资源
/// </summary>
/// <param name="hHandle">创建的分词结果句柄</param>
/// <returns></returns>
[DllImport("HLSSplit.dll", EntryPoint = "HLCloseSplit")]
public static extern void HLCloseSplit(uint hHandle);
[DllImport("HLSSplit.dll", EntryPoint = "HLGetFileKeyCnt")]
public static extern int HLGetFileKeyCnt(uint hHandle);
[DllImport("HLSSplit.dll", EntryPoint = "HLGetFileKeyAt")]
private static extern IntPtr GetFileKeyAt(uint hHandle, int iIndex);
public static Seg HLGetFileKeyAt(uint hHandle, int iIndex)
{
IntPtr pWord = GetFileKeyAt(hHandle, iIndex);
SegTran seg = (SegTran)Marshal.PtrToStructure(pWord, typeof(SegTran));
//Seg word = (Seg)Marshal.PtrToStructure(pWord, typeof(Seg));
Seg word = new Seg();
word.s_szWord = Marshal.PtrToStringAnsi(seg.s_szWord);
word.weight = seg.weight;
//此处需要把高位屏蔽,否则得到的结果会出现错误。所有的词性值在0x4000000000下
word.POS = (POS)((uint)seg.POS & 0x0FFFFFFF);
return word;
}
/// <summary>
/// 对指定字符串进行分词。
/// </summary>
/// <param name="hHandle">分词结果句柄</param>
/// <param name="_instr">要分词的字符串</param>
/// <param name="iFlag">附加计算标志,默认为0</param>
/// <returns>返回成功标志。成功返回true,否则返回false。</returns>
[DllImport("HLSSplit.dll", EntryPoint = "HLSplitWord")]
public static extern bool HLSplitWord(uint hHandle, string _instr, int iFlag);
/// <summary>
/// 对指定字符串进行分词。
/// </summary>
/// <param name="hHandle">分词结果句柄</param>
/// <param name="_instr">要分词的字符串</param>
/// <returns>返回成功标志。成功返回true,否则返回false。</returns>
public static bool HLSplitWord(uint hHandle, string txt)
{
return HLSplitWord(hHandle, txt, 0);
}
/// <summary>
/// 对指定字符串进行分词。
/// </summary>
/// <param name="hHandle">分词结果句柄</param>
/// <param name="_instr">要分词的字符串</param>
/// <param name="iFlag">附加计算标志,默认为0</param>
/// <returns>返回成功标志。成功返回true,否则返回false。</returns>
public static bool HLSplitWord(uint hHandle, string txt, SegOption option)
{
return HLSplitWord(hHandle, txt, (int)option);
}
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]
public struct SegTran
{
/// <summary>
/// 字符串
/// </summary>
public IntPtr s_szWord;// = new IntPtr()
/// <summary>
/// 词性标志
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public POS POS;
/// <summary>
/// 关键词权重,如果不是关键词,权重为0
/// </summary>
public float weight;
};
[StructLayout(LayoutKind.Sequential)]
public struct Seg
{
/// <summary>
/// 字符串
/// </summary>
public string s_szWord;
/// <summary>
/// 词性标志
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public POS POS;
/// <summary>
/// 关键词权重,如果不是关键词,权重为0
/// </summary>
public float weight;
};
//下面是调用部分。
public List<Seg> SplitKey(string strinput)
{
List<Seg> r = new List<Seg>();
if (string.IsNullOrEmpty(strinput))
return r;
uint hHandle = HLSplit.HLOpenSplit();//这儿有值
bool flag = false;
int iexflag = (int)SegOption.SEARCH | (int)SegOption.KEYWORD;//(int)SegOption.POS |
try
{
flag = HLSplit.HLSplitWord(hHandle, strinput, iexflag);//这儿出错
if (flag)
{
int nResultCnt = HLSplit.HLGetFileKeyCnt(hHandle);
for (int i = 0; i < nResultCnt; i++)
{
Seg tmp = HLSplit.HLGetFileKeyAt(hHandle, i);
//tmp = (WordSplitWarp.SHLSegWord)System.Runtime.InteropServices.Marshal.PtrToStructure(HLGetFileKeyAt(hHandle, i), typeof(WordSplitWarp.SHLSegWord));
r.Add(tmp);
}
}
}
catch (Exception e)
{
log.Error(string.Format("输入字符串为:{0}\r\n出错信息为:{1}", strinput, e));
}
finally {
HLSplit.HLCloseSplit(hHandle);
}
请各位大虾帮忙看一下。困扰了我N久,在网上找的资料都不管用。 --------------------编程问答-------------------- 海量分词系统
赞~~~
关注中 --------------------编程问答-------------------- 看不明白 --------------------编程问答-------------------- up up up
补充:.NET技术 , C#