---
title: "一段没有空格的中英文分词的n-gram算法实现"
author: "hermitte"
date: "2007-02-01T23:11:28Z"
tags: []
slug: "n-gram"
draft: false
lastmod: "2007-02-01T23:11:28Z"
id: 51688
url: "https://hermitte.iteye.com/blog/51688"
---

我刚写过个C#的实现。用的N-Gram算法很简单的。也能解决楼上的朋友的问题就是第一个单词和往后数8个单词的排列组合的取最大概率值得时候，把第一位的单词作为分词的结果，然后分词窗口后移，继续下一步。用堆栈作的 等下我给你找找,算法部分直接就可以在java下面Ctrl+C了。。 我开发项目用java,作数据挖掘和商业算法研究用C#的

c# 代码

001. using System;
002. using System.Collections.Generic;
003. using System.Text;
004. using System.Collections;
005. using System.IO;
006. namespace HNOZ
007. {
008. class Program
009. {
010. static double UNKNOWN = 0.05F;
011. static int pt = 0;
012. static int PRE\_LENGTH =8;
013. static int FL\_LENGTH = 8;
014. static int fl = FL\_LENGTH;
015. static int pre = PRE\_LENGTH;
016. static string sentence = "goodmorningbetterhello";
017. static Hashtable dict = new Hashtable();
018. static void Init()
019. {
020. }
021. static void Main(string\[] args)
022. {
023. Hashtable ht = new Hashtable();
024. // sentence = "欧美的政治上的保守党派确实经济上的自由主义鼓吹者欧美欧美的政治上的保守党派确实经济上欧美的政治上的保守党派确实经济上的自由主义鼓吹者欧美欧美的政治上的保守党派确实经济上";
025. //string sentence = "欧美的政治上的保守党派确实经";
026. string sentence = "goodmorningbetterhello";
027. FileStream fs = new FileStream("11.csv", FileMode.Open);
028. StreamReader sr = new StreamReader(fs);
029. string line = "";
030. string hz = "";
031. string gl = "";
032. dict.Add("", 0.00);
033. while ((line = sr.ReadLine()) != null)
034. {
035. int i = line.IndexOf(',');
036. hz = line.Substring(0, i);
037. gl = line.Substring(i + 1, line.Length - i - 1);
038. if (!hz.Equals("?"))
039. dict.Add(hz, double.Parse(gl));
040. }
041. int start = 0;
042. //dict = ht;
043. //string aa = Console.ReadLine();
044. Console.WriteLine(analyse(sentence, start));
045. string e = Console.ReadLine();
046. start = 0;
047. Console.WriteLine(analyse(e, start));
048. Console.WriteLine(analyse(sentence, start));
049. }
050. static string analyse(string sentence, int start)
051. {
052. string results = "";
053. string nowstr = "";
054. int pos = 0;
055. int len = 0;
056. double max = 0;
057. double now = 0;
058. while (start &lt; sentence.Length)
059. {
060. nowstr = Split(sentence.Substring(start, sentence.Length - start));
061. start = start + nowstr.Length;
062. results += nowstr + "/";
063. }
064. return results;
065. }
066. static string Split(string sentence)
067. {
068. int m = 0;
069. int result = 0;
070. double now = 0;
071. double p = 0;
072. int pos = 0;
073. double max = 0;
074. int j = 1;
075. string curWord = "";
076. int i = 1;
077. int len = 0;
078. int\[] oj = new int\[PRE\_LENGTH];
079. if (sentence.Length &lt; PRE\_LENGTH)
080. {
081. fl = sentence.Length + 1;
082. pre = sentence.Length + 1;
083. }
084. else
085. {
086. fl = FL\_LENGTH;
087. pre = PRE\_LENGTH;
088. }
089. while (i &lt; pre)
090. {
091. while (j &lt; fl)
092. {
093. oj\[i] = j;
094. pos = 0;
095. for (int k = 1; k &lt; i; k++)
096. {
097. pos = pos + oj\[k];
098. }
099. if (pos + j &gt; 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 &gt; 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. }