-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUralicApi.cs
288 lines (263 loc) · 10.4 KB
/
UralicApi.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System.Text.RegularExpressions;
using hfst;
namespace UralicNLP
{
public class UralicApi
{
private String ModelPath;
private String DownloadServerUrl = "http://models.uralicnlp.com/nightly/";
private Dictionary<string, hfst.HFST> transducerCache = new Dictionary<string, hfst.HFST>();
public UralicApi()
{
ModelPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".uralicnlp");
}
public UralicApi(String modelPath)
{
ModelPath = modelPath;
}
public bool IsLanguageInstalled(string language)
{
var languageFolder = Path.Combine(ModelPath, language);
return Directory.Exists(languageFolder) || File.Exists(languageFolder);
}
private HFST LoadTransducer(string language, string filename)
{
string languageFolder = Path.Combine(ModelPath, language, filename);
if (!transducerCache.ContainsKey(languageFolder))
{
var transducer = new HFST(languageFolder);
transducerCache[languageFolder] = transducer;
}
return transducerCache[languageFolder];
}
public void ModelInfo(string language)
{
var languageFolder = Path.Combine(ModelPath, language, "metadata.json");
using (FileStream fs = new FileStream(languageFolder, FileMode.Open, FileAccess.Read))
using (StreamReader reader = new StreamReader(fs, System.Text.Encoding.UTF8))
{
string str;
while ((str = reader.ReadLine()) != null)
{
Console.WriteLine(str);
}
}
}
private string GetModelName(bool analyzer, bool descriptive, bool dictionaryForms)
{
if (analyzer)
{
if (dictionaryForms)
{
return "analyser-dict";
}
else if (descriptive)
{
return "analyser";
}
else
{
return "analyser-norm";
}
}
else
{
if (!descriptive && dictionaryForms)
{
return "generator";
}
else if (descriptive)
{
return "generator-desc";
}
else
{
return "generator-norm";
}
}
}
public List<Result> Analyze(string word, string language, bool descriptive, bool dictionaryForms)
{
string modelName = GetModelName(true, descriptive, dictionaryForms);
HFST t = LoadTransducer(language, modelName);
var analyses = t.Lookup(word);
return analyses;
}
public List<Result> Analyze(string word, string language)
{
return Analyze(word, language, true, false);
}
public List<Result> Generate(string word, string language)
{
return Generate(word, language, false, false);
}
public List<Result> Generate(string word, string language, bool descriptive, bool dictionaryForms)
{
string modelName = GetModelName(false, descriptive, dictionaryForms);
HFST t = LoadTransducer(language, modelName);
var analyses = t.Lookup(word);
return analyses;
}
public List<string> Lemmatize(string word, string language, bool descriptive, bool dictionaryForms, bool wordBoundaries)
{
List<string> results = new List<string>();
var res = Analyze(word, language, descriptive, dictionaryForms);
string bound = "";
if (wordBoundaries)
{
bound = "|";
}
foreach (var ana in res)
{
var an = ana.ToString();
string lemma;
if (language.Equals("swe"))
{
lemma = Regex.Replace(an, "[<].*?[>]", bound);
while (lemma.Length > 0 && bound.Length > 0 && bound[0] == lemma[lemma.Length - 1])
{
lemma = lemma.Substring(0, lemma.Length - 1);
}
}
else if (language.Equals("ara"))
{
lemma = StringProcessing.FilterArabic(an, true, bound); // You'll need to implement this method
}
else if (language.Equals("fin_hist"))
{
string rege = "(?<=WORD_ID=)[^\\]]*";
List<string> allMatches = new List<string>();
MatchCollection matches = Regex.Matches(an, rege);
foreach (Match match in matches)
{
allMatches.Add(match.Value);
}
lemma = string.Join(bound, allMatches);
}
else if (an.Contains("<") && an.Contains(">"))
{
// Apertium
string[] parts = an.Split('+');
List<string> lemmaParts = new List<string>();
foreach (var part in parts)
{
lemmaParts.Add(part.Split('<')[0]);
}
lemma = string.Join(bound, lemmaParts);
}
else
{
if (an.Contains("#") && !an.Contains("+Cmp#"))
{
an = an.Replace("#", "+Cmp#");
}
string[] parts = an.Split(new string[] { "+Cmp#" }, StringSplitOptions.None);
List<string> lemmaParts = new List<string>();
foreach (var part in parts)
{
string p = part.Split('+')[0];
if (language.Equals("eng"))
{
p = Regex.Replace(p, "[\\[].*?[\\]]", "");
}
lemmaParts.Add(p);
}
lemma = string.Join(bound, lemmaParts);
}
results.Add(lemma);
}
// Use Distinct to remove duplicates and ToList to convert back to List
return results.Distinct().ToList();
}
public void Uninstall(string language)
{
string directoryPath = Path.Combine(ModelPath, language);
if (Directory.Exists(directoryPath))
{
Directory.Delete(directoryPath, true);
}
}
public void SupportedLanguages()
{
try
{
string s = CommonTools.ReadToString(DownloadServerUrl + "supported_languages.json");
Console.WriteLine(s);
}
catch (System.IO.IOException ex)
{
Console.WriteLine("An IO exception occurred: " + ex.Message);
// Optionally, handle the exception here
}
}
public void Download(string language)
{
Download(language, true);
}
public List<string> Lemmatize(string word, string language)
{
return Lemmatize(word, language, true, false, false);
}
public List<string> Lemmatize(string word, string language, bool wordBoundaries)
{
return Lemmatize(word, language, true, false, wordBoundaries);
}
public void Download(string language, bool showProgress)
{
var urlMap = new Dictionary<string, string>
{
["analyser"] = "analyser-gt-desc.hfstol",
["analyzer.pt"] = $"../neural/{language}_analyzer_nmt-model_step_100000.pt",
["generator.pt"] = $"../neural/{language}_generator_nmt-model_step_100000.pt",
["lemmatizer.pt"] = $"../neural/{language}_lemmatizer_nmt-model_step_100000.pt",
["analyser-norm"] = "analyser-gt-norm.hfstol",
["analyser-dict"] = "analyser-dict-gt-norm.hfstol",
["generator-desc"] = "generator-gt-desc.hfstol",
["generator-norm"] = "generator-gt-norm.hfstol",
["generator"] = "generator-dict-gt-norm.hfstol",
["cg"] = "disambiguator.bin",
["metadata.json"] = "metadata.json",
["dictionary.json"] = "dictionary.json"
};
var languageFolder = Path.Combine(ModelPath, language);
try
{
Directory.CreateDirectory(languageFolder);
}
catch (IOException ex)
{
// Handle exceptions related to directory creation
Console.WriteLine("Failed to create directory: " + ex.Message);
}
foreach (var entry in urlMap)
{
string fileName = entry.Key;
string urlName = $"{language}/{entry.Value}";
Console.WriteLine($"Downloading model {fileName} for language {language}");
try
{
CommonTools.DownloadToFile(DownloadServerUrl + urlName, Path.Combine(languageFolder, fileName), showProgress);
}
catch (Exception ex)
{
Console.WriteLine("Model wasn't downloaded, it may not exist for this language");
if ("metadata.json" == fileName)
{
try
{
using (var writer = new StreamWriter(File.OpenWrite(Path.Combine(languageFolder, fileName)), System.Text.Encoding.UTF8))
{
writer.Write("{\"info\":\"no metadata provided\"}");
}
}
catch (IOException ex1)
{
// Handle exceptions related to file writing
Console.WriteLine("Failed to write file: " + ex1.Message);
}
}
}
}
}
}
}