ICTCLAS 平台调用的封装

ICTCLAS30 已经给了开放函数的头文件,可以直接从C#调用

[StructLayout(LayoutKind.Explicit)]
public struct result_t
{
    [FieldOffset(
0)]
    
public int start;
    [FieldOffset(
4)]
    
public int length;
    [FieldOffset(
8)]
    
public int sPos;
    [FieldOffset(
12)]
    
public int sPosLow;
    [FieldOffset(
16)]
    
public int POS_id;
    [FieldOffset(
20)]
    
public int word_ID;
    [FieldOffset(
24)]
    
public int word_type;
    [FieldOffset(
28)]
    
public int weight;

}

/// <summary>
/// Description of ICTCLAS.
/// </summary>
public class ICTCLAS30
{

    
const string path = @"ICTCLAS30.dll";

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_Init")]
    
public static extern bool Init(String sInitDirPath);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_ParagraphProcess")]
    
public static extern String ParagraphProcess(String sParagraph, int bPOStagged);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_Exit")]
    
public static extern bool Exit();

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_ImportUserDict")]
    
public static extern int ImportUserDict(String sFilename);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_FileProcess")]
    
public static extern bool FileProcess(String sSrcFilename, String sDestFilename, int bPOStagged);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_FileProcessEx")]
    
public static extern bool FileProcessEx(String sSrcFilename, String sDestFilename);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_GetParagraphProcessAWordCount")]
    
public static extern int GetParagraphProcessAWordCount(String sParagraph);
    
//ICTCLAS_GetParagraphProcessAWordCount
    [DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "ICTCLAS_ParagraphProcessAW")]
    
public static extern void ParagraphProcessAW(int nCount, [Out, MarshalAs(UnmanagedType.LPArray)] result_t[] result);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_AddUserWord")]
    
public static extern int AddUserWord(String sWord);

    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_SaveTheUsrDic")]
    
public static extern int SaveTheUsrDic();


    [DllImport(path, CharSet 
= CharSet.Ansi, EntryPoint = "ICTCLAS_DelUsrWord")]
    
static extern int DelUsrWord(String sWord);

    
public ICTCLAS30()
    {

    }
}

使用的时候把ICTCLAS30.dll,Configure.xml和Data文件夹copy到程序exe运行的位置,否则需要制定他们的位置

使用实例的代码片段

ICTCLAS30.Init(null);//Configure.xml所在的目录,为null表示当前目录

string resultString = ICTCLAS.ParagraphProcess(strInput,0);//分词

int count = ICTCLAS.GetParagraphProcessAWordCount(strInput);
result_t[] rt 
=new result_t[count];
ICTCLAS.ParagraphProcessAW(count, rt);
//获得分析信息
原文地址:https://www.cnblogs.com/format/p/1597331.html