ITeye博客

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

hermitte · 2007-02-01 23:11 · 商业智能和数据挖掘 · Markdown

我刚写过个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. namespace HNOZ
  7. {
  8. class Program
  9. {
  10. static double UNKNOWN = 0.05F;
  11. static int pt = 0;
  12. static int PRE_LENGTH =8;
  13. static int FL_LENGTH = 8;
  14. static int fl = FL_LENGTH;
  15. static int pre = PRE_LENGTH;
  16. static string sentence = "goodmorningbetterhello";
  17. static Hashtable dict = new Hashtable();
  18. static void Init()
  19. {
  20. }
  21. static void Main(string[] args)
  22. {
  23. Hashtable ht = new Hashtable();
  24. // sentence = "欧美的政治上的保守党派确实经济上的自由主义鼓吹者欧美欧美的政治上的保守党派确实经济上欧美的政治上的保守党派确实经济上的自由主义鼓吹者欧美欧美的政治上的保守党派确实经济上";
  25. //string sentence = "欧美的政治上的保守党派确实经";
  26. string sentence = "goodmorningbetterhello";
  27. FileStream fs = new FileStream("11.csv", FileMode.Open);
  28. StreamReader sr = new StreamReader(fs);
  29. string line = "";
  30. string hz = "";
  31. string gl = "";
  32. dict.Add("", 0.00);
  33. while ((line = sr.ReadLine()) != null)
  34. {
  35. int i = line.IndexOf(',');
  36. hz = line.Substring(0, i);
  37. gl = line.Substring(i + 1, line.Length - i - 1);
  38. if (!hz.Equals("?"))
  39. dict.Add(hz, double.Parse(gl));
  40. }
  41. int start = 0;
  42. //dict = ht;
  43. //string aa = Console.ReadLine();
  44. Console.WriteLine(analyse(sentence, start));
  45. string e = Console.ReadLine();
  46. start = 0;
  47. Console.WriteLine(analyse(e, start));
  48. Console.WriteLine(analyse(sentence, start));
  49. }
  50. static string analyse(string sentence, int start)
  51. {
  52. string results = "";
  53. string nowstr = "";
  54. int pos = 0;
  55. int len = 0;
  56. double max = 0;
  57. double now = 0;
  58. while (start < sentence.Length)
  59. {
  60. nowstr = Split(sentence.Substring(start, sentence.Length - start));
  61. start = start + nowstr.Length;
  62. results += nowstr + "/";
  63. }
  64. return results;
  65. }
  66. static string Split(string sentence)
  67. {
  68. int m = 0;
  69. int result = 0;
  70. double now = 0;
  71. double p = 0;
  72. int pos = 0;
  73. double max = 0;
  74. int j = 1;
  75. string curWord = "";
  76. int i = 1;
  77. int len = 0;
  78. int[] oj = new int[PRE_LENGTH];
  79. if (sentence.Length < PRE_LENGTH)
  80. {
  81. fl = sentence.Length + 1;
  82. pre = sentence.Length + 1;
  83. }
  84. else
  85. {
  86. fl = FL_LENGTH;
  87. pre = PRE_LENGTH;
  88. }
  89. while (i < pre)
  90. {
  91. while (j < fl)
  92. {
  93. oj[i] = j;
  94. pos = 0;
  95. for (int k = 1; k < i; k++)
  96. {
  97. pos = pos + oj[k];
  98. }
  99. if (pos + j > sentence.Length)
  100. {
  101. curWord = "";
  102. }
  103. else
  104. {
  105. len = j;
  106. curWord = sentence.Substring(pos, len);
  107. }
  108. if (dict[curWord] != null)
  109. {
  110. p = (double)dict[curWord];
  111. }
  112. else
  113. {
  114. j++;
  115. continue;
  116. }
  117. if (i == pre - 1)
  118. {
  119. if (p + now > max)
  120. {
  121. result = oj[1];
  122. max = p + now;
  123. }
  124. else
  125. {
  126. }
  127. j++;
  128. }
  129. else
  130. {
  131. now = now + p;
  132. i++;
  133. j = 1;
  134. }
  135. //  j++;
  136. }
  137. j = oj[i - 1];
  138. oj[i] = 0;
  139. j++;
  140. i--;
  141. if (j == 1 && i == 0)
  142. break;
  143. }
  144. return sentence.Substring(0, result);
  145. }
  146. }
  147. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。