熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#調用ICTCLAS30

2022-06-13   來源: .NET編程 

  SharpICTCLAS雖說是開源的年以後就沒有人再進行維護跑搜狗的語料問題不少就連C#版本的作者也承認有不少問題

  想得到更為准確的分詞結果還是研究SharpICTCLAS也就是ICTCLAS dll 文件不是C#開發的所以引入要通過DllImport

  先自己寫了個類

  代碼

  using System;

  using SystemCollectionsGeneric;

  using SystemText;

  using SystemRuntimeInteropServices;

  namespace test

  {

  [StructLayout(LayoutKindExplicit)]

  public struct result_t

  {

  [FieldOffset()]

  public int start;

  [FieldOffset()]

  public int length;

  [FieldOffset()]

  public int sPos;

  [FieldOffset()]

  public int sPosLow;

  [FieldOffset()]

  public int POS_id;

  [FieldOffset()]

  public int word_ID;

  [FieldOffset()]

  public int word_type;

  [FieldOffset()]

  public int weight;

  }

  class ICTCLAS

  {

  const string path = @ICTCLASdll;

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_Init)]

  public static extern bool Init(String sInitDirPath);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_ParagraphProcess)]

  public static extern String ParagraphProcess(String sParagraph int bPOStagged);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_Exit)]

  public static extern bool Exit();

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_ImportUserDict)]

  public static extern int ImportUserDict(String sFilename);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_FileProcess)]

  public static extern bool FileProcess(String sSrcFilename String sDestFilename int bPOStagged);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_FileProcessEx)]

  public static extern bool FileProcessEx(String sSrcFilename String sDestFilename);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_GetParagraphProcessAWordCount)]

  public static extern int GetParagraphProcessAWordCount(String sParagraph);

  //ICTCLAS_GetParagraphProcessAWordCount

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_ParagraphProcessAW)]

  public static extern void ParagraphProcessAW(int nCount [Out MarshalAs(UnmanagedTypeLPArray)] result_t[] result);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_AddUserWord)]

  public static extern int AddUserWord(String sWord);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_SaveTheUsrDic)]

  public static extern int SaveTheUsrDic();

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_DelUsrWord)]

  static extern int DelUsrWord(String sWord);

  public ICTCLAS()

  {

  }

  }

  }

  調用

  代碼

  if (!ICTCLASInit(null))

  {

  SystemConsoleWriteLine(Init ICTCLAS failed!);

  return;

  }

  SystemConsoleWriteLine(Init ICTCLAS Success!);

  String pResult;

  pResult = ICTCLASParagraphProcess(點擊下載超女紀敏佳深受觀眾喜愛禽流感爆發在非典之後 );

  SystemConsoleWriteLine(pResult);

  ICTCLASExit();

  注

  使用的時候把ICTCLASdllConfigurexml和Data文件夾copy到程序exe運行的位置否則需要制定他們的位置

  如何把pResult搞成昨天博文裡的wordResult格式還是個問題還需要好好研究

  附上官方網站的C#調用示例

  代碼

  using System;

  using SystemIO;

  using SystemRuntimeInteropServices;

  namespace win_csharp

  {

  [StructLayout(LayoutKindExplicit)]

  public struct result_t

  {

  [FieldOffset()]

  public int start;

  [FieldOffset()]

  public int length;

  [FieldOffset()]

  public int POS_id;

  [FieldOffset()]

  public int word_ID;

  }

  /// <summary>

  /// Class 的摘要說明

  /// </summary>

  class Class

  {

  const string path = @ICTCLASdll;

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_Init)]

  public static extern bool ICTCLAS_Init(String sInitDirPath);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_ParagraphProcess)]

  public static extern String ICTCLAS_ParagraphProcess(String sParagraph int bPOStagged);

  [DllImport(path CharSet = CharSetAnsi EntryPoint = ICTCLAS_Exit)]

  public static extern bool ICTCLAS_Exit();

  /// <summary>

  /// 應用程序的主入口點

  /// </summary>

  [STAThread]

  static void Main(string[] args)

  {

  //

  // TODO: 在此處添加代碼以啟動應用程序

  //

  if (!ICTCLAS_Init(null))

  {

  SystemConsoleWriteLine(Init ICTCLAS failed!);

  return;

  }

  SystemConsoleWriteLine(Init ICTCLAS Success!);

  String pResult;

  pResult = ICTCLAS_ParagraphProcess(點擊下載超女紀敏佳深受觀眾喜愛禽流感爆發在非典之後 );

  SystemConsoleWriteLine(pResult);

  ICTCLAS_Exit();

  }

  }

  }

  參考 format Blog  

  ICTCLAS 官方文檔


From:http://tw.wingwit.com/Article/program/net/201311/12707.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.