逆向最大匹配分词算法C#
生活随笔
收集整理的這篇文章主要介紹了
逆向最大匹配分词算法C#
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
逆向順序
句子:大家好我叫XX我是一名程序員
程序員 -> 序員 -> 員
名程序 -> 程序 -> 序
一名程 -> 名程 -> 程
是一名 -> 一名 -> 名
我是一 -> 是一 -> 一
X我是 -> 我是 -> 是
XX我 ?-> X我 -> 我
叫XX ?-> XX -> X
我叫X -> 叫X -> X
好我叫 -> 我叫 -> 叫
家好我 -> 好我 -> 我
大家好 -> 家好 -> 好
大家 ? -> 家
大
?
class Program{public static HashSet<string> dictionary = new HashSet<string>();static void Main(string[] args){Initail();List<string> list = new List<string>();string s = "大家好我叫XX我是一名程序員";string[] sentences = s.Split(',');int max = 3;for (int i = 0; i < sentences.Length; i++){string str = sentences[i];int start = sentences[i].Length - max;int len = sentences[i].Length - start;while (len > 0){string subWord = sentences[i].Substring((start < 0 ? 0 : start), len);Console.WriteLine(subWord);if (Search(subWord)){list.Add(subWord);start = start - max;if (start < 0){len = start < 0 ? max + start : max;}}else{int k = 1;bool flag = false;string tempWord = null;for (; k <= subWord.Length - 1; k++){tempWord = subWord.Substring(k);Console.WriteLine(tempWord);if (Search(tempWord)){flag = true;list.Add(tempWord);break;}}if (flag){start = start - tempWord.Length;}else{start--;}len = start < 0 ? max + start : max;}}}foreach (string x in list){Console.WriteLine(x);}Console.ReadKey();}public static void Initail(){dictionary.Add("大家");dictionary.Add("好");dictionary.Add("我");dictionary.Add("一名");dictionary.Add("程序員");dictionary.Add("nick");}public static bool Search(string word){return dictionary.Contains(word);}} 分類:?ASP.NET本文轉自左正博客園博客,原文鏈接:http://www.cnblogs.com/soundcode/p/4931166.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的逆向最大匹配分词算法C#的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Golang 一些基本功能使用记录
- 下一篇: 使用C#开发数据库应用系统