📄 chineseparse.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ChineseWordUnit
{
//ChineseParse.cs //分词器
/**//// <summary>
/// 中文分词器。
/// </summary>
public class ChineseParse
{
private static ChineseWordsHashCountSet _countTable;
static ChineseParse()
{
_countTable = new ChineseWordsHashCountSet();
InitFromFile("ChineseDictionary.txt");
}
/**/
/// <summary>
/// 从指定的文件中初始化中文词语字典和字符串次数字典。
/// </summary>
/// <param name="fileName">文件名</param>
public static void InitFromFile(string fileName)
{
//string path = Directory.GetCurrentDirectory() + @"\ " + fileName;
string path = fileName;
if (File.Exists(path))
{
//using (StreamReader sr = new StreamReader(File.ReadAllText(path, Encoding.Default)))
using (StreamReader sr = new StreamReader(path, Encoding.Default))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
// ChineseWordUnit _tempUnit = InitUnit(s);
ChineseWordUnit _tempUnit = new ChineseWordUnit(s);
_countTable.InsertWord(_tempUnit.Word);
}
}
}
}
/**/
/// <summary>
/// 将一个字符串解析为ChineseWordUnit。
/// </summary>
/// <param name="s">字符串</param>
/// <returns>解析得到的ChineseWordUnit</returns>
private static ChineseWordUnit InitUnit(string s)
{
Regex reg = new Regex(@"\s+");
string[] temp = reg.Split(s);
if (temp.Length != 2)
{
throw new Exception("字符串解析错误:" + s);
}
return new ChineseWordUnit(temp[0], Int32.Parse(temp[1]));
}
/**/
/// <summary>
/// 分析输入的字符串,将其切割成一个个的词语。
/// </summary>
/// <param name="s">待切割的字符串</param>
/// <returns>所切割得到的中文词语数组</returns>
//public static string[] ParseChinese(string s)
//{
// int _length = s.Length;
// string _temp = String.Empty;
// ArrayList _words = new ArrayList();
// for (int i = 0; i < s.Length; )
// {
// _temp = s.Substring(i, 1);
// if (_countTable.GetCount(_temp) > 1)
// {
// int j = 2;
// for (; i + j < s.Length + 1 && _countTable.GetCount(s.Substring(i, j)) > 0; j++)
// {
// }
// _temp = s.Substring(i, j - 1);
// i = i + j - 2;
// i++;
// //_temp = "*";
// _words.Add(_temp);
// }
// else
// {
// i++;
// continue;
// }
// //string[] _tempStringArray = new string[_words.Count];
// //_words.CopyTo(_tempStringArray);
// //return _tempStringArray;
// }
// string[] _tempStringArray = new string[_words.Count];
// _words.CopyTo(_tempStringArray);
// return _tempStringArray;
//}
public static string ParseChinese(string s)
{
int _length = s.Length;
string _s = s;
string _temp = String.Empty;
string _FixedWord = String.Empty;
ArrayList _words = new ArrayList();
string Fillworld = "";
for (int i = 0; i < s.Length; )
{
_temp = s.Substring(i, 1);
if (_countTable.GetCount(_temp) >= 0)
{
int j = 2;
for (; i + j < s.Length + 1 && _countTable.GetCount(s.Substring(i, j)) >= 0; j++)
{
if (_countTable.GetCount(s.Substring(i, j)) == 1)
{
Fillworld = "";
_FixedWord = s.Substring(i, j);
for (int num = 0; num < j; num++)
{
Fillworld += "*";
}
_s = _s.Substring(0, i) + Fillworld + _s.Substring(i + j);
}
}
if (_countTable.GetCount(_temp) == 1)
{
_s = _s.Substring(0, i) + "*" + _s.Substring(i + 1);
}
//s = s.Substring(0, i) + Fillworld + s.Substring(i + j - 1);
//if (_temp == s.Substring(i, j - 1))
//{
// i = i + j - 2;
// i++;
// //_temp = "*";
// _words.Add(_temp);
//}
//else
//{
//}
//_temp = s.Substring(i, j - 1);
i = i + j - 2;
i++;
//_temp = "*";
_words.Add(_FixedWord);
_FixedWord = "";
}
else
{
i++;
continue;
}
//string[] _tempStringArray = new string[_words.Count];
//_words.CopyTo(_tempStringArray);
//return _tempStringArray;
}
string[] _tempStringArray = new string[_words.Count];
_words.CopyTo(_tempStringArray);
return _s;
//return _tempStringArray;
}
//public static string[] ParseChinese(string s)
//{
// string _s = "";
// int _length = s.Length;
// string _temp = String.Empty;
// ArrayList _words = new ArrayList();
// for (int i = 0; i < s.Length; )
// {
// _temp = s.Substring(i, 1);
// if (_countTable.GetCount(_temp) > 1)
// {
// int j = 2;
// for (; i + j < s.Length + 1 && _countTable.GetCount(s.Substring(i, j)) > 0; j++)
// {
// }
// if (_countTable.GetCount(s.Substring(i, j)) == -2 &&)
// {
// _temp = s.Substring(i, j);
// i = i + j - 2;
// i++;
// //_temp = "*";
// _words.Add(_temp);
// }
// //_temp = s.Substring(i, j - 1);
// i = i + j - 2;
// i++;
// ////_temp = "*";
// //_words.Add(_temp);
// }
// else
// {
// i++;
// continue;
// }
// //string[] _tempStringArray = new string[_words.Count];
// //_words.CopyTo(_tempStringArray);
// //return _tempStringArray;
// }
// string[] _tempStringArray = new string[_words.Count];
// _words.CopyTo(_tempStringArray);
// return _tempStringArray;
//}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -