| 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 |
30x
30x
50x
50x
26x
26x
26x
26x
26x
26x
27x
29x
29x
29x
26x
26x
25x
26x
27x
27x
27x
27x
33x
33x
33x
33x
34x
34x
10x
34x
2x
2x
20x
2x
2x
2x
50x
26x
26x
26x
26x
6x
44x
52x
52x
71x
71x
19x
19x
52x
52x
52x
19x
13x
52x
61x
61x
61x
115x
115x
54x
54x
61x
61x
3x
3x
3x
3x
4x
4x
4x
4x
3x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
2x
3x
3x
3x
3x
4x
4x
4x
4x
3x
2x
2x
2x
2x
2x
2x
27x
27x
27x
27x
27x
27x
31x
1x
1x
1x
1x
1x
26x
56x
56x
56x
56x
56x
31x
56x
56x
31x
31x
56x
51x
51x
51x
51x
107x
107x
51x
56x
51x
30x
| /*
* Copyright (c) AXA Shared Services Spain S.A.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const NamedEntity = require('./named-entity');
const RegexNamedEntity = require('./regex-named-entity');
/**
* Class for the Trim Named Entity.
*/
class TrimNamedEntity extends NamedEntity {
/**
* Constructor of the class.
* @param {Object} settings Settings for the instance.
*/
constructor(settings) {
super(settings);
this.type = 'trim';
}
addBetweenCondition(srcLanguages, srcLeftWords, srcRightWords, srcOptions) {
const options = srcOptions || {};
const languages = Array.isArray(srcLanguages)
? srcLanguages
: [srcLanguages];
const leftWords = Array.isArray(srcLeftWords)
? srcLeftWords
: [srcLeftWords];
const rightWords = Array.isArray(srcRightWords)
? srcRightWords
: [srcRightWords];
const conditions = [];
for (let i = 0; i < leftWords.length; i += 1) {
for (let j = 0; j < rightWords.length; j += 1) {
const leftWord =
options.noSpaces === true ? leftWords[i] : ` ${leftWords[i]} `;
const rightWord =
options.noSpaces === true ? rightWords[j] : ` ${rightWords[j]} `;
conditions.push(`(?<=${leftWord})(.*)(?=${rightWord})`);
}
}
let regex = `/${conditions.join('|')}/g`;
if (!(options.caseSensitive === true)) {
regex += 'i';
}
languages.forEach(language => {
const locale = this.getLocale(language);
Eif (!locale.conditions) {
locale.conditions = [];
}
locale.conditions.push({
type: 'between',
leftWords,
rightWords,
regex: RegexNamedEntity.str2regex(regex),
options,
});
});
}
addPositionCondition(position, srcLanguages, srcWords, srcOptions) {
const options = srcOptions || {};
const languages = Array.isArray(srcLanguages)
? srcLanguages
: [srcLanguages];
const words = Array.isArray(srcWords) ? srcWords : [srcWords];
languages.forEach(language => {
const locale = this.getLocale(language);
if (!locale.conditions) {
locale.conditions = [];
}
locale.conditions.push({ type: position, words, options });
});
}
addAfterCondition(srcLanguages, srcWords, srcOptions) {
this.addPositionCondition('after', srcLanguages, srcWords, srcOptions);
}
addAfterFirstCondition(srcLanguages, srcWords, srcOptions) {
this.addPositionCondition('afterFirst', srcLanguages, srcWords, srcOptions);
}
addAfterLastCondition(srcLanguages, srcWords, srcOptions) {
this.addPositionCondition('afterLast', srcLanguages, srcWords, srcOptions);
}
addBeforeCondition(srcLanguages, srcWords, srcOptions) {
this.addPositionCondition('before', srcLanguages, srcWords, srcOptions);
}
addBeforeFirstCondition(srcLanguages, srcWords, srcOptions) {
this.addPositionCondition(
'beforeFirst',
srcLanguages,
srcWords,
srcOptions
);
}
addBeforeLastCondition(srcLanguages, srcWords, srcOptions) {
this.addPositionCondition('beforeLast', srcLanguages, srcWords, srcOptions);
}
mustSkip(word, condition) {
if (
condition.options &&
condition.options.skip &&
condition.options.skip.length > 0
) {
for (let i = 0; i < condition.options.skip.length; i += 1) {
const skipWord = condition.options.skip[i];
Iif (condition.options.caseSensitive) {
if (skipWord === word) {
return true;
}
} else if (skipWord.toLowerCase() === word.toLowerCase()) {
return true;
}
}
}
return false;
}
matchBetween(utterance, condition) {
const result = [];
let matchFound;
do {
const match = condition.regex.exec(` ${utterance} `);
if (match) {
result.push({
type: 'between',
start: match.index - 1,
end: condition.regex.lastIndex - 2,
len: match[0].length,
accuracy: 1,
sourceText: match[0],
utteranceText: match[0],
entity: this.name,
});
matchFound = true;
} else {
matchFound = false;
}
} while (matchFound);
const filteredResult = [];
for (let i = 0; i < result.length; i += 1) {
if (!this.mustSkip(result[i].utteranceText, condition)) {
filteredResult.push(result[i]);
}
}
return filteredResult;
}
findWord(utterance, word, caseSensitive = false, noSpaces = false) {
const result = [];
let matchFound;
const regex = new RegExp(
noSpaces ? word : ` ${word} | ${word}|${word} `,
caseSensitive ? 'g' : 'ig'
);
do {
const match = regex.exec(utterance);
if (match) {
result.push({
start: match.index,
end: regex.lastIndex,
});
matchFound = true;
} else {
matchFound = false;
}
} while (matchFound);
return result;
}
getBeforeResults(utterance, wordPositions) {
const result = [];
let startPos = 0;
let endPos = 0;
for (let i = 0; i < wordPositions.length; i += 1) {
endPos = wordPositions[i].start;
const text = utterance.substring(startPos, endPos);
result.push({
type: 'before',
start: startPos,
end: endPos - 1,
len: text.length,
accuracy: 0.99,
sourceText: text,
utteranceText: text,
entity: this.name,
});
startPos = wordPositions[i].end;
}
return result;
}
getBeforeFirstResults(utterance, wordPositions) {
const result = [];
const startPos = 0;
const endPos = wordPositions[0].start;
const text = utterance.substring(startPos, endPos);
result.push({
type: 'beforeFirst',
start: startPos,
end: endPos - 1,
len: text.length,
accuracy: 0.99,
sourceText: text,
utteranceText: text,
entity: this.name,
});
return result;
}
getBeforeLastResults(utterance, wordPositions) {
const result = [];
const startPos = 0;
const endPos = wordPositions[wordPositions.length - 1].start;
const text = utterance.substring(startPos, endPos);
result.push({
type: 'beforeLast',
start: startPos,
end: endPos - 1,
len: text.length,
accuracy: 0.99,
sourceText: text,
utteranceText: text,
entity: this.name,
});
return result;
}
getAfterResults(utterance, wordPositions) {
const result = [];
let startPos = 0;
let endPos = utterance.length;
for (let i = wordPositions.length - 1; i >= 0; i -= 1) {
startPos = wordPositions[i].end;
const text = utterance.substring(startPos, endPos);
result.unshift({
type: 'after',
start: startPos,
end: endPos - 1,
len: text.length,
accuracy: 0.99,
sourceText: text,
utteranceText: text,
entity: this.name,
});
endPos = wordPositions[i].start;
}
return result;
}
getAfterFirstResults(utterance, wordPositions) {
const result = [];
const startPos = wordPositions[0].end;
const endPos = utterance.length;
const text = utterance.substring(startPos, endPos);
result.push({
type: 'afterFirst',
start: startPos,
end: endPos - 1,
len: text.length,
accuracy: 0.99,
sourceText: text,
utteranceText: text,
entity: this.name,
});
return result;
}
getAfterLastResults(utterance, wordPositions) {
const result = [];
const startPos = wordPositions[wordPositions.length - 1].end;
const endPos = utterance.length;
const text = utterance.substring(startPos, endPos);
result.push({
type: 'afterLast',
start: startPos,
end: endPos - 1,
len: text.length,
accuracy: 0.99,
sourceText: text,
utteranceText: text,
entity: this.name,
});
return result;
}
getResults(utterance, wordPositions, type) {
switch (type) {
case 'before':
return this.getBeforeResults(utterance, wordPositions, type);
case 'beforeFirst':
return this.getBeforeFirstResults(utterance, wordPositions, type);
case 'beforeLast':
return this.getBeforeLastResults(utterance, wordPositions, type);
case 'after':
return this.getAfterResults(utterance, wordPositions, type);
case 'afterFirst':
return this.getAfterFirstResults(utterance, wordPositions, type);
case 'afterLast':
return this.getAfterLastResults(utterance, wordPositions, type);
default:
return [];
}
}
match(utterance, condition) {
const result = [];
for (let i = 0; i < condition.words.length; i += 1) {
const word = condition.options.noSpaces
? condition.words[i]
: ` ${condition.words[i]}`;
const wordPositions = this.findWord(utterance, word);
if (wordPositions.length > 0) {
result.push(
...this.getResults(utterance, wordPositions, condition.type)
);
}
}
const filteredResult = [];
for (let i = 0; i < result.length; i += 1) {
Eif (!this.mustSkip(result[i].utteranceText, condition)) {
filteredResult.push(result[i]);
}
}
return filteredResult;
}
extract(utterance, language) {
const result = [];
const locale = this.getLocaleRules(language);
Iif (!locale || !locale.conditions) {
return result;
}
for (let i = 0; i < locale.conditions.length; i += 1) {
const condition = locale.conditions[i];
if (condition.type === 'between') {
result.push(...this.matchBetween(utterance, condition));
} else {
result.push(...this.match(utterance, condition));
}
}
return result;
}
}
module.exports = TrimNamedEntity;
|