日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

逆向最大匹配分词算法C#

發布時間:2025/3/21 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 逆向最大匹配分词算法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#的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。