最大匹配分词算法
2010-04-09 18:22
197 查看
代码
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2010/4/9
* Time: 15:06
* Directions:最大匹配分词算法
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Text;
using System.Collections;
namespace cc
{
class Program
{
public static void Main(string[] args)
{
string s1=@"那一刻我升起风马不为乞福只为守候你的到来那一天闭目在经殿香雾中蓦然听见你颂经中的真言那一月我摇动所有的经桶不为超度只为触摸你的指尖那一年磕长头匍匐在山路不为觐见只为贴着你的温暖那一世转山转水转佛塔啊不为修来生只为途中与你相见";
string s2="";
int maxlen=4;//分词长度
string w;
DateTime timeStart=DateTime.Now;//开始时间
StreamReader sr=new StreamReader(@"E:\works\ChineseDictionary.txt",Encoding.Default);
ArrayList arr=new ArrayList();//把中文词典保存在数组中
while(sr.Peek() > -1)
{
arr.Add(sr.ReadLine());
}
while(s1 != "")
{
if(s1.Length > maxlen)
{
w=s1.Substring(0,maxlen);
}//if
else
{
w=s1;
}
while (w.Length!=1)
{
if (arr.IndexOf(w) != -1)
{
break;
}
w=w.Substring(0,w.Length-1);
}//while
s2 = s2 + w + "/";
s1=s1.Substring(w.Length);
}//while
DateTime timeEnd=DateTime.Now;//结束时间
TimeSpan ts=timeEnd - timeStart;
Console.WriteLine(s2);
Console.WriteLine("分词花费的时间:{0}",ts.ToString());
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2010/4/9
* Time: 15:06
* Directions:最大匹配分词算法
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Text;
using System.Collections;
namespace cc
{
class Program
{
public static void Main(string[] args)
{
string s1=@"那一刻我升起风马不为乞福只为守候你的到来那一天闭目在经殿香雾中蓦然听见你颂经中的真言那一月我摇动所有的经桶不为超度只为触摸你的指尖那一年磕长头匍匐在山路不为觐见只为贴着你的温暖那一世转山转水转佛塔啊不为修来生只为途中与你相见";
string s2="";
int maxlen=4;//分词长度
string w;
DateTime timeStart=DateTime.Now;//开始时间
StreamReader sr=new StreamReader(@"E:\works\ChineseDictionary.txt",Encoding.Default);
ArrayList arr=new ArrayList();//把中文词典保存在数组中
while(sr.Peek() > -1)
{
arr.Add(sr.ReadLine());
}
while(s1 != "")
{
if(s1.Length > maxlen)
{
w=s1.Substring(0,maxlen);
}//if
else
{
w=s1;
}
while (w.Length!=1)
{
if (arr.IndexOf(w) != -1)
{
break;
}
w=w.Substring(0,w.Length-1);
}//while
s2 = s2 + w + "/";
s1=s1.Substring(w.Length);
}//while
DateTime timeEnd=DateTime.Now;//结束时间
TimeSpan ts=timeEnd - timeStart;
Console.WriteLine(s2);
Console.WriteLine("分词花费的时间:{0}",ts.ToString());
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
相关文章推荐
- Java实现的最大匹配分词算法详解
- 最大匹配分词算法
- 最大匹配算法: 分词入门级算法 C#实现
- python 实现机械分词(1)-正向最大匹配算法
- 最大匹配算法实现中文分词(讲解+代码)
- C#写中文基于词表的最大逆向匹配分词算法
- 试着写 搜索算法-- 最大逆向匹配分词算法
- 用正向和逆向最大匹配算法进行中文分词
- 中文分词的逆向最大匹配算法
- 搜索引擎--范例:中英文混杂分词算法的实现--正向最大匹配算法的原理和实现
- 中文分词算法之最大正向匹配算法(Python版)
- HMM最大匹配分词算法(Python)
- 中文分词常用算法之基于词典的双向最大匹配
- python中文分词教程之前向最大正向匹配算法详解
- 一个简单最大正向匹配(Maximum Matching)MM中文分词算法的实现
- 逆向最大匹配分词算法C#
- 中文分词算法之最大逆向匹配法
- 分词算法:正向最大匹配算法
- 简单的逆向最大匹配算法实现中文分词(Python)
- 。一个最大逆向匹配分词算法的例子