您好,登錄后才能下訂單哦!
//正向最大匹配分詞算法 ,耗時(shí)長(zhǎng),這并不是一個(gè)很好的算法,我的這個(gè)輸出是逆向輸入的 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ClusterCharater { public class SplitChineseCharacter { private String[] dictionary = { "今天", "是","星期" ,"六","星期六" }; //詞典 private String input = null; public List<String> Reslut = new List<string>(); public SplitChineseCharacter(String input) { this.input = input; } public void start() { String temp = null; for (int i = 0; i < this.input.Length; i++) { temp = this.input.Substring(i); // 每次從字符串的首部截取一個(gè)字,并存到temp中 // 如果該詞在字典中, 則刪除該詞并在原始字符串中截取該詞 if (this.isInDictionary(temp)) { Reslut.Add(temp); this.input = this.input.Replace(temp, ""); i = -1; // i=-1是因?yàn)橐匦虏檎遥?nbsp;而要先執(zhí)行循環(huán)中的i++ } } // 當(dāng)前循環(huán)完畢,詞的末尾截去一個(gè)字,繼續(xù)循環(huán), 直到詞變?yōu)榭? if (null != this.input && !"".Equals(this.input)) { this.input = this.input.Substring(0, this.input.Length - 1); this.start(); } } //判斷當(dāng)前詞是否在字典中 public Boolean isInDictionary(String temp) { for (int i = 0; i < this.dictionary.Length; i++) { if (temp.Equals(this.dictionary[i])) { return true; } } return false; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ClusterCharater { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
String s=inputtext.Text.Trim();
SplitChineseCharacter scc = new SplitChineseCharacter(s);
scc.start();
foreach (String ss in scc.Reslut)
{
output.Text += ss+"/";
}
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。