用C#语言进行分词。
大体思路:
①读入txt文本
<pre name="code" class="csharp"><pre name="code" class="csharp"> FileStream fs = new FileStream("E:\\data\\my_data\\yuanwen.txt", FileMode.Open);
StreamReader wholetext = new StreamReader(fs, System.Text.Encoding.Default);
// Console.WriteLine(wholetext.ReadLine());//C#读入中文txt文件
string txt = wholetext.ReadLine();
Console.WriteLine(txt);
char[] words = txt.ToCharArray();//将整篇文章打散,数组的每一个成员存入一个字
int wholelen = words.Length;//数组的总长度
Console.WriteLine(words.Length);//求整篇文章存入数组之后的长度,一共有227个字,但数组长度为231,貌似空格也被计入数组?!!?
Console.WriteLine(words[7]);//用来测试数组的输出,输出的是“在”,但是“在”实际上是第四个数
②连入数据库,数据库用的是access
//以下要连入数据库
string mystr, mysql;//最后是否要写入一个txt文件,先假定不写
OleDbConnection myconn = new OleDbConnection();
OleDbCommand mycmd = new OleDbCommand();
mystr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=words.mdb";
myconn.ConnectionString = mystr;
myconn.Open();
string cutword = null;//预备用来放入待切的数组元素,并将其转化成字符串
mycmd.Connection = myconn;
if (myconn.State == ConnectionState.Open)
Console.WriteLine("已成功连入数据库");
else
Console.WriteLine("未成功连入数据库");
③下面进行分词
举个例子:
如果不是你,我不会相信,朋友比情人还死心塌地。