-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWordNGrams.ecl
314 lines (281 loc) · 10.9 KB
/
WordNGrams.ecl
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
/**
* Easy method for generate word n-grams (so-called "shingles") from a string.
* The result is a dataset containing the n-grams as strings. Multiple n-grams
* (e.g. unigrams, bigrams, trigrams, etc) can be generated with one call.
*
* Exported record definition:
*
* WordNGramLayout
*
* Exported functions:
*
* GenerateWordNGrams
* GenerateWordUnigrams
* GenerateWordBigrams
* GenerateWordTrigrams
*
* Sample calls shown at the end of the file.
*
* Origin: https://github.com/hpccsystems-solutions-lab/Useful_ECL
*/
IMPORT Std;
EXPORT WordNGrams := MODULE
/**
* Record layout for datasets returned by exported functions
*/
EXPORT WordNGramLayout := RECORD
STRING ngrams;
END;
/**
* Generates word n-grams from an input string. A word delimiter is defined
* as a space or any punctuation character other than an apostrophe. Runs
* of word delimiters are treated as a single delimiter.
*
* Multiple n-grams (e.g. unigrams, bigrams, etc) can be generated in the
* same call by adjusting the min_gram and max_gram argument values.
*
* @param s The string to process; REQUIRED
* @param min_gram The smallest n-gram to include in the output;
* a zero value will be treated as a one value;
* OPTIONAL, defaults to 1 (unigram)
* @param max_gram The largest n-gram to include in the output;
* if provided and less than the (possibly
* adjusted) value of min_gram, will be set to
* min_gram; OPTIONAL, defaults to 1 (unigram)
* @param min_word_len The minimum length of a word that will be
* processed; words less than this length will be
* ignored when creating the ngrams; OPTIONAL,
* defaults to 1
*
* @return A dataset containing all n-grams generated from s.
*
* @see GenerateWordUnigrams
* @see GenerateWordBigrams
* @see GenerateWordTrigrams
*/
EXPORT STREAMED DATASET(WordNGramLayout) GenerateWordNGrams(CONST STRING s, UNSIGNED1 min_gram = 1, UNSIGNED1 max_gram = 1, UNSIGNED1 min_word_len = 1) := EMBED(C++)
#option pure;
#include <string>
#include <vector>
#body
#define IS_DELIMITER(x) ((::ispunct(x) != 0 && x != '\'') || ::isspace(x))
class StreamDataset : public RtlCInterface, implements IRowStream
{
public:
StreamDataset(IEngineRowAllocator* _resultAllocator, const char* _inputString, uint32_t _inputStringLen, uint32_t _minGram, uint32_t _maxGram, uint32_t _minWordLen)
: resultAllocator(_resultAllocator)
{
int32_t startPos = -1;
for (uint32_t pos = 0; pos < _inputStringLen; pos++)
{
if (!IS_DELIMITER(_inputString[pos]))
{
startPos = pos;
break;
}
}
if (startPos >= 0)
{
uint32_t wordPos = startPos;
bool lastCharWasDelim = false;
for (uint32_t pos = startPos + 1; pos < _inputStringLen; pos++)
{
if (IS_DELIMITER(_inputString[pos]))
{
if (!lastCharWasDelim && pos-wordPos >= _minWordLen)
{
wordList.push_back(std::string(&_inputString[wordPos], pos-wordPos));
}
lastCharWasDelim = true;
}
else
{
if (lastCharWasDelim)
{
wordPos = pos;
lastCharWasDelim = false;
}
}
}
if (!lastCharWasDelim && wordPos < _inputStringLen && _inputStringLen-wordPos >= _minWordLen)
{
wordList.push_back(std::string(&_inputString[wordPos], _inputStringLen-wordPos));
}
}
isStopped = (_inputStringLen == 0 || wordList.size() == 0);
minGram = (_minGram > 0 ? _minGram : 1);
maxGram = (_maxGram >= _minGram ? _maxGram : _minGram);
minWordLen = _minWordLen;
currentWord = 0;
currentGram = minGram;
}
RTLIMPLEMENT_IINTERFACE
virtual const void* nextRow()
{
if (isStopped)
{
return NULL;
}
while (currentWord < wordList.size())
{
if (currentGram <= maxGram && currentWord + currentGram <= wordList.size())
{
std::string buffer;
for (unsigned int x = 0; x < currentGram; x++)
{
if (x > 0)
buffer += " ";
buffer += wordList[x + currentWord];
}
RtlDynamicRowBuilder rowBuilder(resultAllocator);
uint32_t len = buffer.length();
uint32_t totalRowSize = sizeof(len) + len;
byte* row = rowBuilder.ensureCapacity(totalRowSize, NULL);
memcpy(row, &len, sizeof(len));
memcpy(row + sizeof(len), buffer.data(), len);
++currentGram;
return rowBuilder.finalizeRowClear(totalRowSize);
}
else
{
++currentWord;
currentGram = minGram;
}
}
isStopped = true;
return NULL;
}
virtual void stop()
{
isStopped = true;
}
protected:
Linked<IEngineRowAllocator> resultAllocator;
private:
bool isStopped;
uint32_t minGram;
uint32_t maxGram;
uint32_t minWordLen;
uint32_t currentWord;
uint32_t currentGram;
std::vector<std::string> wordList;
};
return new StreamDataset(_resultAllocator, s, lenS, min_gram, max_gram, min_word_len);
ENDEMBED;
/**
* Helper function that simplifies the generation of unigrams.
*
* @param s The string to process; REQUIRED
* @param min_word_len The minimum length of a word that will be
* processed; words less than this length will be
* ignored when creating the ngrams; OPTIONAL,
* defaults to 1
*
* @return A dataset containing all unigrams generated from s.
*
* @see GenerateWordNGrams
* @see GenerateWordBigrams
* @see GenerateWordTrigrams
*/
EXPORT STREAMED DATASET(WordNGramLayout) GenerateWordUnigrams(CONST STRING s, UNSIGNED1 min_word_len = 1) := GenerateWordNGrams(s, 1, 1, min_word_len);
/**
* Helper function that simplifies the generation of bigrams.
*
* @param s The string to process; REQUIRED
* @param min_word_len The minimum length of a word that will be
* processed; words less than this length will be
* ignored when creating the ngrams; OPTIONAL,
* defaults to 1
*
* @return A dataset containing all bigrams generated from s.
*
* @see GenerateWordNGrams
* @see GenerateWordUnigrams
* @see GenerateWordTrigrams
*/
EXPORT STREAMED DATASET(WordNGramLayout) GenerateWordBigrams(CONST STRING s, UNSIGNED1 min_word_len = 1) := GenerateWordNGrams(s, 2, 2, min_word_len);
/**
* Helper function that simplifies the generation of trigrams.
*
* @param s The string to process; REQUIRED
* @param min_word_len The minimum length of a word that will be
* processed; words less than this length will be
* ignored when creating the ngrams; OPTIONAL,
* defaults to 1
*
* @return A dataset containing all trigrams generated from s.
*
* @see GenerateWordNGrams
* @see GenerateWordUnigrams
* @see GenerateWordBigrams
*/
EXPORT STREAMED DATASET(WordNGramLayout) GenerateWordTrigrams(CONST STRING s, UNSIGNED1 min_word_len = 1) := GenerateWordNGrams(s, 3, 3, min_word_len);
END;
/**
* Sample calls:
*
* res1 := WordNGrams.GenerateWordNGrams('The quick brown fox jumped over the lazy dog.', 1, 9);
* OUTPUT(res1);
*
* Result:
*
* The
* The quick
* The quick brown
* The quick brown fox
* The quick brown fox jumped
* The quick brown fox jumped over
* The quick brown fox jumped over the
* The quick brown fox jumped over the lazy
* The quick brown fox jumped over the lazy dog
* quick
* quick brown
* quick brown fox
* quick brown fox jumped
* quick brown fox jumped over
* quick brown fox jumped over the
* quick brown fox jumped over the lazy
* quick brown fox jumped over the lazy dog
* brown
* brown fox
* brown fox jumped
* brown fox jumped over
* brown fox jumped over the
* brown fox jumped over the lazy
* brown fox jumped over the lazy dog
* fox
* fox jumped
* fox jumped over
* fox jumped over the
* fox jumped over the lazy
* fox jumped over the lazy dog
* jumped
* jumped over
* jumped over the
* jumped over the lazy
* jumped over the lazy dog
* over
* over the
* over the lazy
* over the lazy dog
* the
* the lazy
* the lazy dog
* lazy
* lazy dog
* dog
*
* res2 := WordNGrams.GenerateWordBigrams('The quick brown fox jumped over the lazy dog.');
* OUTPUT(res2);
*
* Result:
*
* The quick
* quick brown
* brown fox
* fox jumped
* jumped over
* over the
* the lazy
* lazy dog
*/