thera 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.document +5 -0
  2. data/.gitignore +56 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +20 -0
  5. data/LICENSE.txt +1 -0
  6. data/README.rdoc +8 -0
  7. data/Rakefile +1 -0
  8. data/ext/Makefile +225 -0
  9. data/ext/extconf.rb +29 -0
  10. data/ext/quarry/quarry_toolkit.cpp +148 -0
  11. data/lib/quarry/Makefile.linux +2 -0
  12. data/lib/quarry/Makefile.osx +6 -0
  13. data/lib/quarry/Makefile.targets +23 -0
  14. data/lib/quarry/obj/.gitkeep +0 -0
  15. data/lib/quarry/src/classifier/aode/aode_classifier.cpp +0 -0
  16. data/lib/quarry/src/classifier/aode/aode_classifier.h +0 -0
  17. data/lib/quarry/src/classifier/centroid/centroid_classifier.cpp +0 -0
  18. data/lib/quarry/src/classifier/centroid/centroid_classifier.h +0 -0
  19. data/lib/quarry/src/classifier/classifier.cpp +32 -0
  20. data/lib/quarry/src/classifier/classifier.h +59 -0
  21. data/lib/quarry/src/classifier/knn/knn_classifier.cpp +0 -0
  22. data/lib/quarry/src/classifier/knn/knn_classifier.h +0 -0
  23. data/lib/quarry/src/classifier/multinomial_bayes/multinomial_bayes_classifier.cpp +40 -0
  24. data/lib/quarry/src/classifier/multinomial_bayes/multinomial_bayes_classifier.h +18 -0
  25. data/lib/quarry/src/classifier/naive_bayes/naive_bayes_classifier.cpp +80 -0
  26. data/lib/quarry/src/classifier/naive_bayes/naive_bayes_classifier.h +52 -0
  27. data/lib/quarry/src/data_set/data_set.cpp +130 -0
  28. data/lib/quarry/src/data_set/data_set.h +78 -0
  29. data/lib/quarry/src/data_set/dense/dense_data_set.h +39 -0
  30. data/lib/quarry/src/data_set/dense/dense_example.h +44 -0
  31. data/lib/quarry/src/data_set/example.cpp +10 -0
  32. data/lib/quarry/src/data_set/example.h +23 -0
  33. data/lib/quarry/src/data_set/feature.h +36 -0
  34. data/lib/quarry/src/data_set/features/nominal_feature.cpp +57 -0
  35. data/lib/quarry/src/data_set/features/nominal_feature.h +76 -0
  36. data/lib/quarry/src/data_set/features/numeric_feature.cpp +69 -0
  37. data/lib/quarry/src/data_set/features/numeric_feature.h +78 -0
  38. data/lib/quarry/src/data_set/sparse/sparse_data_set.h +40 -0
  39. data/lib/quarry/src/data_set/sparse/sparse_example.cpp +82 -0
  40. data/lib/quarry/src/data_set/sparse/sparse_example.h +38 -0
  41. data/lib/quarry/src/metrics/confusion_matrix.cpp +129 -0
  42. data/lib/quarry/src/metrics/confusion_matrix.h +82 -0
  43. data/lib/quarry/src/model/model.cpp +29 -0
  44. data/lib/quarry/src/model/model.h +50 -0
  45. data/lib/quarry/src/preprocessing/examples/example_preprocessor.h +20 -0
  46. data/lib/quarry/src/preprocessing/examples/weights/binary_weight.h +20 -0
  47. data/lib/quarry/src/preprocessing/examples/weights/local_weight.h +29 -0
  48. data/lib/quarry/src/preprocessing/text/example_generator/example_generator.h +19 -0
  49. data/lib/quarry/src/preprocessing/text/example_generator/token_counter.h +59 -0
  50. data/lib/quarry/src/preprocessing/text/inplace_processor/downcase.h +26 -0
  51. data/lib/quarry/src/preprocessing/text/inplace_processor/inplace_processor.h +17 -0
  52. data/lib/quarry/src/preprocessing/text/inplace_processor/porter_stemmer.h +44 -0
  53. data/lib/quarry/src/preprocessing/text/inplace_processor/porter_stemmer_original.cpp +375 -0
  54. data/lib/quarry/src/preprocessing/text/text_pipeline.cpp +29 -0
  55. data/lib/quarry/src/preprocessing/text/text_pipeline.h +37 -0
  56. data/lib/quarry/src/preprocessing/text/token_selector/pos_tag_selector.h +21 -0
  57. data/lib/quarry/src/preprocessing/text/token_selector/stop_words.cpp +82 -0
  58. data/lib/quarry/src/preprocessing/text/token_selector/stop_words.h +20 -0
  59. data/lib/quarry/src/preprocessing/text/token_selector/token_selector.h +17 -0
  60. data/lib/quarry/src/preprocessing/text/tokeniser/simple_tokeniser.cpp +29 -0
  61. data/lib/quarry/src/preprocessing/text/tokeniser/simple_tokeniser.h +20 -0
  62. data/lib/quarry/src/preprocessing/text/tokeniser/tokeniser.h +19 -0
  63. data/lib/quarry/src/quarry.cpp +1 -0
  64. data/lib/quarry/src/quarry.h +29 -0
  65. data/lib/quarry/src/storage/arff.cpp +198 -0
  66. data/lib/quarry/src/storage/arff.h +26 -0
  67. data/lib/quarry/src/storage/binary.cpp +457 -0
  68. data/lib/quarry/src/storage/binary.h +79 -0
  69. data/lib/quarry/src/storage/folders.cpp +98 -0
  70. data/lib/quarry/src/storage/folders.h +25 -0
  71. data/lib/quarry/src/storage/storage.h +19 -0
  72. data/lib/quarry/src/test.cpp +6 -0
  73. data/lib/quarry_rb/classifier/classifier.rb +22 -0
  74. data/lib/quarry_rb/classifier/naive_bayes_classifier.rb +10 -0
  75. data/lib/quarry_rb/confusion_matrix.rb +58 -0
  76. data/lib/quarry_rb/data_set/data_set.rb +42 -0
  77. data/lib/quarry_rb/data_set/example.rb +33 -0
  78. data/lib/quarry_rb/data_set/feature.rb +28 -0
  79. data/lib/quarry_rb/enumerable_helper.rb +32 -0
  80. data/lib/quarry_rb/model/model.rb +56 -0
  81. data/lib/quarry_rb/storage/arff.rb +11 -0
  82. data/lib/quarry_rb/storage/binary.rb +23 -0
  83. data/lib/quarry_rb/storage/folders.rb +11 -0
  84. data/lib/quarry_rb/text_pipeline.rb +16 -0
  85. data/lib/thera.rb +20 -0
  86. data/test/helper.rb +19 -0
  87. data/test/test_quarry.rb +33 -0
  88. data/thera.gemspec +21 -0
  89. metadata +148 -0
@@ -0,0 +1,20 @@
1
+ #ifndef __binary_weight_h__
2
+ #define __binary_weight_h__
3
+ #include "preprocessing/examples/example_preprocessor.h"
4
+
5
+ namespace Preprocessing {
6
+ namespace Examples {
7
+
8
+ class BinaryWeight : public ExamplePreprocessor {
9
+ void process(DataSet::Example *example) {
10
+ for(int i = 0; i < example->size; i++) {
11
+ if(example->get_value(i) != 0.0)
12
+ example->set_value(i, 1.0);
13
+ }
14
+ }
15
+ };
16
+
17
+ }
18
+ }
19
+
20
+ #endif
@@ -0,0 +1,29 @@
1
+ #ifndef __local_weight_h__
2
+ #define __local_weight_h__
3
+ #include "preprocessing/examples/example_preprocessor.h"
4
+
5
+ namespace Preprocessing {
6
+ namespace Examples {
7
+
8
+ class LocalWeight : public ExamplePreprocessor {
9
+ void process(DataSet::Example *example) {
10
+ int max_value = 0;
11
+ double value;
12
+
13
+ for(int i = 0; i < example->size; i++) {
14
+ value = example->get_value(i);
15
+ if(value > max_value)
16
+ max_value = value;
17
+ }
18
+
19
+ for(int i = 0; i < example->size; i++) {
20
+ value = example->get_value(i);
21
+ example->set_value(i, value / max_value);
22
+ }
23
+ }
24
+ };
25
+
26
+ }
27
+ }
28
+
29
+ #endif
@@ -0,0 +1,19 @@
1
+ #ifndef __example_generator_h__
2
+ #define __example_generator_h__
3
+ #include "data_set/sparse/sparse_data_set.h"
4
+ #include "data_set/sparse/sparse_example.h"
5
+
6
+ namespace Preprocessing {
7
+ namespace Text {
8
+
9
+ class ExampleGenerator {
10
+ public:
11
+ ExampleGenerator() {}
12
+ virtual DataSet::SparseExample *generate(DataSet::SparseDataSet *data_set, vector<char *> *tokens) { return NULL; }
13
+ virtual uint32_t mark() = 0;
14
+ };
15
+
16
+ }
17
+ }
18
+
19
+ #endif
@@ -0,0 +1,59 @@
1
+ #ifndef __token_counter_h__
2
+ #define __token_counter_h__
3
+ #include "example_generator.h"
4
+ #include <map>
5
+
6
+ namespace Preprocessing {
7
+ namespace Text {
8
+
9
+ class TokenCounter : public ExampleGenerator {
10
+ public:
11
+ static const uint32_t file_mark = 'tcou';
12
+ uint32_t mark() { return file_mark; }
13
+
14
+ typedef enum {
15
+ Count,
16
+ Local,
17
+ Binary
18
+ } TokenCounterWeight;
19
+
20
+ map<string, int> token_counts;
21
+ TokenCounterWeight weight;
22
+
23
+ TokenCounter(TokenCounterWeight weight = Count) : ExampleGenerator(), token_counts(), weight(weight) {}
24
+
25
+ DataSet::SparseExample *generate(DataSet::SparseDataSet *data_set, vector<char *> *tokens) {
26
+ int max_count = 0, count = 0;
27
+ double value = 0.0;
28
+ token_counts.clear();
29
+ string token;
30
+
31
+ // count the number of occurrences of each token
32
+ for(vector<char *>::iterator tokens_it = tokens->begin(); tokens_it != tokens->end(); tokens_it++) {
33
+ token = string(*tokens_it);
34
+ count = ++token_counts[token];
35
+ if(count > max_count)
36
+ max_count = count;
37
+ }
38
+
39
+ // construct the example
40
+ DataSet::SparseExample *example = data_set->new_example(token_counts.size());
41
+ for(map<string, int>::iterator token_counts_it = token_counts.begin(); token_counts_it != token_counts.end(); token_counts_it++) {
42
+ value = token_counts_it->second;
43
+
44
+ if(weight == Local)
45
+ value = value / max_count;
46
+ else if(weight == Binary)
47
+ value = 1;
48
+
49
+ example->set_value(data_set->get_or_create_numeric_feature_by_name(token_counts_it->first)->index, value);
50
+ }
51
+
52
+ return example;
53
+ }
54
+ };
55
+
56
+ }
57
+ }
58
+
59
+ #endif
@@ -0,0 +1,26 @@
1
+ #ifndef __downcase_h__
2
+ #define __dowmcase_h__
3
+ #include "inplace_processor.h"
4
+ #include <cctype>
5
+
6
+ namespace Preprocessing {
7
+ namespace Text {
8
+
9
+ class Downcase : public InplaceProcessor {
10
+ public:
11
+ static const uint32_t file_mark = 'down';
12
+ uint32_t mark() { return file_mark; }
13
+
14
+ char *process(char *start, char *end) {
15
+ while(start != end) {
16
+ *start = tolower(*start);
17
+ start++;
18
+ }
19
+ return end;
20
+ }
21
+ };
22
+
23
+ }
24
+ }
25
+
26
+ #endif
@@ -0,0 +1,17 @@
1
+ #ifndef __inplace_processor_h__
2
+ #define __inplace_processor_h__
3
+
4
+ namespace Preprocessing {
5
+ namespace Text {
6
+
7
+ class InplaceProcessor {
8
+ public:
9
+ InplaceProcessor() {}
10
+ virtual char *process(char *start, char *end) { return end; }
11
+ virtual uint32_t mark() = 0;
12
+ };
13
+
14
+ }
15
+ }
16
+
17
+ #endif
@@ -0,0 +1,44 @@
1
+ #ifndef __porter_stemmer_h__
2
+ #define __porter_stemmer_h__
3
+ #include "inplace_processor.h"
4
+
5
+ // from porter_stemmer_original.c
6
+ extern "C" {
7
+ struct stemmer;
8
+ extern struct stemmer * create_stemmer(void);
9
+ extern void free_stemmer(struct stemmer * z);
10
+ extern int stem(struct stemmer * z, char * b, int k);
11
+ }
12
+
13
+ namespace Preprocessing {
14
+ namespace Text {
15
+
16
+ class PorterStemmer : public InplaceProcessor {
17
+ public:
18
+ static const uint32_t file_mark = 'port';
19
+ uint32_t mark() { return file_mark; }
20
+
21
+ struct stemmer *stemm;
22
+ PorterStemmer() : InplaceProcessor() {
23
+ stemm = create_stemmer();
24
+ }
25
+
26
+ ~PorterStemmer() {
27
+ free_stemmer(stemm);
28
+ }
29
+
30
+ char *process(char *start, char *end) {
31
+ int length = end - start;
32
+ int new_length = stem(stemm, start, end - start - 1);
33
+
34
+ for(int i = new_length + 1; i <= length; i++)
35
+ start[i] = 0;
36
+
37
+ return start + new_length;
38
+ }
39
+ };
40
+
41
+ }
42
+ }
43
+
44
+ #endif
@@ -0,0 +1,375 @@
1
+
2
+ /* This is the Porter stemming algorithm, coded up as thread-safe ANSI C
3
+ by the author.
4
+
5
+ It may be be regarded as cononical, in that it follows the algorithm
6
+ presented in
7
+
8
+ Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
9
+ no. 3, pp 130-137,
10
+
11
+ only differing from it at the points maked --DEPARTURE-- below.
12
+
13
+ See also http://www.tartarus.org/~martin/PorterStemmer
14
+
15
+ The algorithm as described in the paper could be exactly replicated
16
+ by adjusting the points of DEPARTURE, but this is barely necessary,
17
+ because (a) the points of DEPARTURE are definitely improvements, and
18
+ (b) no encoding of the Porter stemmer I have seen is anything like
19
+ as exact as this version, even with the points of DEPARTURE!
20
+
21
+ You can compile it on Unix with 'gcc -O3 -o stem stem.c' after which
22
+ 'stem' takes a list of inputs and sends the stemmed equivalent to
23
+ stdout.
24
+
25
+ The algorithm as encoded here is particularly fast.
26
+
27
+ Release 2 (the more old-fashioned, non-thread-safe version may be
28
+ regarded as release 1.)
29
+ */
30
+
31
+ #include <stdlib.h> /* for malloc, free */
32
+ #include <string.h> /* for memcmp, memmove */
33
+ #include <iostream>
34
+
35
+ /* The main part of the stemming algorithm starts here.
36
+ */
37
+
38
+ #define TRUE 1
39
+ #define FALSE 0
40
+
41
+ extern "C" {
42
+
43
+ /* stemmer is a structure for a few local bits of data,
44
+ */
45
+
46
+ struct stemmer {
47
+ char * b; /* buffer for word to be stemmed */
48
+ int k; /* offset to the end of the string */
49
+ int j; /* a general offset into the string */
50
+ };
51
+
52
+
53
+ /* Member b is a buffer holding a word to be stemmed. The letters are in
54
+ b[0], b[1] ... ending at b[z->k]. Member k is readjusted downwards as
55
+ the stemming progresses. Zero termination is not in fact used in the
56
+ algorithm.
57
+
58
+ Note that only lower case sequences are stemmed. Forcing to lower case
59
+ should be done before stem(...) is called.
60
+
61
+
62
+ Typical usage is:
63
+
64
+ struct stemmer * z = create_stemmer();
65
+ char b[] = "pencils";
66
+ int res = stem(z, b, 6);
67
+ /- stem the 7 characters of b[0] to b[6]. The result, res,
68
+ will be 5 (the 's' is removed). -/
69
+ free_stemmer(z);
70
+ */
71
+
72
+
73
+ extern struct stemmer * create_stemmer(void)
74
+ {
75
+ return (struct stemmer *) malloc(sizeof(struct stemmer));
76
+ /* assume malloc succeeds */
77
+ }
78
+
79
+ extern void free_stemmer(struct stemmer * z)
80
+ {
81
+ free(z);
82
+ }
83
+
84
+
85
+ /* cons(z, i) is TRUE <=> b[i] is a consonant. ('b' means 'z->b', but here
86
+ and below we drop 'z->' in comments.
87
+ */
88
+
89
+ static int cons(struct stemmer * z, int i)
90
+ { switch (z->b[i])
91
+ { case 'a': case 'e': case 'i': case 'o': case 'u': return FALSE;
92
+ case 'y': return (i == 0) ? TRUE : !cons(z, i - 1);
93
+ default: return TRUE;
94
+ }
95
+ }
96
+
97
+ /* m(z) measures the number of consonant sequences between 0 and j. if c is
98
+ a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
99
+ presence,
100
+
101
+ <c><v> gives 0
102
+ <c>vc<v> gives 1
103
+ <c>vcvc<v> gives 2
104
+ <c>vcvcvc<v> gives 3
105
+ ....
106
+ */
107
+
108
+ static int m(struct stemmer * z)
109
+ { int n = 0;
110
+ int i = 0;
111
+ int j = z->j;
112
+ while(TRUE)
113
+ { if (i > j) return n;
114
+ if (! cons(z, i)) break; i++;
115
+ }
116
+ i++;
117
+ while(TRUE)
118
+ { while(TRUE)
119
+ { if (i > j) return n;
120
+ if (cons(z, i)) break;
121
+ i++;
122
+ }
123
+ i++;
124
+ n++;
125
+ while(TRUE)
126
+ { if (i > j) return n;
127
+ if (! cons(z, i)) break;
128
+ i++;
129
+ }
130
+ i++;
131
+ }
132
+ }
133
+
134
+ /* vowelinstem(z) is TRUE <=> 0,...j contains a vowel */
135
+
136
+ static int vowelinstem(struct stemmer * z)
137
+ {
138
+ int j = z->j;
139
+ int i; for (i = 0; i <= j; i++) if (! cons(z, i)) return TRUE;
140
+ return FALSE;
141
+ }
142
+
143
+ /* doublec(z, j) is TRUE <=> j,(j-1) contain a double consonant. */
144
+
145
+ static int doublec(struct stemmer * z, int j)
146
+ {
147
+ char * b = z->b;
148
+ if (j < 1) return FALSE;
149
+ if (b[j] != b[j - 1]) return FALSE;
150
+ return cons(z, j);
151
+ }
152
+
153
+ /* cvc(z, i) is TRUE <=> i-2,i-1,i has the form consonant - vowel - consonant
154
+ and also if the second c is not w,x or y. this is used when trying to
155
+ restore an e at the end of a short word. e.g.
156
+
157
+ cav(e), lov(e), hop(e), crim(e), but
158
+ snow, box, tray.
159
+
160
+ */
161
+
162
+ static int cvc(struct stemmer * z, int i)
163
+ { if (i < 2 || !cons(z, i) || cons(z, i - 1) || !cons(z, i - 2)) return FALSE;
164
+ { int ch = z->b[i];
165
+ if (ch == 'w' || ch == 'x' || ch == 'y') return FALSE;
166
+ }
167
+ return TRUE;
168
+ }
169
+
170
+ /* ends(z, s) is TRUE <=> 0,...k ends with the string s. */
171
+
172
+ static int ends(struct stemmer * z, char * s)
173
+ { int length = s[0];
174
+ char * b = z->b;
175
+ int k = z->k;
176
+ if (s[length] != b[k]) return FALSE; /* tiny speed-up */
177
+ if (length > k + 1) return FALSE;
178
+ if (memcmp(b + k - length + 1, s + 1, length) != 0) return FALSE;
179
+ z->j = k-length;
180
+ return TRUE;
181
+ }
182
+
183
+ /* setto(z, s) sets (j+1),...k to the characters in the string s, readjusting
184
+ k. */
185
+
186
+ static void setto(struct stemmer * z, char * s)
187
+ { int length = s[0];
188
+ int j = z->j;
189
+ memmove(z->b + j + 1, s + 1, length);
190
+ z->k = j+length;
191
+ }
192
+
193
+ /* r(z, s) is used further down. */
194
+
195
+ static void r(struct stemmer * z, char * s) { if (m(z) > 0) setto(z, s); }
196
+
197
+ /* step1ab(z) gets rid of plurals and -ed or -ing. e.g.
198
+
199
+ caresses -> caress
200
+ ponies -> poni
201
+ ties -> ti
202
+ caress -> caress
203
+ cats -> cat
204
+
205
+ feed -> feed
206
+ agreed -> agree
207
+ disabled -> disable
208
+
209
+ matting -> mat
210
+ mating -> mate
211
+ meeting -> meet
212
+ milling -> mill
213
+ messing -> mess
214
+
215
+ meetings -> meet
216
+
217
+ */
218
+
219
+ static void step1ab(struct stemmer * z)
220
+ {
221
+ char * b = z->b;
222
+ if (b[z->k] == 's')
223
+ { if (ends(z, "\04" "sses")) z->k -= 2; else
224
+ if (ends(z, "\03" "ies")) setto(z, "\01" "i"); else
225
+ if (b[z->k - 1] != 's') z->k--;
226
+ }
227
+ if (ends(z, "\03" "eed")) { if (m(z) > 0) z->k--; } else
228
+ if ((ends(z, "\02" "ed") || ends(z, "\03" "ing")) && vowelinstem(z))
229
+ { z->k = z->j;
230
+ if (ends(z, "\02" "at")) setto(z, "\03" "ate"); else
231
+ if (ends(z, "\02" "bl")) setto(z, "\03" "ble"); else
232
+ if (ends(z, "\02" "iz")) setto(z, "\03" "ize"); else
233
+ if (doublec(z, z->k))
234
+ { z->k--;
235
+ { int ch = b[z->k];
236
+ if (ch == 'l' || ch == 's' || ch == 'z') z->k++;
237
+ }
238
+ }
239
+ else if (m(z) == 1 && cvc(z, z->k)) setto(z, "\01" "e");
240
+ }
241
+ }
242
+
243
+ /* step1c(z) turns terminal y to i when there is another vowel in the stem. */
244
+
245
+ static void step1c(struct stemmer * z)
246
+ {
247
+ if (ends(z, "\01" "y") && vowelinstem(z)) z->b[z->k] = 'i';
248
+ }
249
+
250
+
251
+ /* step2(z) maps double suffices to single ones. so -ization ( = -ize plus
252
+ -ation) maps to -ize etc. note that the string before the suffix must give
253
+ m(z) > 0. */
254
+
255
+ static void step2(struct stemmer * z) { switch (z->b[z->k-1])
256
+ {
257
+ case 'a': if (ends(z, "\07" "ational")) { r(z, "\03" "ate"); break; }
258
+ if (ends(z, "\06" "tional")) { r(z, "\04" "tion"); break; }
259
+ break;
260
+ case 'c': if (ends(z, "\04" "enci")) { r(z, "\04" "ence"); break; }
261
+ if (ends(z, "\04" "anci")) { r(z, "\04" "ance"); break; }
262
+ break;
263
+ case 'e': if (ends(z, "\04" "izer")) { r(z, "\03" "ize"); break; }
264
+ break;
265
+ case 'l': if (ends(z, "\03" "bli")) { r(z, "\03" "ble"); break; } /*-DEPARTURE-*/
266
+
267
+ /* To match the published algorithm, replace this line with
268
+ case 'l': if (ends(z, "\04" "abli")) { r(z, "\04" "able"); break; } */
269
+
270
+ if (ends(z, "\04" "alli")) { r(z, "\02" "al"); break; }
271
+ if (ends(z, "\05" "entli")) { r(z, "\03" "ent"); break; }
272
+ if (ends(z, "\03" "eli")) { r(z, "\01" "e"); break; }
273
+ if (ends(z, "\05" "ousli")) { r(z, "\03" "ous"); break; }
274
+ break;
275
+ case 'o': if (ends(z, "\07" "ization")) { r(z, "\03" "ize"); break; }
276
+ if (ends(z, "\05" "ation")) { r(z, "\03" "ate"); break; }
277
+ if (ends(z, "\04" "ator")) { r(z, "\03" "ate"); break; }
278
+ break;
279
+ case 's': if (ends(z, "\05" "alism")) { r(z, "\02" "al"); break; }
280
+ if (ends(z, "\07" "iveness")) { r(z, "\03" "ive"); break; }
281
+ if (ends(z, "\07" "fulness")) { r(z, "\03" "ful"); break; }
282
+ if (ends(z, "\07" "ousness")) { r(z, "\03" "ous"); break; }
283
+ break;
284
+ case 't': if (ends(z, "\05" "aliti")) { r(z, "\02" "al"); break; }
285
+ if (ends(z, "\05" "iviti")) { r(z, "\03" "ive"); break; }
286
+ if (ends(z, "\06" "biliti")) { r(z, "\03" "ble"); break; }
287
+ break;
288
+ case 'g': if (ends(z, "\04" "logi")) { r(z, "\03" "log"); break; } /*-DEPARTURE-*/
289
+
290
+ /* To match the published algorithm, delete this line */
291
+
292
+ } }
293
+
294
+ /* step3(z) deals with -ic-, -full, -ness etc. similar strategy to step2. */
295
+
296
+ static void step3(struct stemmer * z) { switch (z->b[z->k])
297
+ {
298
+ case 'e': if (ends(z, "\05" "icate")) { r(z, "\02" "ic"); break; }
299
+ if (ends(z, "\05" "ative")) { r(z, "\00" ""); break; }
300
+ if (ends(z, "\05" "alize")) { r(z, "\02" "al"); break; }
301
+ break;
302
+ case 'i': if (ends(z, "\05" "iciti")) { r(z, "\02" "ic"); break; }
303
+ break;
304
+ case 'l': if (ends(z, "\04" "ical")) { r(z, "\02" "ic"); break; }
305
+ if (ends(z, "\03" "ful")) { r(z, "\00" ""); break; }
306
+ break;
307
+ case 's': if (ends(z, "\04" "ness")) { r(z, "\00" ""); break; }
308
+ break;
309
+ } }
310
+
311
+ /* step4(z) takes off -ant, -ence etc., in context <c>vcvc<v>. */
312
+
313
+ static void step4(struct stemmer * z)
314
+ { switch (z->b[z->k-1])
315
+ { case 'a': if (ends(z, "\02" "al")) break; return;
316
+ case 'c': if (ends(z, "\04" "ance")) break;
317
+ if (ends(z, "\04" "ence")) break; return;
318
+ case 'e': if (ends(z, "\02" "er")) break; return;
319
+ case 'i': if (ends(z, "\02" "ic")) break; return;
320
+ case 'l': if (ends(z, "\04" "able")) break;
321
+ if (ends(z, "\04" "ible")) break; return;
322
+ case 'n': if (ends(z, "\03" "ant")) break;
323
+ if (ends(z, "\05" "ement")) break;
324
+ if (ends(z, "\04" "ment")) break;
325
+ if (ends(z, "\03" "ent")) break; return;
326
+ case 'o': if (ends(z, "\03" "ion") && (z->b[z->j] == 's' || z->b[z->j] == 't')) break;
327
+ if (ends(z, "\02" "ou")) break; return;
328
+ /* takes care of -ous */
329
+ case 's': if (ends(z, "\03" "ism")) break; return;
330
+ case 't': if (ends(z, "\03" "ate")) break;
331
+ if (ends(z, "\03" "iti")) break; return;
332
+ case 'u': if (ends(z, "\03" "ous")) break; return;
333
+ case 'v': if (ends(z, "\03" "ive")) break; return;
334
+ case 'z': if (ends(z, "\03" "ize")) break; return;
335
+ default: return;
336
+ }
337
+ if (m(z) > 1) z->k = z->j;
338
+ }
339
+
340
+ /* step5(z) removes a final -e if m(z) > 1, and changes -ll to -l if
341
+ m(z) > 1. */
342
+
343
+ static void step5(struct stemmer * z)
344
+ {
345
+ char * b = z->b;
346
+ z->j = z->k;
347
+ if (b[z->k] == 'e')
348
+ { int a = m(z);
349
+ if ((a > 1) || (a == 1 && !cvc(z, z->k - 1))) z->k--;
350
+ }
351
+ if (b[z->k] == 'l' && doublec(z, z->k) && m(z) > 1) z->k--;
352
+ }
353
+
354
+ /* In stem(z, b, k), b is a char pointer, and the string to be stemmed is
355
+ from b[0] to b[k] inclusive. Possibly b[k+1] == '\0', but it is not
356
+ important. The stemmer adjusts the characters b[0] ... b[k] and returns
357
+ the new end-point of the string, k'. Stemming never increases word
358
+ length, so 0 <= k' <= k.
359
+ */
360
+
361
+ extern int stem(struct stemmer * z, char * b, int k)
362
+ {
363
+ if (k <= 1) return k; /*-DEPARTURE-*/
364
+ z->b = b; z->k = k; /* copy the parameters into z */
365
+
366
+ /* With this line, strings of length 1 or 2 don't go through the
367
+ stemming process, although no mention is made of this in the
368
+ published algorithm. Remove the line to match the published
369
+ algorithm. */
370
+
371
+ step1ab(z); step1c(z); step2(z); step3(z); step4(z); step5(z);
372
+ return z->k;
373
+ }
374
+
375
+ } // extern "C"
@@ -0,0 +1,29 @@
1
+ #include "text_pipeline.h"
2
+ #include <iostream>
3
+
4
+ DataSet::SparseExample *Preprocessing::Text::TextPipeline::process_text(DataSet::SparseDataSet *data_set, char *text) {
5
+ tokens.clear();
6
+ tokeniser->tokenise(text);
7
+ return generator->generate(data_set, &tokens);
8
+ }
9
+
10
+ void Preprocessing::Text::TextPipeline::process_token(char *start, char *end) {
11
+ for(int i = 0; i < processors.size(); i++)
12
+ processors[i]->process(start, end);
13
+
14
+ for(int i = 0; i < selectors.size(); i++)
15
+ if(!selectors[i]->select(start, end))
16
+ return;
17
+
18
+ tokens.push_back(start);
19
+ }
20
+
21
+ Preprocessing::Text::TextPipeline *Preprocessing::Text::StandardPipeline() {
22
+ TextPipeline *pipeline = new TextPipeline();
23
+ pipeline->tokeniser = new SimpleTokeniser(pipeline);
24
+ pipeline->processors.push_back(new Downcase());
25
+ pipeline->processors.push_back(new PorterStemmer());
26
+ pipeline->selectors.push_back(new StopWords());
27
+ pipeline->generator = new TokenCounter(TokenCounter::Local);
28
+ return pipeline;
29
+ }
@@ -0,0 +1,37 @@
1
+ #ifndef __text_pipeline_h__
2
+ #define __text_pipeline_h__
3
+ #include "data_set/sparse/sparse_example.h"
4
+ #include "example_generator/example_generator.h"
5
+ #include "example_generator/token_counter.h"
6
+ #include "inplace_processor/inplace_processor.h"
7
+ #include "inplace_processor/downcase.h"
8
+ #include "inplace_processor/porter_stemmer.h"
9
+ #include "token_selector/token_selector.h"
10
+ #include "token_selector/stop_words.h"
11
+ #include "token_selector/pos_tag_selector.h"
12
+ #include "tokeniser/tokeniser.h"
13
+ #include "tokeniser/simple_tokeniser.h"
14
+ #include <vector>
15
+
16
+ namespace Preprocessing {
17
+ namespace Text {
18
+ class Tokeniser;
19
+
20
+ class TextPipeline {
21
+ public:
22
+ Tokeniser *tokeniser;
23
+ vector<InplaceProcessor *> processors;
24
+ vector<TokenSelector *> selectors;
25
+ ExampleGenerator *generator;
26
+ vector<char *> tokens;
27
+
28
+ TextPipeline() : tokeniser(NULL), processors(), selectors(), generator(NULL), tokens() {}
29
+ DataSet::SparseExample *process_text(DataSet::SparseDataSet *data_set, char *text);
30
+ void process_token(char *start, char *end);
31
+ };
32
+
33
+ TextPipeline *StandardPipeline();
34
+ }
35
+ }
36
+
37
+ #endif
@@ -0,0 +1,21 @@
1
+ #ifndef __pos_tag_selector_h__
2
+ #define __pos_tag_selector_h__
3
+ #include "token_selector.h"
4
+
5
+ namespace Preprocessing {
6
+ namespace Text {
7
+
8
+ class POSTagSelector : public TokenSelector {
9
+ public:
10
+ static const uint32_t file_mark = 'post';
11
+ uint32_t mark() { return file_mark; }
12
+
13
+ bool select(char *start, char *end) {
14
+ return true;
15
+ }
16
+ };
17
+
18
+ }
19
+ }
20
+
21
+ #endif