My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;


namespace SpellCheck
{
class wordweightpair:IComparable
{
public int probabillity_weight;
public string word;
public wordweightpair(int p, string w)
{
probabillity_weight=p;
word=w;
}
public int CompareTo(object cl)
{
if (typeof(wordweightpair) == cl.GetType())
{
return this.probabillity_weight - ((wordweightpair)cl).probabillity_weight;
}
else
{
return 0;
}
}
}
class spell
{
private Dictionary<string,int> nWords=new Dictionary<string,int>();
private List<char> alphabets;
public spell(String dict)
{

/*this is contructor,
pass dict (a long text of any language (of which you want to build the spell checker)
all dictionary will be built from this.
*/
int pos=0;
Regex wordpat = new Regex(@"[a-z]+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m=null;
while((m=wordpat.Match(dict,pos)).Success)
{
string cur_val = m.Value.ToLower();
if (nWords.ContainsKey(cur_val))
{
nWords[cur_val] = nWords[cur_val] + 1;
}
else
{
nWords.Add(cur_val, 0);
}
pos=m.Index+m.Length;
}
buildalphabet();
}
private void buildalphabet()
{
alphabets=new List<char>();
for(char c='a';c<='z';c++)
{
alphabets.Add(c);
}
}
private List<string> edits(string word)
{
List<string> result=new List<string>();
for (int i = 0; i < word.Length; ++i)
{
/*Delete*/
result.Add(word.Substring(0, i) + word.Substring(i+1));
}
for(int i=0; i < word.Length -1; ++i)
{
/*transpose*/
result.Add(word.Substring(0, i) + word.Substring(i + 1, 1) + word.Substring(i, 1) + word.Substring(i + 2));
}
for (int i = 0; i < word.Length; ++i)
{
/*alter*/
foreach(char c in alphabets)
{
result.Add(word.Substring(0, i) + c + word.Substring(i + 1));
}
}
for (int i = 0; i <= word.Length; ++i)
{
/*insert*/
foreach(char c in alphabets)
{
result.Add(word.Substring(0, i) + c + word.Substring(i));
}
}
return result;
}
public string correct(string word)
{
word = word.ToLower();
if(nWords.ContainsKey(word))
{
return word;
}
List<string> list=edits(word);
List<wordweightpair> candidates = new List<wordweightpair>();


foreach(string s in list)
{
if(nWords.ContainsKey(s))
{
candidates.Add(new wordweightpair(nWords[s], s));
}
}
if (candidates.Count > 0)
{
candidates.Sort();
return candidates[0].word;
}
foreach (string s in list)
{
List<string> list2=edits(s);/*second level of edits*/
foreach (string ss in list2)
{
if(nWords.ContainsKey(ss))
{
candidates.Add(new wordweightpair(nWords[ss], ss));
}
}
}
if (candidates.Count > 0)
{
candidates.Sort();
return candidates[0].word;
}
return "NOT FOUND";
}

}
}

Change log

r9 by Mark.dawn on Jan 23, 2011   Diff
putting some comment
Go to: 
Project members, sign in to write a code review

Older revisions

r8 by Mark.dawn on Nov 26, 2010   Diff
a redundant check pointed by Nicola
Turri
r7 by mark.dawn on Mar 26, 2010   Diff
spell-check implementation based on
Norvig's spelling corrector.
http://norvig.com/spell-correct.html
All revisions of this file

File info

Size: 4412 bytes, 142 lines
Powered by Google Project Hosting