⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.cs

📁 只是中科院分词系统的SharpICTCLAS分词系统
💻 CS
字号:
/***********************************************************************************
 * ICTCLAS简介:计算所汉语词法分析系统ICTCLAS
 *              Institute of Computing Technology, Chinese Lexical Analysis System
 *              功能有:中文分词;词性标注;未登录词识别。
 *              分词正确率高达97.58%(973专家评测结果),
 *              未登录词识别召回率均高于90%,其中中国人名的识别召回率接近98%;
 *              处理速度为31.5Kbytes/s。
 * 著作权:  Copyright(c)2002-2005中科院计算所 职务著作权人:张华平
 * 遵循协议:自然语言处理开放资源许可证1.0
 * Email: zhanghp@software.ict.ac.cn
 * Homepage:www.i3s.ac.cn
 * 
 *----------------------------------------------------------------------------------
 * 
 * Copyright (c) 2000, 2001
 *     Institute of Computing Tech.
 *     Chinese Academy of Sciences
 *     All rights reserved.
 *
 * This file is the confidential and proprietary property of
 * Institute of Computing Tech. and the posession or use of this file requires
 * a written license from the author.
 * Author:   Kevin Zhang
 *          (zhanghp@software.ict.ac.cn)、
 * 
 *----------------------------------------------------------------------------------
 * 
 * SharpICTCLAS:.net平台下的ICTCLAS
 *               是由河北理工大学经管学院吕震宇根据Free版ICTCLAS改编而成,
 *               并对原有代码做了部分重写与调整
 * 
 * Email: zhenyulu@163.com
 * Blog: http://www.cnblogs.com/zhenyulu
 * 
 ***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace SharpICTCLAS
{
   public sealed class Utility
   {
      private Utility()
      {
      }

      #region GetPOSValue Method

      public static int GetPOSValue(string sPOS)
      {
         char[] c = sPOS.ToCharArray();

         if (c.Length == 1)
            return Convert.ToInt32(c[0]) * 256;
         else if (c.Length == 2)
            return Convert.ToInt32(c[0]) * 256 + Convert.ToInt32(c[1]);
         else
         {
            string s1 = sPOS.Substring(0, sPOS.IndexOf('+'));
            string s2 = sPOS.Substring(sPOS.IndexOf('+') + 1);
            return 100 * GetPOSValue(s1) + Int32.Parse(s2);
         }
      }

      #endregion

      #region GetPOSString Method

      public static string GetPOSString(int nPOS)
      {
         string sPOSRet;

         if (nPOS > Convert.ToInt32('a') * 25600)
         {
            if ((nPOS / 100) % 256 != 0)
               sPOSRet = string.Format("{0}{1}+{2}", Convert.ToChar(nPOS / 25600), Convert.ToChar((nPOS / 100) % 256), nPOS % 100);
            else
               sPOSRet = string.Format("{0}+{1}", Convert.ToChar(nPOS / 25600), nPOS % 100);
         }
         else
         {
            if (nPOS > 256)
               sPOSRet = string.Format("{0}{1}", Convert.ToChar(nPOS / 256), Convert.ToChar(nPOS % 256));
            else
               sPOSRet = string.Format("{0}", Convert.ToChar(nPOS % 256));
         }
         return sPOSRet;
      }

      #endregion

      //====================================================================
      // 根据汉字的两个字节返回对应的CC_ID
      //====================================================================
      public static int CC_ID(byte b1, byte b2)
      {
         return (Convert.ToInt32(b1) - 176) * 94 + (Convert.ToInt32(b2) - 161);
      }

      //====================================================================
      // 根据汉字返回对应的CC_ID
      //====================================================================
      public static int CC_ID(char c)
      {
         byte[] b = Encoding.GetEncoding("gb2312").GetBytes(c.ToString());
         if (b.Length != 2)
            return -1;
         else
            return (Convert.ToInt32(b[0]) - 176) * 94 + (Convert.ToInt32(b[1]) - 161);
      }

      //====================================================================
      // 根据CC_ID返回对应的汉字
      //====================================================================
      public static char CC_ID2Char(int cc_id)
      {
         if (cc_id < 0 || cc_id > Predefine.CC_NUM)
            return '\0';

         byte[] b = new byte[2];

         b[0] = CC_CHAR1(cc_id);
         b[1] = CC_CHAR2(cc_id);
         return (Encoding.GetEncoding("gb2312").GetChars(b))[0];
      }

      //====================================================================
      // 根据CC_ID返回对应汉字的第一个字节
      //====================================================================
      public static byte CC_CHAR1(int cc_id)
      {
         return Convert.ToByte(cc_id / 94 + 176);
      }

      //====================================================================
      // 根据CC_ID返回对应汉字的第二个字节
      //====================================================================
      public static byte CC_CHAR2(int cc_id)
      {
         return Convert.ToByte(cc_id % 94 + 161);
      }

      //====================================================================
      // 将字符串转换为字节数组(用于将汉字需要拆分成2字节)
      //====================================================================
      public static byte[] String2ByteArray(string s)
      {
         return Encoding.GetEncoding("gb2312").GetBytes(s);
      }

      //====================================================================
      // 将字节数组重新转换为字符串
      //====================================================================
      public static string ByteArray2String(byte[] byteArray)
      {
         return Encoding.GetEncoding("gb2312").GetString(byteArray);
      }

      //====================================================================
      // 获取字符串长度(一个汉字按2个长度算)
      //====================================================================
      public static int GetWordLength(string s)
      {
         return String2ByteArray(s).Length;
      }

      //====================================================================
      // Func Name  : charType
      // Description: Judge the type of sChar or (sChar,sChar+1)
      // Parameters : sFilename: the file name for the output CC List
      // Returns    : int : the type of char
      //====================================================================
      public static int charType(char c)
      {
         if (Convert.ToInt32(c) < 128)
         {
            string delimiters = " *!,.?()[]{}+=";
            //注释:原来程序为"\042!,.?()[]{}+=","\042"为10进制42好ASC字符,为*
            if (delimiters.IndexOf(c) >= 0)
               return Predefine.CT_DELIMITER;
            return Predefine.CT_SINGLE;
         }

         byte[] byteArray = Encoding.GetEncoding("gb2312").GetBytes(c.ToString());

         if (byteArray.Length != 2)
            return Predefine.CT_OTHER;

         int b1 = Convert.ToInt32(byteArray[0]);
         int b2 = Convert.ToInt32(byteArray[1]);

         return DoubleByteCharType(b1, b2);
      }

      private static int DoubleByteCharType(int b1, int b2)
      {
         //-------------------------------------------------------
         /*
            code  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
            A2A0     ⅰ ⅱ ⅲ ⅳ ⅴ ⅵ ⅶ ⅷ ⅸ ⅹ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -