-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDarPos.js
294 lines (255 loc) · 9.47 KB
/
DarPos.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
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
//https://github.com/dariusk/pos-js
var re = {
ids: /(?:^|\s)[a-z0-9-]{8,45}(?:$|\s)/ig, // ID, CRC, UUID's
number: /[0-9]*\.[0-9]+|[0-9]+/ig,
space: /\s+/ig,
unblank: /\S/,
email: /[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](?:\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](?:-?\.?[a-zA-Z0-9])*(?:\.[a-zA-Z](?:-?[a-zA-Z0-9])*)+/gi,
urls: /(?:https?:\/\/)(?:[\da-z\.-]+)\.(?:[a-z\.]{2,6})(?:[\/\w\.\-\?#=]*)*\/?/ig,
punctuation: /[\/\.\,\?\!\"\'\:\;\$\(\)\#\’\`]/ig,
time: /(?:[0-9]|0[0-9]|1[0-9]|2[0-3]):(?:[0-5][0-9])\s?(?:[aApP][mM])/ig
}
function LexerNode(string, regex, regexs){
string = string.trim();
this.string = string;
this.children = [];
if (string) {
this.matches = string.match(regex);
var childElements = string.split(regex);
}
if (!this.matches) {
this.matches = [];
var childElements = [string];
}
if (!regexs.length) {
// no more regular expressions, we're done
this.children = childElements;
} else {
// descend recursively
var nextRegex = regexs[0], nextRegexes = regexs.slice(1);
for (var i in childElements) {
if (childElements.hasOwnProperty(i)) {
this.children.push(
new LexerNode(childElements[i], nextRegex, nextRegexes));
}
}
}
}
LexerNode.prototype.fillArray = function(array){
for (var i in this.children) {
if (this.children.hasOwnProperty(i)) {
var child = this.children[i];
if (child.fillArray) {
child.fillArray(array);
} else if (re.unblank.test(child)) {
array.push(child.trim());
}
if (i < this.matches.length) {
var match = this.matches[i];
if (re.unblank.test(match))
array.push(match.trim());
}
}
}
}
LexerNode.prototype.toString = function(){
var array = [];
this.fillArray(array);
return array.toString();
}
function Lexer(){
// URLS can contain IDS, so first urls, then ids
// then split by then numbers, then whitespace, then email and finally punctuation
// this.regexs = [re.urls, re.ids, re.number, re.space, re.email, re.punctuation];
this.regexs = [
re.urls, re.ids, re.time, re.number, re.space, re.email, re.punctuation
];
}
Lexer.prototype.lex = function(string){
var array = []
, node = new LexerNode(string, this.regexs[0], this.regexs.slice(1));
node.fillArray(array);
return array;
}
var lexer = new Lexer();
console.log(lexer.lex("I made $5.60 today in 1 hour of work. The E.M.T.'s were on time, but only barely.").toString());
/*
Transformation rules for Brill's POS tagger
Copyright (C) 2015 Hugo W.L. ter Doest
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Usage:
// transformationRules = new BrillTransformationRules();
// transformationRules.rules.forEach(function(ruleFunction) {
// ruleFunction(taggedSentence, i);
// });
// where taggedSentence is an array of arrays of the form:
// [[the, DET], [red, JJ], [book, NN]] and i the position to be processed
function BrillTransformationRules() {
this.rules = [rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8];
}
BrillTransformationRules.prototype.getRule = function(index) {
return(this.rules[index]);
};
BrillTransformationRules.prototype.setRule = function(index, rule) {
this.rules[index] = rule;
};
BrillTransformationRules.prototype.appendRule = function(rule) {
this.rules[this.rules.length] = rule;
};
BrillTransformationRules.prototype.setRules = function(newRules) {
this.rules = newRules;
};
BrillTransformationRules.prototype.getRules = function() {
return(this.rules);
};
/**
* Indicates whether or not this string starts with the specified string.
* @param {Object} string
*/
function startsWith($this, string) {
if (!string) {
return false;
}
return $this.indexOf(string) == 0;
}
/**
* Indicates whether or not this string ends with the specified string.
* @param {Object} string
*/
function endsWith($this, string) {
if (!string || string.length > $this.length) {
return false;
}
return $this.indexOf(string) == $this.length - string.length;
}
// rule 1: DT, {VBD | VBP} --> DT, NN
function rule1(taggedSentence, index) {
if ((index > 0) && (taggedSentence[index - 1][1] === "DT")) {
if ((taggedSentence[index][1] === "VBD") ||
(taggedSentence[index][1] === "VBP") ||
(taggedSentence[index][1] === "VB")) {
taggedSentence[index][1] = "NN";
}
}
}
// rule 2: convert a noun to a number (CD) if "." appears in the word
function rule2(taggedSentence, index) {
if (startsWith(taggedSentence[index][1], "N")) {
if (taggedSentence[index][0].indexOf(".") > -1) {
// url if there are two contiguous alpha characters
if (/[a-zA-Z]{2}/.test(taggedSentence[index][0])) {
taggedSentence[index][1] = "URL";
}
else {
taggedSentence[index][1] = "CD";
}
}
// Attempt to convert into a number
if (!isNaN(parseFloat(taggedSentence[index][0]))) {
taggedSentence[index][1] = "CD";
}
}
}
// rule 3: convert a noun to a past participle if words[i] ends with "ed"
function rule3(taggedSentence, index) {
if (startsWith(taggedSentence[index][1], "N") && endsWith(taggedSentence[index][0], "ed")) {
taggedSentence[index][1] = "VBN";
}
}
// rule 4: convert any type to adverb if it ends in "ly";
function rule4(taggedSentence, index) {
if (endsWith(taggedSentence[index][0], "ly")) {
taggedSentence[index][1] = "RB";
}
}
// rule 5: convert a common noun (NN or NNS) to a adjective if it ends with "al"
function rule5(taggedSentence, index) {
if (startsWith(taggedSentence[index][1], "NN") && endsWith(taggedSentence[index][0], "al")) {
taggedSentence[index][1] = "JJ";
}
}
// rule 6: convert a noun to a verb if the preceding work is "would"
function rule6(taggedSentence, index) {
if ((index > 0) && startsWith(taggedSentence[index][1], "NN") && (taggedSentence[index - 1][0].toLowerCase() === "would")) {
taggedSentence[index][1] = "VB";
}
}
// rule 7: if a word has been categorized as a common noun and it ends with "s",
// then set its type to plural common noun (NNS)
function rule7(taggedSentence, index) {
if ((taggedSentence[index][1] === "NN") && (endsWith(taggedSentence[index][0], "s"))) {
taggedSentence[index][1] = "NNS";
}
}
// rule 8: convert a common noun to a present participle verb (i.e., a gerund)
function rule8(taggedSentence, index) {
if (startsWith(taggedSentence[index][1], "NN") && endsWith(taggedSentence[index][0], "ing")) {
taggedSentence[index][1] = "VBG";
}
}
var transformationRules = new BrillTransformationRules();
function POSTagger(){
this.lexicon = window.Lexicon;
}
POSTagger.prototype.wordInLexicon = function(word){
var ss = this.lexicon[word];
if (ss != null)
return true;
// 1/22/2002 mod (from Lisp code): if not in hash, try lower case:
if (!ss)
ss = this.lexicon[word.toLowerCase()];
if (ss)
return true;
return false;
}
POSTagger.prototype.tag = function(words) {
var taggedSentence = new Array(words.length);
// Initialise taggedSentence with words and initial categories
for (var i = 0, size = words.length; i < size; i++) {
taggedSentence[i] = new Array(2);
taggedSentence[i][0] = words[i];
// lexicon maps a word to an array of possible categories
var ss = this.lexicon[words[i]];
// 1/22/2002 mod (from Lisp code): if not in hash, try lower case:
if (!ss)
ss = this.lexicon[words[i].toLowerCase()];
if (!ss && (words[i].length === 1))
taggedSentence[i][1] = words[i] + "^";
// We need to catch scenarios where we pass things on the prototype
// that aren't in the lexicon: "constructor" breaks this otherwise
if (!ss || (Object.prototype.toString.call(ss) !== '[object Array]'))
taggedSentence[i][1] = "NN";
else
taggedSentence[i][1] = ss[0];
}
// Apply transformation rules
taggedSentence.forEach(function(taggedWord, index) {
transformationRules.getRules().forEach(function(rule) {
rule(taggedSentence, index);
});
});
return taggedSentence;
}
POSTagger.prototype.prettyPrint = function(taggedWords) {
for (i in taggedWords) {
print(taggedWords[i][0] + "(" + taggedWords[i][1] + ")");
}
}
POSTagger.prototype.extendLexicon = function(lexicon) {
for (var word in lexicon) {
if (!this.lexicon.hasOwnProperty(word)) {
this.lexicon[word] = lexicon[word];
}
}
}
console.log(new POSTagger().tag(["i", "went", "to", "the", "store", "to", "buy", "5.2", "gallons", "of", "milk"]));