engtagger 0.1.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/{LICENSE.txt → LICENSE} +22 -0
- data/README.md +152 -0
- data/Rakefile +2 -24
- data/engtagger.gemspec +17 -0
- data/lib/engtagger/porter.rb +2 -6
- data/lib/engtagger/pos_tags.hash +0 -0
- data/lib/engtagger/pos_words.hash +0 -0
- data/lib/engtagger/version.rb +3 -0
- data/lib/engtagger.rb +831 -729
- data/test/test_engtagger.rb +39 -2
- metadata +39 -65
- data/History.txt +0 -10
- data/Manifest.txt +0 -13
- data/README.txt +0 -140
data/lib/engtagger.rb
CHANGED
@@ -1,729 +1,831 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require '
|
6
|
-
require '
|
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
|
-
return
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
NN = get_ext('nn[sp]*')
|
63
|
-
NNP = get_ext('nnp')
|
64
|
-
PREP = get_ext('in')
|
65
|
-
DET = get_ext('det')
|
66
|
-
PAREN = get_ext('[lr]rb')
|
67
|
-
QUOT = get_ext('ppr')
|
68
|
-
SEN = get_ext('pp')
|
69
|
-
WORD = get_ext('\w+')
|
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
|
-
# => (String)
|
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
|
-
end
|
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
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
end
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
end
|
411
|
-
|
412
|
-
#
|
413
|
-
#
|
414
|
-
def
|
415
|
-
return
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
#
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
text = text.gsub(
|
548
|
-
text = text.gsub(
|
549
|
-
text = text.gsub(
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
#
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), 'engtagger')
|
5
|
+
require 'rubygems'
|
6
|
+
require 'kconv'
|
7
|
+
require 'porter'
|
8
|
+
|
9
|
+
# use hpricot for extracting English text from docs with XML like tags
|
10
|
+
begin
|
11
|
+
require 'hpricot'
|
12
|
+
rescue LoadError
|
13
|
+
$no_hpricot = true
|
14
|
+
end
|
15
|
+
|
16
|
+
# File paths
|
17
|
+
$lexpath = File.join(File.dirname(__FILE__), 'engtagger')
|
18
|
+
$word_path = File.join($lexpath, "pos_words.hash")
|
19
|
+
$tag_path = File.join($lexpath, "pos_tags.hash")
|
20
|
+
|
21
|
+
# for memoization (code snipet from http://eigenclass.org/hiki/bounded-space-memoization)
|
22
|
+
class Module
|
23
|
+
def memoize(method)
|
24
|
+
# alias_method is faster than define_method + old.bind(self).call
|
25
|
+
alias_method "__memoized__#{method}", method
|
26
|
+
module_eval <<-EOF
|
27
|
+
def #{method}(*a, &b)
|
28
|
+
# assumes the block won't change the result if the args are the same
|
29
|
+
(@__memoized_#{method}_cache ||= {})[a] ||= __memoized__#{method}(*a, &b)
|
30
|
+
end
|
31
|
+
EOF
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# English part-of-speech tagger class
|
36
|
+
class EngTagger
|
37
|
+
|
38
|
+
#################
|
39
|
+
# Class methods #
|
40
|
+
#################
|
41
|
+
|
42
|
+
# Return a class variable that holds probability data
|
43
|
+
def self.hmm
|
44
|
+
return @@hmm
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return a class variable that holds lexical data
|
48
|
+
def self.lexicon
|
49
|
+
return @@lexicon
|
50
|
+
end
|
51
|
+
|
52
|
+
# Return a regexp from a string argument that matches an XML-style pos tag
|
53
|
+
def self.get_ext(tag = nil)
|
54
|
+
return nil unless tag
|
55
|
+
return Regexp.new("<#{tag}>[^<]+</#{tag}>\s*")
|
56
|
+
end
|
57
|
+
|
58
|
+
# Regexps to match XML-style part-of-speech tags
|
59
|
+
NUM = get_ext('cd')
|
60
|
+
GER = get_ext('vbg')
|
61
|
+
ADJ = get_ext('jj[rs]*')
|
62
|
+
NN = get_ext('nn[sp]*')
|
63
|
+
NNP = get_ext('nnp')
|
64
|
+
PREP = get_ext('in')
|
65
|
+
DET = get_ext('det')
|
66
|
+
PAREN = get_ext('[lr]rb')
|
67
|
+
QUOT = get_ext('ppr')
|
68
|
+
SEN = get_ext('pp')
|
69
|
+
WORD = get_ext('\w+')
|
70
|
+
VB = get_ext('vb')
|
71
|
+
VBG = get_ext('vbg')
|
72
|
+
VBD = get_ext('vbd')
|
73
|
+
PART = get_ext('vbn')
|
74
|
+
VBP = get_ext('vbp')
|
75
|
+
VBZ = get_ext('vbz')
|
76
|
+
JJ = get_ext('jj')
|
77
|
+
JJR = get_ext('jjr')
|
78
|
+
JJS = get_ext('jjs')
|
79
|
+
RB = get_ext('rb')
|
80
|
+
RBR = get_ext('rbr')
|
81
|
+
RBS = get_ext('rbs')
|
82
|
+
RP = get_ext('rp')
|
83
|
+
WRB = get_ext('wrb')
|
84
|
+
WDT = get_ext('wdt')
|
85
|
+
WP = get_ext('wp')
|
86
|
+
WPS = get_ext('wps')
|
87
|
+
CC = get_ext('cc')
|
88
|
+
IN = get_ext('in')
|
89
|
+
|
90
|
+
# Convert a Treebank-style, abbreviated tag into verbose definitions
|
91
|
+
def self.explain_tag(tag)
|
92
|
+
if TAGS[tag]
|
93
|
+
return TAGS[tag]
|
94
|
+
else
|
95
|
+
return tag
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# The folloging is to make a hash to convert a pos tag to its definition
|
100
|
+
# used by the explain_tag method
|
101
|
+
tags = [
|
102
|
+
"CC", "Conjunction, coordinating",
|
103
|
+
"CD", "Adjective, cardinal number",
|
104
|
+
"DET", "Determiner",
|
105
|
+
"EX", "Pronoun, existential there",
|
106
|
+
"FW", "Foreign words",
|
107
|
+
"IN", "Preposition / Conjunction",
|
108
|
+
"JJ", "Adjective",
|
109
|
+
"JJR", "Adjective, comparative",
|
110
|
+
"JJS", "Adjective, superlative",
|
111
|
+
"LS", "Symbol, list item",
|
112
|
+
"MD", "Verb, modal",
|
113
|
+
"NN", "Noun",
|
114
|
+
"NNP", "Noun, proper",
|
115
|
+
"NNPS", "Noun, proper, plural",
|
116
|
+
"NNS", "Noun, plural",
|
117
|
+
"PDT", "Determiner, prequalifier",
|
118
|
+
"POS", "Possessive",
|
119
|
+
"PRP", "Determiner, possessive second",
|
120
|
+
"PRPS", "Determiner, possessive",
|
121
|
+
"RB", "Adverb",
|
122
|
+
"RBR", "Adverb, comparative",
|
123
|
+
"RBS", "Adverb, superlative",
|
124
|
+
"RP", "Adverb, particle",
|
125
|
+
"SYM", "Symbol",
|
126
|
+
"TO", "Preposition",
|
127
|
+
"UH", "Interjection",
|
128
|
+
"VB", "Verb, infinitive",
|
129
|
+
"VBD", "Verb, past tense",
|
130
|
+
"VBG", "Verb, gerund",
|
131
|
+
"VBN", "Verb, past/passive participle",
|
132
|
+
"VBP", "Verb, base present form",
|
133
|
+
"VBZ", "Verb, present 3SG -s form",
|
134
|
+
"WDT", "Determiner, question",
|
135
|
+
"WP", "Pronoun, question",
|
136
|
+
"WPS", "Determiner, possessive & question",
|
137
|
+
"WRB", "Adverb, question",
|
138
|
+
"PP", "Punctuation, sentence ender",
|
139
|
+
"PPC", "Punctuation, comma",
|
140
|
+
"PPD", "Punctuation, dollar sign",
|
141
|
+
"PPL", "Punctuation, quotation mark left",
|
142
|
+
"PPR", "Punctuation, quotation mark right",
|
143
|
+
"PPS", "Punctuation, colon, semicolon, elipsis",
|
144
|
+
"LRB", "Punctuation, left bracket",
|
145
|
+
"RRB", "Punctuation, right bracket"
|
146
|
+
]
|
147
|
+
tags = tags.collect{|t| t.downcase.gsub(/[\.\,\'\-\s]+/, '_')}
|
148
|
+
tags = tags.collect{|t| t.gsub(/\&/, "and").gsub(/\//, "or")}
|
149
|
+
TAGS = Hash[*tags]
|
150
|
+
|
151
|
+
# Hash storing config values:
|
152
|
+
#
|
153
|
+
# * :unknown_word_tag
|
154
|
+
# => (String) Tag to assign to unknown words
|
155
|
+
# * :stem
|
156
|
+
# => (Boolean) Stem single words using Porter module
|
157
|
+
# * :weight_noun_phrases
|
158
|
+
# => (Boolean) When returning occurrence counts for a noun phrase, multiply
|
159
|
+
# the valuethe number of words in the NP.
|
160
|
+
# * :longest_noun_phrase
|
161
|
+
# => (Integer) Will ignore noun phrases longer than this threshold. This
|
162
|
+
# affects only the get_words() and get_nouns() methods.
|
163
|
+
# * :relax
|
164
|
+
# => (Boolean) Relax the Hidden Markov Model: this may improve accuracy for
|
165
|
+
# uncommon words, particularly words used polysemously
|
166
|
+
# * :tag_lex
|
167
|
+
# => (String) Name of the YAML file containing a hash of adjacent part of
|
168
|
+
# speech tags and the probability of each
|
169
|
+
# * :word_lex
|
170
|
+
# => (String) Name of the YAML file containing a hash of words and corresponding
|
171
|
+
# parts of speech
|
172
|
+
# * :unknown_lex
|
173
|
+
# => (String) Name of the YAML file containing a hash of tags for unknown
|
174
|
+
# words and corresponding parts of speech
|
175
|
+
# * :tag_path
|
176
|
+
# => (String) Directory path of tag_lex
|
177
|
+
# * :word_path
|
178
|
+
# => (String) Directory path of word_lex and unknown_lex
|
179
|
+
# * :debug
|
180
|
+
# => (Boolean) Print debug messages
|
181
|
+
attr_accessor :conf
|
182
|
+
|
183
|
+
###############
|
184
|
+
# Constructor #
|
185
|
+
###############
|
186
|
+
|
187
|
+
# Take a hash of parameters that override default values.
|
188
|
+
# See above for details.
|
189
|
+
def initialize(params = {})
|
190
|
+
@conf = Hash.new
|
191
|
+
@conf[:unknown_word_tag] = ''
|
192
|
+
@conf[:stem] = false
|
193
|
+
@conf[:weight_noun_phrases] = false
|
194
|
+
@conf[:longest_noun_phrase] = 5
|
195
|
+
@conf[:relax] = false
|
196
|
+
@conf[:tag_lex] = 'tags.yml'
|
197
|
+
@conf[:word_lex] = 'words.yml'
|
198
|
+
@conf[:unknown_lex] = 'unknown.yml'
|
199
|
+
@conf[:word_path] = $word_path
|
200
|
+
@conf[:tag_path] = $tag_path
|
201
|
+
@conf[:debug] = false
|
202
|
+
# assuming that we start analyzing from the beginninga new sentence...
|
203
|
+
@conf[:current_tag] = 'pp'
|
204
|
+
@conf.merge!(params)
|
205
|
+
unless File.exist?(@conf[:word_path]) and File.exist?(@conf[:tag_path])
|
206
|
+
print "Couldn't locate POS lexicon, creating a new one" if @conf[:debug]
|
207
|
+
@@hmm = Hash.new
|
208
|
+
@@lexicon = Hash.new
|
209
|
+
else
|
210
|
+
lexf = File.open(@conf[:word_path], 'r')
|
211
|
+
@@lexicon = Marshal.load(lexf)
|
212
|
+
lexf.close
|
213
|
+
hmmf = File.open(@conf[:tag_path], 'r')
|
214
|
+
@@hmm = Marshal.load(hmmf)
|
215
|
+
hmmf.close
|
216
|
+
end
|
217
|
+
@@mnp = get_max_noun_regex
|
218
|
+
end
|
219
|
+
|
220
|
+
##################
|
221
|
+
# Public methods #
|
222
|
+
##################
|
223
|
+
|
224
|
+
# Examine the string provided and return it fully tagged in XML style
|
225
|
+
def add_tags(text, verbose = false)
|
226
|
+
return nil unless valid_text(text)
|
227
|
+
tagged = []
|
228
|
+
words = clean_text(text)
|
229
|
+
tags = Array.new
|
230
|
+
words.each do |word|
|
231
|
+
cleaned_word = clean_word(word)
|
232
|
+
tag = assign_tag(@conf[:current_tag], cleaned_word)
|
233
|
+
@conf[:current_tag] = tag = (tag and tag != "") ? tag : 'nn'
|
234
|
+
tag = EngTagger.explain_tag(tag) if verbose
|
235
|
+
tagged << '<' + tag + '>' + word + '</' + tag + '>'
|
236
|
+
end
|
237
|
+
reset
|
238
|
+
return tagged.join(' ')
|
239
|
+
end
|
240
|
+
|
241
|
+
# Given a text string, return as many nouns and noun phrases as possible.
|
242
|
+
# Applies add_tags and involves three stages:
|
243
|
+
#
|
244
|
+
# * Tag the text
|
245
|
+
# * Extract all the maximal noun phrases
|
246
|
+
# * Recursively extract all noun phrases from the MNPs
|
247
|
+
#
|
248
|
+
def get_words(text)
|
249
|
+
return false unless valid_text(text)
|
250
|
+
tagged = add_tags(text)
|
251
|
+
if(@conf[:longest_noun_phrase] <= 1)
|
252
|
+
return get_nouns(tagged)
|
253
|
+
else
|
254
|
+
return get_noun_phrases(tagged)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
# Return an easy-on-the-eyes tagged version of a text string.
|
259
|
+
# Applies add_tags and reformats to be easier to read.
|
260
|
+
def get_readable(text, verbose = false)
|
261
|
+
return nil unless valid_text(text)
|
262
|
+
tagged = add_tags(text, verbose)
|
263
|
+
tagged = tagged.gsub(/<\w+>([^<]+)<\/(\w+)>/o) do
|
264
|
+
$1 + '/' + $2.upcase
|
265
|
+
end
|
266
|
+
return tagged
|
267
|
+
end
|
268
|
+
|
269
|
+
# Return an array of sentences (without POS tags) from a text.
|
270
|
+
def get_sentences(text)
|
271
|
+
return nil unless valid_text(text)
|
272
|
+
tagged = add_tags(text)
|
273
|
+
sentences = Array.new
|
274
|
+
tagged.split(/<\/pp>/).each do |line|
|
275
|
+
sentences << strip_tags(line)
|
276
|
+
end
|
277
|
+
sentences = sentences.map do |sentence|
|
278
|
+
sentence.gsub(Regexp.new(" ('s?) ")){$1 + ' '}
|
279
|
+
sentence.gsub(Regexp.new(" (\W+) ")){$1 + ' '}
|
280
|
+
sentence.gsub(Regexp.new(" (`+) ")){' ' + $1}
|
281
|
+
sentence.gsub(Regexp.new(" (\W+)$")){$1}
|
282
|
+
sentence.gsub(Regexp.new("^(`+) ")){$1}
|
283
|
+
end
|
284
|
+
return sentences
|
285
|
+
end
|
286
|
+
|
287
|
+
# Given a POS-tagged text, this method returns a hash of all proper nouns
|
288
|
+
# and their occurrence frequencies. The method is greedy and will
|
289
|
+
# return multi-word phrases, if possible, so it would find ``Linguistic
|
290
|
+
# Data Consortium'' as a single unit, rather than as three individual
|
291
|
+
# proper nouns. This method does not stem the found words.
|
292
|
+
def get_proper_nouns(tagged)
|
293
|
+
return nil unless valid_text(tagged)
|
294
|
+
tags = [NNP]
|
295
|
+
nnp = build_matches_hash(build_trimmed(tagged, tags))
|
296
|
+
# Now for some fancy resolution stuff...
|
297
|
+
nnp.keys.each do |key|
|
298
|
+
words = key.split(/\s/)
|
299
|
+
# Let's say this is an organization's name --
|
300
|
+
# (and it's got at least three words)
|
301
|
+
# is there a corresponding acronym in this hash?
|
302
|
+
if words.length > 2
|
303
|
+
# Make a (naive) acronym out of this name
|
304
|
+
acronym = words.map do |word|
|
305
|
+
/\A([a-z])[a-z]*\z/ =~ word
|
306
|
+
$1
|
307
|
+
end.join ''
|
308
|
+
# If that acronym has been seen,
|
309
|
+
# remove it and add the values to
|
310
|
+
# the full name
|
311
|
+
if nnp[acronym]
|
312
|
+
nnp[key] += nnp[acronym]
|
313
|
+
nnp.delete(acronym)
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
return nnp
|
318
|
+
end
|
319
|
+
|
320
|
+
# Given a POS-tagged text, this method returns all nouns and their
|
321
|
+
# occurrence frequencies.
|
322
|
+
def get_nouns(tagged)
|
323
|
+
return nil unless valid_text(tagged)
|
324
|
+
tags = [NN]
|
325
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
326
|
+
end
|
327
|
+
|
328
|
+
# Returns all types of verbs and does not descriminate between the various kinds.
|
329
|
+
# Is the combination of all other verb methods listed in this class.
|
330
|
+
def get_verbs(tagged)
|
331
|
+
return nil unless valid_text(tagged)
|
332
|
+
tags = [VB, VBD, VBG, PART, VBP, VBZ]
|
333
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
334
|
+
end
|
335
|
+
|
336
|
+
def get_infinitive_verbs(tagged)
|
337
|
+
return nil unless valid_text(tagged)
|
338
|
+
tags = [VB]
|
339
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
340
|
+
end
|
341
|
+
|
342
|
+
def get_past_tense_verbs(tagged)
|
343
|
+
return nil unless valid_text(tagged)
|
344
|
+
tags = [VBD]
|
345
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
346
|
+
end
|
347
|
+
|
348
|
+
def get_gerund_verbs(tagged)
|
349
|
+
return nil unless valid_text(tagged)
|
350
|
+
tags = [VBG]
|
351
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
352
|
+
end
|
353
|
+
|
354
|
+
def get_passive_verbs(tagged)
|
355
|
+
return nil unless valid_text(tagged)
|
356
|
+
tags = [PART]
|
357
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
358
|
+
end
|
359
|
+
|
360
|
+
def get_base_present_verbs(tagged)
|
361
|
+
return nil unless valid_text(tagged)
|
362
|
+
tags = [VBP]
|
363
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
364
|
+
end
|
365
|
+
|
366
|
+
def get_present_verbs(tagged)
|
367
|
+
return nil unless valid_text(tagged)
|
368
|
+
tags = [VBZ]
|
369
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
370
|
+
end
|
371
|
+
|
372
|
+
def get_adjectives(tagged)
|
373
|
+
return nil unless valid_text(tagged)
|
374
|
+
tags = [JJ]
|
375
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
376
|
+
end
|
377
|
+
|
378
|
+
def get_comparative_adjectives(tagged)
|
379
|
+
return nil unless valid_text(tagged)
|
380
|
+
tags = [JJR]
|
381
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
382
|
+
end
|
383
|
+
|
384
|
+
def get_superlative_adjectives(tagged)
|
385
|
+
return nil unless valid_text(tagged)
|
386
|
+
tags = [JJS]
|
387
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
388
|
+
end
|
389
|
+
|
390
|
+
def get_adverbs(tagged)
|
391
|
+
return nil unless valid_text(tagged)
|
392
|
+
tags = [RB, RBR, RBS, RP]
|
393
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
394
|
+
end
|
395
|
+
|
396
|
+
def get_interrogatives(tagged)
|
397
|
+
return nil unless valid_text(tagged)
|
398
|
+
tags = [WRB, WDT, WP, WPS]
|
399
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
400
|
+
end
|
401
|
+
# To be consistent with documentation's naming of 'interrogative' parts of speech as 'question'
|
402
|
+
alias_method :get_question_parts, :get_interrogatives
|
403
|
+
|
404
|
+
# Returns all types of conjunctions and does not discriminate between the various kinds.
|
405
|
+
# E.g. coordinating, subordinating, correlative...
|
406
|
+
def get_conjunctions(tagged)
|
407
|
+
return nil unless valid_text(tagged)
|
408
|
+
tags = [CC, IN]
|
409
|
+
build_matches_hash(build_trimmed(tagged, tags))
|
410
|
+
end
|
411
|
+
|
412
|
+
# Given a POS-tagged text, this method returns only the maximal noun phrases.
|
413
|
+
# May be called directly, but is also used by get_noun_phrases
|
414
|
+
def get_max_noun_phrases(tagged)
|
415
|
+
return nil unless valid_text(tagged)
|
416
|
+
tags = [@@mnp]
|
417
|
+
mn_phrases = build_trimmed(tagged, tags)
|
418
|
+
ret = Hash.new(0)
|
419
|
+
mn_phrases.each do |p|
|
420
|
+
p = stem(p) unless p =~ /\s/ # stem single words
|
421
|
+
ret[p] += 1 unless p =~ /\A\s*\z/
|
422
|
+
end
|
423
|
+
return ret
|
424
|
+
end
|
425
|
+
|
426
|
+
# Similar to get_words, but requires a POS-tagged text as an argument.
|
427
|
+
def get_noun_phrases(tagged)
|
428
|
+
return nil unless valid_text(tagged)
|
429
|
+
found = Hash.new(0)
|
430
|
+
phrase_ext = /(?:#{PREP}|#{DET}|#{NUM})+/xo
|
431
|
+
scanned = tagged.scan(@@mnp)
|
432
|
+
# Find MNPs in the text, one sentence at a time
|
433
|
+
# Record and split if the phrase is extended by a (?:PREP|DET|NUM)
|
434
|
+
mn_phrases = []
|
435
|
+
scanned.each do |m|
|
436
|
+
found[m] += 1 if phrase_ext =~ m
|
437
|
+
mn_phrases += m.split(phrase_ext)
|
438
|
+
end
|
439
|
+
mn_phrases.each do |mnp|
|
440
|
+
# Split the phrase into an array of words, and create a loop for each word,
|
441
|
+
# shortening the phrase by removing the word in the first position.
|
442
|
+
# Record the phrase and any single nouns that are found
|
443
|
+
words = mnp.split
|
444
|
+
words.length.times do |i|
|
445
|
+
found[words.join(' ')] += 1 if words.length > 1
|
446
|
+
w = words.shift
|
447
|
+
found[w] += 1 if w =~ /#{NN}/
|
448
|
+
end
|
449
|
+
end
|
450
|
+
ret = Hash.new(0)
|
451
|
+
found.keys.each do |f|
|
452
|
+
k = strip_tags(f)
|
453
|
+
v = found[f]
|
454
|
+
# We weight by the word count to favor long noun phrases
|
455
|
+
space_count = k.scan(/\s+/)
|
456
|
+
word_count = space_count.length + 1
|
457
|
+
# Throttle MNPs if necessary
|
458
|
+
next if word_count > @conf[:longest_noun_phrase]
|
459
|
+
k = stem(k) unless word_count > 1 # stem single words
|
460
|
+
multiplier = 1
|
461
|
+
multiplier = word_count if @conf[:weight_noun_phrases]
|
462
|
+
ret[k] += multiplier * v
|
463
|
+
end
|
464
|
+
return ret
|
465
|
+
end
|
466
|
+
|
467
|
+
# Reads some included corpus data and saves it in a stored hash on the
|
468
|
+
# local file system. This is called automatically if the tagger can't
|
469
|
+
# find the stored lexicon.
|
470
|
+
def install
|
471
|
+
puts "Creating part-of-speech lexicon" if @conf[:debug]
|
472
|
+
load_tags(@conf[:tag_lex])
|
473
|
+
load_words(@conf[:word_lex])
|
474
|
+
load_words(@conf[:unknown_lex])
|
475
|
+
File.open(@conf[:word_path], 'w') do |f|
|
476
|
+
Marshal.dump(@@lexicon, f)
|
477
|
+
end
|
478
|
+
File.open(@conf[:tag_path], 'w') do |f|
|
479
|
+
Marshal.dump(@@hmm, f)
|
480
|
+
end
|
481
|
+
end
|
482
|
+
|
483
|
+
###################
|
484
|
+
# Private methods #
|
485
|
+
###################
|
486
|
+
|
487
|
+
:private
|
488
|
+
|
489
|
+
def build_trimmed(tagged, tags)
|
490
|
+
tags.map { |tag| tagged.scan(tag) }.flatten.map do |n|
|
491
|
+
strip_tags(n)
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
def build_matches_hash(trimmed)
|
496
|
+
ret = Hash.new(0)
|
497
|
+
trimmed.each do |n|
|
498
|
+
n = stem(n)
|
499
|
+
next unless n.length < 100 # sanity check on word length
|
500
|
+
ret[n] += 1 unless n =~ /\A\s*\z/
|
501
|
+
end
|
502
|
+
ret
|
503
|
+
end
|
504
|
+
|
505
|
+
# Downcase the first letter of word
|
506
|
+
def lcfirst(word)
|
507
|
+
word.split(//)[0].downcase + word.split(//)[1..-1].join
|
508
|
+
end
|
509
|
+
|
510
|
+
# Upcase the first letter of word
|
511
|
+
def ucfirst(word)
|
512
|
+
word.split(//)[0].upcase + word.split(//)[1..-1].join
|
513
|
+
end
|
514
|
+
|
515
|
+
# Return the word stem as given by Stemmable module. This can be
|
516
|
+
# turned off with the class parameter @conf[:stem] => false.
|
517
|
+
def stem(word)
|
518
|
+
return word unless @conf[:stem]
|
519
|
+
return word.stem
|
520
|
+
end
|
521
|
+
|
522
|
+
# This method will reset the preceeding tag to a sentence ender (PP).
|
523
|
+
# This prepares the first word of a new sentence to be tagged correctly.
|
524
|
+
def reset
|
525
|
+
@conf[:current_tag] = 'pp'
|
526
|
+
end
|
527
|
+
|
528
|
+
# Check whether the text is a valid string
|
529
|
+
def valid_text(text)
|
530
|
+
if !text
|
531
|
+
# there's nothing to parse
|
532
|
+
"method call on uninitialized variable" if @conf[:debug]
|
533
|
+
return false
|
534
|
+
elsif /\A\s*\z/ =~ text
|
535
|
+
# text is an empty string, nothing to parse
|
536
|
+
return false
|
537
|
+
else
|
538
|
+
# $text is valid
|
539
|
+
return true
|
540
|
+
end
|
541
|
+
end
|
542
|
+
|
543
|
+
# Return a text string with the part-of-speech tags removed
|
544
|
+
def strip_tags(tagged, downcase = false)
|
545
|
+
return nil unless valid_text(tagged)
|
546
|
+
text = tagged.gsub(/<[^>]+>/m, "")
|
547
|
+
text = text.gsub(/\s+/m, " ")
|
548
|
+
text = text.gsub(/\A\s*/, "")
|
549
|
+
text = text.gsub(/\s*\z/, "")
|
550
|
+
if downcase
|
551
|
+
return text.downcase
|
552
|
+
else
|
553
|
+
return text
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
# Strip the provided text of HTML-style tags and separate off any punctuation
|
558
|
+
# in preparation for tagging
|
559
|
+
def clean_text(text)
|
560
|
+
return false unless valid_text(text)
|
561
|
+
text = text.toutf8
|
562
|
+
unless $no_hpricot
|
563
|
+
# Strip out any markup and convert entities to their proper form
|
564
|
+
cleaned_text = Hpricot(text).inner_text
|
565
|
+
else
|
566
|
+
cleaned_text = text
|
567
|
+
end
|
568
|
+
tokenized = []
|
569
|
+
# Tokenize the text (splitting on punctuation as you go)
|
570
|
+
cleaned_text.split(/\s+/).each do |line|
|
571
|
+
tokenized += split_punct(line)
|
572
|
+
end
|
573
|
+
words = split_sentences(tokenized)
|
574
|
+
return words
|
575
|
+
end
|
576
|
+
|
577
|
+
# This handles all of the trailing periods, keeping those that
|
578
|
+
# belong on abbreviations and removing those that seem to be
|
579
|
+
# at the end of sentences. This method makes some assumptions
|
580
|
+
# about the use of capitalization in the incoming text
|
581
|
+
def split_sentences(array)
|
582
|
+
tokenized = array
|
583
|
+
people = %w(jr mr ms mrs dr prof esq sr sen sens rep reps gov attys attys
|
584
|
+
supt det mssrs rev)
|
585
|
+
army = %w(col gen lt cmdr adm capt sgt cpl maj brig)
|
586
|
+
inst = %w(dept univ assn bros ph.d)
|
587
|
+
place = %w(arc al ave blvd bld cl ct cres exp expy dist mt mtn ft fy fwy
|
588
|
+
hwy hway la pde pd plz pl rd st tce)
|
589
|
+
comp = %w(mfg inc ltd co corp)
|
590
|
+
state = %w(ala ariz ark cal calif colo col conn del fed fla ga ida id ill
|
591
|
+
ind ia kans kan ken ky la me md is mass mich minn miss mo mont
|
592
|
+
neb nebr nev mex okla ok ore penna penn pa dak tenn tex ut vt
|
593
|
+
va wash wis wisc wy wyo usafa alta man ont que sask yuk)
|
594
|
+
month = %w(jan feb mar apr may jun jul aug sep sept oct nov dec)
|
595
|
+
misc = %w(vs etc no esp)
|
596
|
+
abbr = Hash.new
|
597
|
+
[people, army, inst, place, comp, state, month, misc].flatten.each do |i|
|
598
|
+
abbr[i] = true
|
599
|
+
end
|
600
|
+
words = Array.new
|
601
|
+
tokenized.each_with_index do |t, i|
|
602
|
+
if tokenized[i + 1] and tokenized [i + 1] =~ /[A-Z\W]/ and tokenized[i] =~ /\A(.+)\.\z/
|
603
|
+
w = $1
|
604
|
+
# Don't separate the period off words that
|
605
|
+
# meet any of the following conditions:
|
606
|
+
#
|
607
|
+
# 1. It is defined in one of the lists above
|
608
|
+
# 2. It is only one letter long: Alfred E. Sloan
|
609
|
+
# 3. It has a repeating letter-dot: U.S.A. or J.C. Penney
|
610
|
+
unless abbr[w.downcase] or w =~ /\A[a-z]\z/i or w =~ /[a-z](?:\.[a-z])+\z/i
|
611
|
+
words << w
|
612
|
+
words << '.'
|
613
|
+
next
|
614
|
+
end
|
615
|
+
end
|
616
|
+
words << tokenized[i]
|
617
|
+
end
|
618
|
+
# If the final word ends in a period..
|
619
|
+
if words[-1] and words[-1] =~ /\A(.*\w)\.\z/
|
620
|
+
words[-1] = $1
|
621
|
+
words.push '.'
|
622
|
+
end
|
623
|
+
return words
|
624
|
+
end
|
625
|
+
|
626
|
+
# Separate punctuation from words, where appropriate. This leaves trailing
|
627
|
+
# periods in place to be dealt with later. Called by the clean_text method.
|
628
|
+
def split_punct(text)
|
629
|
+
# If there's no punctuation, return immediately
|
630
|
+
return [text] if /\A\w+\z/ =~ text
|
631
|
+
# Sanity checks
|
632
|
+
text = text.gsub(/\W{10,}/o, " ")
|
633
|
+
|
634
|
+
# Put quotes into a standard format
|
635
|
+
text = text.gsub(/`(?!`)(?=.*\w)/o, "` ") # Shift left quotes off text
|
636
|
+
text = text.gsub(/"(?=.*\w)/o, " `` ") # Convert left quotes to ``
|
637
|
+
text = text.gsub(/(\W|^)'(?=.*\w)/o){$1 ? $1 + " ` " : " ` "} # Convert left quotes to `
|
638
|
+
text = text.gsub(/"/, " '' ") # Convert (remaining) quotes to ''
|
639
|
+
text = text.gsub(/(\w)'(?!')(?=\W|$)/o){$1 + " ' "} # Separate right single quotes
|
640
|
+
|
641
|
+
# Handle all other punctuation
|
642
|
+
text = text.gsub(/--+/o, " - ") # Convert and separate dashes
|
643
|
+
text = text.gsub(/,(?!\d)/o, " , ") # Shift commas off everything but numbers
|
644
|
+
text = text.gsub(/:/o, " :") # Shift semicolons off
|
645
|
+
text = text.gsub(/(\.\.\.+)/o){" " + $1 + " "} # Shift ellipses off
|
646
|
+
text = text.gsub(/([\(\[\{\}\]\)])/o){" " + $1 + " "} # Shift off brackets
|
647
|
+
text = text.gsub(/([\!\?#\$%;~|])/o){" " + $1 + " "} # Shift off other ``standard'' punctuation
|
648
|
+
|
649
|
+
# English-specific contractions
|
650
|
+
text = text.gsub(/([A-Za-z])'([dms])\b/o){$1 + " '" + $2} # Separate off 'd 'm 's
|
651
|
+
text = text.gsub(/n't\b/o, " n't") # Separate off n't
|
652
|
+
text = text.gsub(/'(ve|ll|re)\b/o){" '" + $1} # Separate off 've, 'll, 're
|
653
|
+
result = text.split(' ')
|
654
|
+
return result
|
655
|
+
end
|
656
|
+
|
657
|
+
# Given a preceding tag, assign a tag word. Called by the add_tags method.
|
658
|
+
# This method is a modified version of the Viterbi algorithm for part-of-speech tagging
|
659
|
+
def assign_tag(prev_tag, word)
|
660
|
+
if word == "-unknown-"
|
661
|
+
# classify unknown words accordingly
|
662
|
+
return @conf[:unknown_word_tag]
|
663
|
+
elsif word == "-sym-"
|
664
|
+
# If this is a symbol, tag it as a symbol
|
665
|
+
return "sym"
|
666
|
+
end
|
667
|
+
best_so_far = 0
|
668
|
+
w = @@lexicon[word]
|
669
|
+
t = @@hmm
|
670
|
+
|
671
|
+
# TAG THE TEXT: What follows is a modified version of the Viterbi algorithm
|
672
|
+
# which is used in most POS taggers
|
673
|
+
best_tag = ""
|
674
|
+
t[prev_tag].keys.each do |tag|
|
675
|
+
# With @config[:relax] set, this method
|
676
|
+
# will also include any `open classes' of POS tags
|
677
|
+
pw = 0
|
678
|
+
if w[tag]
|
679
|
+
pw = w[tag]
|
680
|
+
elsif @conf[:relax] and tag =~ /\A(?:jj|nn|rb|vb)/
|
681
|
+
pw = 0
|
682
|
+
else
|
683
|
+
next
|
684
|
+
end
|
685
|
+
|
686
|
+
# Bayesian logic:
|
687
|
+
# P = P( tag | prev_tag ) * P( tag | word )
|
688
|
+
probability = t[prev_tag][tag] * (pw + 1)
|
689
|
+
# Set the tag with maximal probability
|
690
|
+
if probability > best_so_far
|
691
|
+
best_so_far = probability
|
692
|
+
best_tag = tag
|
693
|
+
end
|
694
|
+
end
|
695
|
+
return best_tag
|
696
|
+
end
|
697
|
+
|
698
|
+
# This method determines whether a word should be considered in its
|
699
|
+
# lower or upper case form. This is useful in considering proper nouns
|
700
|
+
# and words that begin sentences. Called by add_tags.
|
701
|
+
def clean_word(word)
|
702
|
+
lcf = lcfirst(word)
|
703
|
+
# seen this word as it appears (lower or upper case)
|
704
|
+
if @@lexicon[word]
|
705
|
+
return word
|
706
|
+
elsif @@lexicon[lcf]
|
707
|
+
# seen this word only as lower case
|
708
|
+
return lcf
|
709
|
+
else
|
710
|
+
# never seen this word. guess.
|
711
|
+
return classify_unknown_word(word)
|
712
|
+
end
|
713
|
+
end
|
714
|
+
|
715
|
+
# This changes any word not appearing in the lexicon to identifiable
|
716
|
+
# classes of words handled by a simple unknown word classification
|
717
|
+
# metric. Called by the clean_word method.
|
718
|
+
def classify_unknown_word(word)
|
719
|
+
if /[\(\{\[]/ =~ word # Left brackets
|
720
|
+
classified = "*LRB*"
|
721
|
+
elsif
|
722
|
+
/[\)\}\]]/ =~ word # Right brackets
|
723
|
+
classified = "*RRB*"
|
724
|
+
elsif /-?(?:\d+(?:\.\d*)?|\.\d+)\z/ =~ word # Floating point number
|
725
|
+
classified = "*NUM*"
|
726
|
+
elsif /\A\d+[\d\/:-]+\d\z/ =~ word # Other number constructs
|
727
|
+
classified = "*NUM*"
|
728
|
+
elsif /\A-?\d+\w+\z/o =~ word # Ordinal number
|
729
|
+
classified = "*ORD*"
|
730
|
+
elsif /\A[A-Z][A-Z\.-]*\z/o =~ word # Abbreviation (all caps)
|
731
|
+
classified = "-abr-"
|
732
|
+
elsif /\w-\w/o =~ word # Hyphenated word
|
733
|
+
/-([^-]+)\z/ =~ word
|
734
|
+
h_suffix = $1
|
735
|
+
if h_suffix and (@@lexicon[h_suffix] and @@lexicon[h_suffix]['jj'])
|
736
|
+
# last part of this is defined as an adjective
|
737
|
+
classified = "-hyp-adj-"
|
738
|
+
else
|
739
|
+
# last part of this is not defined as an adjective
|
740
|
+
classified = "-hyp-"
|
741
|
+
end
|
742
|
+
elsif /\A\W+\z/o =~ word
|
743
|
+
classified = "-sym-" # Symbol
|
744
|
+
elsif word == ucfirst(word)
|
745
|
+
classified = "-cap-" # Capitalized word
|
746
|
+
elsif /ing\z/o =~ word
|
747
|
+
classified = "-ing-" # Ends in 'ing'
|
748
|
+
elsif /s\z/o =~ word
|
749
|
+
classified = "-s-" # Ends in 's'
|
750
|
+
elsif /tion\z/o =~ word
|
751
|
+
classified = "-tion-" # Ends in 'tion'
|
752
|
+
elsif /ly\z/o =~ word
|
753
|
+
classified = "-ly-" # Ends in 'ly'
|
754
|
+
elsif /ed\z/o =~ word
|
755
|
+
classified = "-ed-" # Ends in 'ed
|
756
|
+
else
|
757
|
+
classified = "-unknown-" # Completely unknown
|
758
|
+
end
|
759
|
+
return classified
|
760
|
+
end
|
761
|
+
|
762
|
+
# This returns a compiled regexp for extracting maximal noun phrases
|
763
|
+
# from a POS-tagged text.
|
764
|
+
def get_max_noun_regex
|
765
|
+
regex = /
|
766
|
+
# optional number, gerund - adjective -participle
|
767
|
+
(?:#{NUM})?(?:#{GER}|#{ADJ}|#{PART})*
|
768
|
+
# Followed by one or more nouns
|
769
|
+
(?:#{NN})+
|
770
|
+
(?:
|
771
|
+
# Optional preposition, determinant, cardinal
|
772
|
+
(?:#{PREP})*(?:#{DET})?(?:#{NUM})?
|
773
|
+
# Optional gerund-adjective -participle
|
774
|
+
(?:#{GER}|#{ADJ}|#{PART})*
|
775
|
+
# one or more nouns
|
776
|
+
(?:#{NN})+
|
777
|
+
)*
|
778
|
+
/xo #/
|
779
|
+
return regex
|
780
|
+
end
|
781
|
+
|
782
|
+
# Load the 2-grams into a hash from YAML data: This is a naive (but fast)
|
783
|
+
# YAML data parser. It will load a YAML document with a collection of key:
|
784
|
+
# value entries ( {pos tag}: {probability} ) mapped onto single keys ( {tag} ).
|
785
|
+
# Each map is expected to be on a single line; i.e., det: { jj: 0.2, nn: 0.5, vb: 0.0002 }
|
786
|
+
def load_tags(lexicon)
|
787
|
+
path = File.join($lexpath, lexicon)
|
788
|
+
fh = File.open(path, 'r')
|
789
|
+
while line = fh.gets
|
790
|
+
/\A"?([^{"]+)"?: \{ (.*) \}/ =~ line
|
791
|
+
next unless $1 and $2
|
792
|
+
key, data = $1, $2
|
793
|
+
tags = Hash.new
|
794
|
+
items = data.split(/,\s+/)
|
795
|
+
pairs = {}
|
796
|
+
items.each do |i|
|
797
|
+
/([^:]+):\s*(.+)/ =~ i
|
798
|
+
pairs[$1] = $2.to_f
|
799
|
+
end
|
800
|
+
@@hmm[key] = pairs
|
801
|
+
end
|
802
|
+
fh.close
|
803
|
+
end
|
804
|
+
|
805
|
+
# Load the 2-grams into a hash from YAML data: This is a naive (but fast)
|
806
|
+
# YAML data parser. It will load a YAML document with a collection of key:
|
807
|
+
# value entries ( {pos tag}: {count} ) mapped onto single keys ( {a word} ).
|
808
|
+
# Each map is expected to be on a single line; i.e., key: { jj: 103, nn: 34, vb: 1 }
|
809
|
+
def load_words(lexicon)
|
810
|
+
path = File.join($lexpath, lexicon)
|
811
|
+
fh = File.open(path, 'r')
|
812
|
+
while line = fh.gets
|
813
|
+
/\A"?([^{"]+)"?: \{ (.*) \}/ =~ line
|
814
|
+
next unless $1 and $2
|
815
|
+
key, data = $1, $2
|
816
|
+
tags = Hash.new
|
817
|
+
items = data.split(/,\s+/)
|
818
|
+
pairs = {}
|
819
|
+
items.each do |i|
|
820
|
+
/([^:]+):\s*(.+)/ =~ i
|
821
|
+
pairs[$1] = $2.to_f
|
822
|
+
end
|
823
|
+
@@lexicon[key] = pairs
|
824
|
+
end
|
825
|
+
fh.close
|
826
|
+
end
|
827
|
+
|
828
|
+
#memoize the stem and assign_tag methods
|
829
|
+
memoize("stem")
|
830
|
+
memoize("assign_tag")
|
831
|
+
end
|