-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEKISentModule.js
227 lines (184 loc) · 5.88 KB
/
EKISentModule.js
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
EKIToolkit.prototype.modules.Lausestaja = function(EKI, config) {
/**
* This is the Lausestaja factory
*/
"use strict";
// See on moodul, lisame talle üldisi mooduli võimeid (init, destroy, notify)
EKI.modules.addGenericModuleParts(EKI.modules.Lausestaja);
/* observerList contains callbacks to launch after analysis */
//~ var observerList = [],
/* holds the set of morphologically analyzed tokens */
var tokenStorage = {},
/* holds up to JSONBatchsize tokens to be sent to analysis */
analyzeBatch = [],
/* holds tokens sent, but whose analysis hasn't been registered yet */
tokensWaitingForAnalysis = [],
/* holds the config */
config = config || {
//~ 'action': 'silbitus',
//~ 'JSONBatchsize': 100,
//~ 'JSONRetries': 3
};
/* *************************************************************** */
function addToken(token) {
/**
* Adds the token to be analyzed
* returns true if token was added
* returns false if token was '' or allready present
*/
"use strict";
analyzeBatch.push(token);
return true; // @todo: kiire häkk
/* kuna EKI morfoloogia analüüsija sisendiks on ainult eesti tähed,
* koristame kõik ülejäänud ära */
var token = cleanToken(token);
if ((token == '') || // don't accept empty tokens, nor
(token in tokenStorage) || // tokens allready analyzed, nor
(analyzeBatch.indexOf(token) != -1) || // tokens allready in the batch, nor
(tokensWaitingForAnalysis.indexOf(token) != -1)) { // tokens waiting for analysis
return false;
}
// analyzeBatch automatiseerib analüüsimise kindlate portsude kaupa
analyzeBatch.push(token);
// kui ports on täis, saadetakse see serverile analüüsimiseks
if (analyzeBatch.length >= config.JSONBatchsize) {
analyze();
}
return true;
}
function cleanToken(token) {
/**
* for now simply remove all non-estonian characters and make it
* lowercased.
*/
"use strict";
var token = token || '';
var nonChars = /[^abcdefghijklmnopqrsšzžtuvwõäöüxy]/ig;
token = token.replace(nonChars, '');
token = token.trim().toLocaleLowerCase();
return token;
}
function analyze(text) {
/** Sends all unanalyzed tokens to the server to be analyzed.
* Analysis is done using the current config
*/
"use strict";
EKISentenceCgi(text);
return true; // @todo: kiire häkk
// see if we need to analyze anything
if (analyzeBatch.length > 0) {
//~ tokensWaitingForAnalysis = tokensWaitingForAnalysis.concat(analyzeBatch);
//~ EKIMorfservJSONRequest(analyzeBatch);
EKISentenceCgi(analyzeBatch[0]);
analyzeBatch = [];
} else {
// otherwise just notify the subscribed observers directly
EKI.notify({type: "sentence-analysis-ALLREADY-ready"});
}
}
function onJSONReplySUCCESS(data, textStatus, jqXHR) {
/** This function is called on a positive reply from the server. It
* adds the analysises to the tokenStorage and notifies all
* observers.
*/
"use strict";
// extend tokenStorage @todo: remove jQuery
//~ jQuery.extend(tokenStorage, data); // @todo: should be able for in loop
tokenStorage = data;
// remove all the tokens that have been successfully analyzed from
// the tokensWaitingForAnalysis
//~ tokensWaitingForAnalysis = tokensWaitingForAnalysis.filter(
//~ function (token) {
//~ return !(token in tokenStorage);
//~ });
// @todo: notifyObservers only if the tokenStorage changed it's state
//~ notifyObservers();
EKI.notify({type: "sentence-analysis-ready"});
}
function onJSONReplyERROR(jqXHR, textStatus, ex) {
/** This function is called on a negative reply from the serve. It
* tries 3 times to re-analyze before giving up.
* @todo: NOT IMPLEMENTED!
*/
"use strict";
// @todo: should we try re-analyze only previously sent tokens, or
// all un-analyzed tokens?
// is it possible to get the sent batch from this JSON?
//~ console.log(textStatus + "," + ex + "," + jqXHR.responseText);
console.log('EKIToolkit: JSON ERROR ' + textStatus);
}
function emptyTokenStorage() {
/**
* Simply empties the tokenStorage
*/
"use strict";
tokenStorage = {};
}
function getTokenStorage() {
/**
* Returns the tokenStorage
*/
"use strict";
return tokenStorage;
}
function getTokenAnalysis (token) {
/**
* Returns the analysis data from tokenStorage or undefined.
*/
var ret;
if (token in tokenStorage) {
return tokenStorage[token];
} else {
return undefined;
}
}
function EKISentenceCgi(text) {
/**
* HIDDEN simple approach to Elgar's service
*/
"use strict";
//~ config['sone'] = tokens;
var sendData = {};
sendData['text'] = text;
jQuery.ajax({
type: "POST",
//~ async: true,
url: 'http://www.eki.ee/elgar/ekeel/sentence.cgi',
//~ contentType: 'application/json; charset=utf-8',
dataType: "json",
//~ data: JSON.stringify(config),
//~ data: JSON.stringify(sendData),
data: sendData,
//~ data: 'text='+text,
//~ timeout: 2000,
success: onJSONReplySUCCESS,
error: onJSONReplyERROR
});
}
function setConfig(confKey, confValue) {
/**
* Sets the config according to newConfig object. Available variables:
* - useCompoundDetection
* - useDictionary
* - useDerivationGuessing
*/
"use strict";
// @todo: we should do error checking that throws something!
config[confKey] = confValue;
}
// Lisame mooduli liidese otse EKIToolkit objekti alla kui 'Morph'
EKI.Lausestaja = {
/* this is the public interface */
setConfig: setConfig,
addToken: addToken,
analyze: analyze,
getTokenStorage: getTokenStorage,
getTokenAnalysis: getTokenAnalysis,
emptyTokenStorage: emptyTokenStorage,
cleanToken: cleanToken,
init: EKI.modules.Lausestaja.init,
destroy: EKI.modules.Lausestaja.destroy,
notify: EKI.modules.Lausestaja.notify, // @todo: kas see on õige?
};
EKI.Lausestaja.init(EKI);
};