一段没有空格的中英文分词的n-gram算法实现

我刚写过个C#的实现。用的N-Gram算法很简单的。也能解决楼上的朋友的问题就是第一个单词和往后数8个单词的排列组合的取最大概率值得时候,把第一位的单词作为分词的结果,然后分词窗口后移,继续下一步。用堆栈作的 等下我给你找找,算法部分直接就可以在java下面Ctrl+C了。。 我开发项目用java,作数据挖掘和商业算法研究用C#的
c# 代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Collections;   
  5. using System.IO;   
  6.   
  7. namespace HNOZ   
  8. {   
  9.     class Program   
  10.     {   
  11.   
  12.   
  13.         static double UNKNOWN = 0.05F;   
  14.         static int pt = 0;   
  15.   
  16.         static int PRE_LENGTH =8;   
  17.         static int FL_LENGTH = 8;   
  18.         static int fl = FL_LENGTH;   
  19.         static int pre = PRE_LENGTH;   
  20.         static string sentence = "goodmorningbetterhello";   
  21.         static Hashtable dict = new Hashtable();   
  22.         static void Init()   
  23.         {   
  24.   
  25.         }   
  26.         static void Main(string[] args)   
  27.         {   
  28.             Hashtable ht = new Hashtable();   
  29.             // sentence = "欧美的政治上的保守党派确实经济上的自由主义鼓吹者欧美欧美的政治上的保守党派确实经济上欧美的政治上的保守党派确实经济上的自由主义鼓吹者欧美欧美的政治上的保守党派确实经济上";   
  30.             //string sentence = "欧美的政治上的保守党派确实经";   
  31.             string sentence = "goodmorningbetterhello";   
  32.   
  33.   
  34.             FileStream fs = new FileStream("11.csv", FileMode.Open);   
  35.             StreamReader sr = new StreamReader(fs);   
  36.             string line = "";   
  37.             string hz = "";   
  38.             string gl = "";   
  39.             dict.Add("", 0.00);   
  40.             while ((line = sr.ReadLine()) != null)   
  41.             {   
  42.                 int i = line.IndexOf(',');   
  43.                 hz = line.Substring(0, i);   
  44.                 gl = line.Substring(i + 1, line.Length - i - 1);   
  45.                 if (!hz.Equals("?"))   
  46.                     dict.Add(hz, double.Parse(gl));   
  47.             }   
  48.             int start = 0;   
  49.             //dict = ht;   
  50.             //string aa = Console.ReadLine();   
  51.             Console.WriteLine(analyse(sentence, start));   
  52.             string e = Console.ReadLine();   
  53.             start = 0;   
  54.             Console.WriteLine(analyse(e, start));   
  55.             Console.WriteLine(analyse(sentence, start));   
  56.   
  57.   
  58.   
  59.         }   
  60.         static string analyse(string sentence, int start)   
  61.         {   
  62.             string results = "";   
  63.             string nowstr = "";   
  64.             int pos = 0;   
  65.             int len = 0;   
  66.             double max = 0;   
  67.             double now = 0;   
  68.   
  69.             while (start < sentence.Length)   
  70.             {   
  71.                 nowstr = Split(sentence.Substring(start, sentence.Length - start));   
  72.   
  73.   
  74.   
  75.   
  76.                 start = start + nowstr.Length;   
  77.                 results += nowstr + "/";   
  78.             }   
  79.             return results;   
  80.         }   
  81.   
  82.   
  83.   
  84.         static string Split(string sentence)   
  85.         {   
  86.             int m = 0;   
  87.             int result = 0;   
  88.             double now = 0;   
  89.             double p = 0;   
  90.             int pos = 0;   
  91.             double max = 0;   
  92.             int j = 1;   
  93.             string curWord = "";   
  94.             int i = 1;   
  95.             int len = 0;   
  96.             int[] oj = new int[PRE_LENGTH];   
  97.             if (sentence.Length < PRE_LENGTH)   
  98.             {   
  99.   
  100.                 fl = sentence.Length + 1;   
  101.                 pre = sentence.Length + 1;   
  102.   
  103.             }   
  104.             else  
  105.             {   
  106.                 fl = FL_LENGTH;   
  107.                 pre = PRE_LENGTH;   
  108.   
  109.   
  110.             }   
  111.             while (i < pre)   
  112.             {   
  113.   
  114.                 while (j < fl)   
  115.                 {   
  116.                     oj[i] = j;   
  117.                     pos = 0;   
  118.                     for (int k = 1; k < i; k++)   
  119.                     {   
  120.                         pos = pos + oj[k];   
  121.   
  122.                     }   
  123.                     if (pos + j > sentence.Length)   
  124.                     {   
  125.   
  126.                         curWord = "";   
  127.                     }   
  128.                     else  
  129.                     {   
  130.   
  131.                         len = j;   
  132.                         curWord = sentence.Substring(pos, len);   
  133.                     }   
  134.   
  135.                     if (dict[curWord] != null)   
  136.                     {   
  137.                         p = (double)dict[curWord];   
  138.                     }   
  139.                     else  
  140.                     {   
  141.                         j++;   
  142.                         continue;   
  143.                     }   
  144.                     if (i == pre - 1)   
  145.                     {   
  146.                         if (p + now > max)   
  147.                         {   
  148.                             result = oj[1];   
  149.                             max = p + now;   
  150.                         }   
  151.                         else  
  152.                         {   
  153.   
  154.   
  155.                         }   
  156.   
  157.                         j++;   
  158.   
  159.   
  160.                     }   
  161.                     else  
  162.                     {   
  163.                         now = now + p;   
  164.                         i++;   
  165.                         j = 1;   
  166.                     }   
  167.   
  168.   
  169.   
  170.                     //  j++;   
  171.   
  172.                 }   
  173.   
  174.                 j = oj[i - 1];   
  175.                 oj[i] = 0;   
  176.                 j++;   
  177.                 i--;   
  178.   
  179.                 if (j == 1 && i == 0)   
  180.                     break;   
  181.   
  182.   
  183.   
  184.             }   
  185.   
  186.   
  187.   
  188.   
  189.   
  190.   
  191.   
  192.   
  193.             return sentence.Substring(0, result);   
  194.   
  195.         }   
  196.   
  197.     }   
  198. }   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值