-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_SpellCorrect.ahk
135 lines (109 loc) · 3.21 KB
/
class_SpellCorrect.ahk
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
#NoEnv
/*
SetBatchLines, -1
s := new Spell
s.Train("something")
s.Train("something")
s.Train("soothing")
s.Train("seething")
Result := ""
For Index, Word In s.Correct("oething")
Result .= Word . "`n"
MsgBox % SubStr(Result,1,-1)
Return
*/
/*
SetBatchLines, -1
FileRead, Data, %A_ScriptDir%\Spelling Data.txt
Model := Object()
Loop, Parse, Data, `n
{
StringSplit, Field, A_LoopField, %A_Tab%
Model[Field1] := Field2
}
s := new Spell
s.Load(Model)
Result := ""
For Index, Word In s.Correct("omthing")
Result .= Word . "`n"
MsgBox % SubStr(Result,1,-1)
Return
*/
class Spell{
__New() {
this.Model := Object()
}
Load(Model,Replace = 1) {
If Replace
this.Model := Model
Else {
For Word, Occurrences In Model {
If ObjHasKey(this.Model,Word)
this.Model[Word] += Occurrences
Else
this.Model[Word] := Occurrences
}
}
}
Train(Word) {
If !ObjHasKey(this.Model,Word)
this.Model[Word] := 1
Else
this.Model[Word] ++
}
Edits1(Word) {
CharSet := "abcdefghijklmnopqrstuvwxyzäöüß"
Result := Object()
Length := StrLen(Word)
Index := 0
Loop, % Length {
Result[SubStr(Word,1,Index) . SubStr(Word,Index + 2)] := 1 ;deletes
Loop, Parse, CharSet
{
Result[SubStr(Word,1,Index) . A_LoopField . SubStr(Word,Index + 2)] := 1 ;replaces
Result[SubStr(Word,1,Index) . A_LoopField . SubStr(Word,Index + 1)] := 1 ;inserts
}
Index ++
}
Loop, Parse, CharSet
Result[Word . A_LoopField] := 1 ;inserts
;transposes
Loop, % Length - 1
Result[SubStr(Word,1,A_Index - 1) . SubStr(Word,A_Index + 1,1) . SubStr(Word,A_Index,1) . SubStr(Word,A_Index + 2)] := 1
Return, Result
}
Rank(Values) {
Candidates := []
For Word, Occurrences In Values
Candidates.Insert(Object("Word",Word,"Occurrences",Occurrences))
Loop, % (Candidates.MaxIndex() - 1) {
Index := A_Index
While, Index > 0 && Candidates[Index].Occurrences < Candidates[Index + 1].Occurrences
Value := Candidates[Index + 1], Candidates[Index + 1] := Candidates[Index], Candidates[Index] := Value, Index --
}
Result := []
For Index, Value In Candidates
Result.Insert(Value.Word)
Return, Result
}
Correct(Word) {
If ObjHasKey(this.Model,Word)
Return, [Word]
Result := Object()
For Value In this.Edits1(Word) {
If ObjHasKey(this.Model,Value)
Result[Value] := this.Model[Value]
}
For Value In Result
Return, this.Rank(Result)
For Edit1 In this.Edits1(Word) {
For Value In this.Edits1(Edit1) {
If ObjHasKey(this.Model,Value)
Result[Value] := this.Model[Value]
}
}
For Value In Result
Return, this.Rank(Result)
Return, [Word]
}
}