-
Notifications
You must be signed in to change notification settings - Fork 1
/
RepList.cs
145 lines (129 loc) · 3.37 KB
/
RepList.cs
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
143
144
145
using System;
using System.Collections.Generic;
using System.Text;
namespace HunspellSharp
{
class RepList
{
private List<replentry> dat;
public RepList(int n)
{
dat = new List<replentry>(Math.Min(n, 16384));
}
public int find(string word, int atstart = 0)
{
int p1 = 0;
int p2 = dat.Count - 1;
int ret = -1;
while (p1 <= p2)
{
int m = (p1 + p2) >> 1;
int c = string.Compare(word, atstart, dat[m].pattern, 0, dat[m].pattern.Length, StringComparison.Ordinal);
if (c < 0)
p2 = m - 1;
else if (c > 0)
p1 = m + 1;
else
{ // scan in the right half for a longer match
ret = m;
p1 = m + 1;
}
}
return ret;
}
public string replace(int wordlen, int ind, bool atstart)
{
int type = atstart ? 1 : 0;
if (wordlen == dat[ind].pattern.Length)
type = atstart ? 3 : 2;
while (type != 0 && string.IsNullOrEmpty(dat[ind].outstrings[type]))
type = (type == 2 && !atstart) ? 0 : type - 1;
return dat[ind].outstrings[type];
}
public int add(string pat1, string pat2)
{
if (pat1.Length == 0 || pat2.Length == 0)
{
return 1;
}
// analyse word context
int type = 0;
if (pat1[0] == '_')
{
pat1 = pat1.Remove(0, 1);
type = 1;
}
if (pat1.Length != 0 && pat1[pat1.Length - 1] == '_')
{
type = type + 2;
pat1 = pat1.Remove(pat1.Length - 1);
}
pat1 = pat1.Replace("_", " ");
// find existing entry
int m = find(pat1);
if (m >= 0 && dat[m].pattern == pat1)
{
// since already used
dat[m].outstrings[type] = pat2.Replace("_", " ");
return 0;
}
// make a new entry if none exists
var r = new replentry(pat1, pat2.Replace("_", " "), type);
dat.Add(r);
// sort to the right place in the list
int i;
for (i = dat.Count - 1; i > 0; --i)
{
if (string.Compare(r.pattern, dat[i - 1].pattern, StringComparison.Ordinal) < 0)
{
dat[i] = dat[i - 1];
}
else
break;
}
dat[i] = r;
return 0;
}
public string conv(string word)
{
StringBuilder dest = null;
int i0 = 0, wordlen = word.Length;
for (int i = 0; i < wordlen; ++i)
{
int n = find(word, i);
if (n < 0) continue;
var l = replace(wordlen - i, n, i == 0);
if (string.IsNullOrEmpty(l)) continue;
if (dest == null) dest = new StringBuilder();
if (i0 < i) dest.Append(word, i0, i - i0);
dest.Append(l);
if (dat[n].pattern.Length != 0)
{
i += dat[n].pattern.Length - 1;
}
i0 = i + 1;
}
if (dest == null) return word;
if (i0 < wordlen) dest.Append(word, i0, wordlen - i0);
return dest.ToString();
}
public bool check_against_breaktable(List<string> breaktable)
{
foreach (var i in dat)
{
foreach (var outstring in i.outstrings)
if (!string.IsNullOrEmpty(outstring))
{
foreach (var str in breaktable)
{
if (outstring.IndexOf(str) >= 0)
{
return false;
}
}
}
}
return true;
}
}
}