engtagger 0.4.6 → 2.0.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -57
- data/NOTICE +73 -0
- data/README.md +117 -63
- data/lib/engtagger/model.etm.gz +0 -0
- data/lib/engtagger/model.rb +178 -0
- data/lib/engtagger/model_builder.rb +183 -0
- data/lib/engtagger/perceptron.rb +163 -0
- data/lib/engtagger/porter.rb +8 -9
- data/lib/engtagger/tokenizer.rb +279 -0
- data/lib/engtagger/version.rb +1 -1
- data/lib/engtagger.rb +69 -389
- metadata +17 -40
- data/.gitattributes +0 -4
- data/.gitignore +0 -21
- data/.rubocop.yml +0 -72
- data/.solargraph.yml +0 -22
- data/.yardopts +0 -5
- data/Gemfile +0 -10
- data/Rakefile +0 -25
- data/engtagger.gemspec +0 -35
- data/lib/engtagger/pos_tags.hash +0 -0
- data/lib/engtagger/pos_words.hash +0 -0
- data/lib/engtagger/tags.yml +0 -45
- data/lib/engtagger/unknown.yml +0 -12
- data/lib/engtagger/words.yml +0 -43818
data/lib/engtagger.rb
CHANGED
|
@@ -1,51 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "lru_redux"
|
|
4
3
|
require_relative "engtagger/version"
|
|
5
4
|
require_relative "engtagger/porter"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end
|
|
16
|
-
MODEV
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
# English part-of-speech tagger class
|
|
5
|
+
require_relative "engtagger/perceptron"
|
|
6
|
+
|
|
7
|
+
# English part-of-speech tagger.
|
|
8
|
+
#
|
|
9
|
+
# Tagging runs an averaged perceptron over a bundled model (see NOTICE for its
|
|
10
|
+
# training data). `add_tags` produces XML-style output in the classic tagset,
|
|
11
|
+
# and the extraction methods (`get_nouns`, `get_noun_phrases`, ...) read that
|
|
12
|
+
# output. Pass `tagset: :ptb` to get standard Penn Treebank tags from
|
|
13
|
+
# `tag_pairs` and `get_readable`.
|
|
21
14
|
class EngTagger
|
|
22
|
-
extend BoundedSpaceMemoizable
|
|
23
|
-
|
|
24
|
-
# File paths
|
|
25
|
-
DEFAULT_LEXPATH = File.join(File.dirname(__FILE__), "engtagger")
|
|
26
|
-
DEFAULT_WORDPATH = File.join(DEFAULT_LEXPATH, "pos_words.hash")
|
|
27
|
-
DEFAULT_TAGPATH = File.join(DEFAULT_LEXPATH, "pos_tags.hash")
|
|
28
|
-
|
|
29
15
|
#################
|
|
30
16
|
# Class methods #
|
|
31
17
|
#################
|
|
32
18
|
|
|
33
|
-
# Return a class variable that holds probability data.
|
|
34
|
-
#
|
|
35
|
-
# @return [Hash] the probability data
|
|
36
|
-
#
|
|
37
|
-
def self.hmm
|
|
38
|
-
@@hmm
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Return a class variable that holds lexical data.
|
|
42
|
-
#
|
|
43
|
-
# @return [Hash] the lexicon
|
|
44
|
-
#
|
|
45
|
-
def self.lexicon
|
|
46
|
-
@@lexicon
|
|
47
|
-
end
|
|
48
|
-
|
|
49
19
|
# Return a regexp from a string argument that matches an XML-style pos tag
|
|
50
20
|
def self.get_ext(tag = nil)
|
|
51
21
|
return nil unless tag
|
|
@@ -147,10 +117,16 @@ class EngTagger
|
|
|
147
117
|
tags = tags.collect { |t| t.gsub("&", "and").gsub("/", "or") }
|
|
148
118
|
TAGS = Hash[*tags]
|
|
149
119
|
|
|
120
|
+
# Configuration keys of the retired HMM engine. They are accepted (so 0.4-era
|
|
121
|
+
# initializers keep working) but have nothing to configure anymore; passing
|
|
122
|
+
# one triggers a single warning rather than an error.
|
|
123
|
+
RETIRED_CONF_KEYS = %i[
|
|
124
|
+
relax unknown_word_tag tag_lex word_lex unknown_lex word_path tag_path
|
|
125
|
+
current_tag
|
|
126
|
+
].freeze
|
|
127
|
+
|
|
150
128
|
# Hash storing config values:
|
|
151
129
|
#
|
|
152
|
-
# * :unknown_word_tag
|
|
153
|
-
# => (String) Tag to assign to unknown words
|
|
154
130
|
# * :stem
|
|
155
131
|
# => (Boolean) Stem single words using Porter module
|
|
156
132
|
# * :weight_noun_phrases
|
|
@@ -159,22 +135,13 @@ class EngTagger
|
|
|
159
135
|
# * :longest_noun_phrase
|
|
160
136
|
# => (Integer) Will ignore noun phrases longer than this threshold. This
|
|
161
137
|
# affects only the get_words() and get_nouns() methods.
|
|
162
|
-
# * :
|
|
163
|
-
# => (
|
|
164
|
-
#
|
|
165
|
-
#
|
|
166
|
-
#
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
# => (String) Name of the YAML file containing a hash of words and corresponding
|
|
170
|
-
# parts of speech
|
|
171
|
-
# * :unknown_lex
|
|
172
|
-
# => (String) Name of the YAML file containing a hash of tags for unknown
|
|
173
|
-
# words and corresponding parts of speech
|
|
174
|
-
# * :tag_path
|
|
175
|
-
# => (String) Directory path of tag_lex
|
|
176
|
-
# * :word_path
|
|
177
|
-
# => (String) Directory path of word_lex and unknown_lex
|
|
138
|
+
# * :tagset
|
|
139
|
+
# => (Symbol) :legacy (default) emits the classic XML-safe lowercase tags;
|
|
140
|
+
# :ptb makes tag_pairs and get_readable emit standard Penn Treebank
|
|
141
|
+
# tags (DT, PRP$, ".", ...). add_tags always uses the classic tagset,
|
|
142
|
+
# which is what keeps its output usable as XML and regex-friendly.
|
|
143
|
+
# * :model
|
|
144
|
+
# => (String) Path to a packed model file; defaults to the bundled model
|
|
178
145
|
# * :debug
|
|
179
146
|
# => (Boolean) Print debug messages
|
|
180
147
|
attr_accessor :conf
|
|
@@ -187,35 +154,24 @@ class EngTagger
|
|
|
187
154
|
# See above for details.
|
|
188
155
|
def initialize(params = {})
|
|
189
156
|
@conf = {}
|
|
190
|
-
@conf[:unknown_word_tag] = ""
|
|
191
157
|
@conf[:stem] = false
|
|
192
158
|
@conf[:weight_noun_phrases] = false
|
|
193
159
|
@conf[:longest_noun_phrase] = 5
|
|
194
|
-
@conf[:
|
|
195
|
-
@conf[:
|
|
196
|
-
@conf[:word_lex] = "words.yml"
|
|
197
|
-
@conf[:unknown_lex] = "unknown.yml"
|
|
198
|
-
@conf[:word_path] = DEFAULT_WORDPATH
|
|
199
|
-
@conf[:tag_path] = DEFAULT_TAGPATH
|
|
160
|
+
@conf[:tagset] = :legacy
|
|
161
|
+
@conf[:model] = Perceptron::DEFAULT_MODEL
|
|
200
162
|
@conf[:debug] = false
|
|
201
|
-
# assuming that we start analyzing from the beginning of a new sentence...
|
|
202
|
-
@conf[:current_tag] = "pp"
|
|
203
163
|
@conf.merge!(params) if params
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
@@hmm = {}
|
|
207
|
-
@@lexicon = {}
|
|
208
|
-
else
|
|
209
|
-
lexf = File.open(@conf[:word_path], "rb")
|
|
210
|
-
@@lexicon = Marshal.load(lexf)
|
|
211
|
-
lexf.close
|
|
212
|
-
hmmf = File.open(@conf[:tag_path], "rb")
|
|
213
|
-
@@hmm = Marshal.load(hmmf)
|
|
214
|
-
hmmf.close
|
|
215
|
-
end
|
|
164
|
+
warn_retired_keys(params)
|
|
165
|
+
@tagger = self.class.perceptron_for(@conf[:model])
|
|
216
166
|
@@mnp = get_max_noun_regex
|
|
217
167
|
end
|
|
218
168
|
|
|
169
|
+
# One loaded model shared across instances.
|
|
170
|
+
def self.perceptron_for(model_path)
|
|
171
|
+
@@perceptrons ||= {}
|
|
172
|
+
@@perceptrons[model_path] ||= Perceptron.new(model: model_path)
|
|
173
|
+
end
|
|
174
|
+
|
|
219
175
|
##################
|
|
220
176
|
# Public methods #
|
|
221
177
|
##################
|
|
@@ -228,17 +184,10 @@ class EngTagger
|
|
|
228
184
|
def tag_pairs(text)
|
|
229
185
|
return [] unless valid_text(text)
|
|
230
186
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
tag = assign_tag(@conf[:current_tag], cleaned_word)
|
|
234
|
-
@conf[:current_tag] = tag = tag && !tag.empty? ? tag : "nn"
|
|
187
|
+
@tagger.tag(text).map do |word, tag|
|
|
188
|
+
tag = @tagger.legacy_tag(tag) unless @conf[:tagset] == :ptb
|
|
235
189
|
[word, tag.to_sym]
|
|
236
190
|
end
|
|
237
|
-
|
|
238
|
-
# reset the tagger state
|
|
239
|
-
reset
|
|
240
|
-
|
|
241
|
-
out
|
|
242
191
|
end
|
|
243
192
|
|
|
244
193
|
# Examine the string provided and return it fully tagged in XML style.
|
|
@@ -247,21 +196,17 @@ class EngTagger
|
|
|
247
196
|
# @param verbose [false, true] whether to use verbose tags
|
|
248
197
|
# @return [String] the marked-up string
|
|
249
198
|
#
|
|
250
|
-
#
|
|
199
|
+
# Uses the classic tagset regardless of :tagset, so every tag is a valid
|
|
200
|
+
# element name and the output works with the extraction methods below. The
|
|
201
|
+
# text inside the tags is not escaped.
|
|
251
202
|
def add_tags(text, verbose = false)
|
|
252
203
|
return nil unless valid_text(text)
|
|
253
204
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
words.each do |word|
|
|
257
|
-
cleaned_word = clean_word(word)
|
|
258
|
-
tag = assign_tag(@conf[:current_tag], cleaned_word)
|
|
259
|
-
@conf[:current_tag] = tag = tag && tag != "" ? tag : "nn"
|
|
205
|
+
@tagger.tag(text).map do |word, ptb|
|
|
206
|
+
tag = @tagger.legacy_tag(ptb)
|
|
260
207
|
tag = EngTagger.explain_tag(tag) if verbose
|
|
261
|
-
|
|
262
|
-
end
|
|
263
|
-
reset
|
|
264
|
-
tagged.join(" ")
|
|
208
|
+
"<#{tag}>#{word}</#{tag}>"
|
|
209
|
+
end.join(" ")
|
|
265
210
|
end
|
|
266
211
|
|
|
267
212
|
# Given a text string, return as many nouns and noun phrases as possible.
|
|
@@ -287,9 +232,13 @@ class EngTagger
|
|
|
287
232
|
def get_readable(text, verbose = false)
|
|
288
233
|
return nil unless valid_text(text)
|
|
289
234
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
235
|
+
if @conf[:tagset] == :ptb && !verbose
|
|
236
|
+
@tagger.get_readable(text)
|
|
237
|
+
else
|
|
238
|
+
tagged = add_tags(text, verbose)
|
|
239
|
+
tagged.gsub(%r{<\w+>([^<]+|[<\w>]+)</(\w+)>}o) do
|
|
240
|
+
"#{$1}/#{$2.upcase}"
|
|
241
|
+
end
|
|
293
242
|
end
|
|
294
243
|
end
|
|
295
244
|
|
|
@@ -305,8 +254,7 @@ class EngTagger
|
|
|
305
254
|
sentences.map do |sentence|
|
|
306
255
|
# Reattach punctuation to the neighboring word: opening quotes/brackets
|
|
307
256
|
# and currency signs join the following word, everything else (commas,
|
|
308
|
-
# closing marks, possessives) joins the preceding word.
|
|
309
|
-
# results used to be discarded, so none of this cleanup was applied.)
|
|
257
|
+
# closing marks, possessives) joins the preceding word.
|
|
310
258
|
sentence.gsub(/ ('s?) /) { "#{$1} " }
|
|
311
259
|
.gsub(/ ([`(\[{$]+) /) { " #{$1}" }
|
|
312
260
|
.gsub(/ ([^\w\s`(\[{$]+) /) { "#{$1} " }
|
|
@@ -586,20 +534,10 @@ class EngTagger
|
|
|
586
534
|
ret
|
|
587
535
|
end
|
|
588
536
|
|
|
589
|
-
#
|
|
590
|
-
#
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
puts "Creating part-of-speech lexicon" if @conf[:debug]
|
|
594
|
-
load_tags(@conf[:tag_lex])
|
|
595
|
-
load_words(@conf[:word_lex])
|
|
596
|
-
load_words(@conf[:unknown_lex])
|
|
597
|
-
File.open(@conf[:word_path], "wb") do |f|
|
|
598
|
-
Marshal.dump(@@lexicon, f)
|
|
599
|
-
end
|
|
600
|
-
File.open(@conf[:tag_path], "wb") do |f|
|
|
601
|
-
Marshal.dump(@@hmm, f)
|
|
602
|
-
end
|
|
537
|
+
# Return the word stem as given by Stemmable module. This can be
|
|
538
|
+
# turned off with the class parameter @conf[:stem] => false.
|
|
539
|
+
def stem(word)
|
|
540
|
+
@conf[:stem] ? word.stem : word
|
|
603
541
|
end
|
|
604
542
|
|
|
605
543
|
###################
|
|
@@ -608,6 +546,20 @@ class EngTagger
|
|
|
608
546
|
|
|
609
547
|
private
|
|
610
548
|
|
|
549
|
+
def warn_retired_keys(params)
|
|
550
|
+
return unless params
|
|
551
|
+
|
|
552
|
+
retired = params.keys.map(&:to_sym) & RETIRED_CONF_KEYS
|
|
553
|
+
return if retired.empty?
|
|
554
|
+
|
|
555
|
+
@@warned_retired ||= {}
|
|
556
|
+
retired.reject { |k| @@warned_retired[k] }.each do |key|
|
|
557
|
+
@@warned_retired[key] = true
|
|
558
|
+
warn "EngTagger: config key :#{key} belonged to the retired HMM engine " \
|
|
559
|
+
"and is ignored as of 2.0 (pin \"~> 0.4\" if you need it)"
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
|
|
611
563
|
def build_trimmed(tagged, tags)
|
|
612
564
|
tags.map { |tag| tagged.scan(tag) }.flatten.map do |n|
|
|
613
565
|
strip_tags(n)
|
|
@@ -625,28 +577,6 @@ class EngTagger
|
|
|
625
577
|
ret
|
|
626
578
|
end
|
|
627
579
|
|
|
628
|
-
# Downcase the first letter of word
|
|
629
|
-
def lcfirst(word)
|
|
630
|
-
word.split(//)[0].downcase + word.split(//)[1..].join
|
|
631
|
-
end
|
|
632
|
-
|
|
633
|
-
# Upcase the first letter of word
|
|
634
|
-
def ucfirst(word)
|
|
635
|
-
word.split(//)[0].upcase + word.split(//)[1..].join
|
|
636
|
-
end
|
|
637
|
-
|
|
638
|
-
# Return the word stem as given by Stemmable module. This can be
|
|
639
|
-
# turned off with the class parameter @conf[:stem] => false.
|
|
640
|
-
def stem(word)
|
|
641
|
-
@conf[:stem] ? word.stem : word
|
|
642
|
-
end
|
|
643
|
-
|
|
644
|
-
# This method will reset the preceding tag to a sentence ender (PP).
|
|
645
|
-
# This prepares the first word of a new sentence to be tagged correctly.
|
|
646
|
-
def reset
|
|
647
|
-
@conf[:current_tag] = "pp"
|
|
648
|
-
end
|
|
649
|
-
|
|
650
580
|
# Check whether the text is a valid string
|
|
651
581
|
def valid_text(text)
|
|
652
582
|
if !text
|
|
@@ -673,204 +603,6 @@ class EngTagger
|
|
|
673
603
|
downcase ? text.downcase : text
|
|
674
604
|
end
|
|
675
605
|
|
|
676
|
-
# Strip the provided text and separate off any punctuation in preparation for tagging
|
|
677
|
-
def clean_text(text)
|
|
678
|
-
return false unless valid_text(text)
|
|
679
|
-
|
|
680
|
-
cleaned_text = text.encode("utf-8")
|
|
681
|
-
tokenized = []
|
|
682
|
-
# Tokenize the text (splitting on punctuation as you go)
|
|
683
|
-
cleaned_text.split(/\s+/).each do |line|
|
|
684
|
-
tokenized.concat(split_punct(line))
|
|
685
|
-
end
|
|
686
|
-
split_sentences(tokenized)
|
|
687
|
-
end
|
|
688
|
-
|
|
689
|
-
# This handles all of the trailing periods, keeping those that
|
|
690
|
-
# belong on abbreviations and removing those that seem to be
|
|
691
|
-
# at the end of sentences. This method makes some assumptions
|
|
692
|
-
# about the use of capitalization in the incoming text
|
|
693
|
-
def split_sentences(array)
|
|
694
|
-
tokenized = array
|
|
695
|
-
people = %w[jr mr ms mrs dr prof esq sr sen sens rep reps gov attys attys
|
|
696
|
-
supt det mssrs rev]
|
|
697
|
-
army = %w[col gen lt cmdr adm capt sgt cpl maj brig]
|
|
698
|
-
inst = %w[dept univ assn bros ph.d]
|
|
699
|
-
place = %w[arc al ave blvd bld cl ct cres exp expy dist mt mtn ft fy fwy
|
|
700
|
-
hwy hway la pde pd plz pl rd st tce]
|
|
701
|
-
comp = %w[mfg inc ltd co corp]
|
|
702
|
-
state = %w[ala ariz ark cal calif colo col conn del fed fla ga ida id ill
|
|
703
|
-
ind ia kans kan ken ky la me md is mass mich minn miss mo mont
|
|
704
|
-
neb nebr nev mex okla ok ore penna penn pa dak tenn tex ut vt
|
|
705
|
-
va wash wis wisc wy wyo usafa alta man ont que sask yuk]
|
|
706
|
-
month = %w[jan feb mar apr may jun jul aug sep sept oct nov dec]
|
|
707
|
-
misc = %w[vs etc no esp]
|
|
708
|
-
abbr = {}
|
|
709
|
-
[people, army, inst, place, comp, state, month, misc].flatten.each do |i|
|
|
710
|
-
abbr[i] = true
|
|
711
|
-
end
|
|
712
|
-
words = []
|
|
713
|
-
tokenized.each_with_index do |_, i|
|
|
714
|
-
if tokenized[i + 1] && tokenized[i + 1] =~ /[A-Z\W]/ && tokenized[i] =~ /\A(.+)\.\z/
|
|
715
|
-
w = $1
|
|
716
|
-
# Don't separate the period off words that
|
|
717
|
-
# meet any of the following conditions:
|
|
718
|
-
#
|
|
719
|
-
# 1. It is defined in one of the lists above
|
|
720
|
-
# 2. It is only one letter long: Alfred E. Sloan
|
|
721
|
-
# 3. It has a repeating letter-dot: U.S.A. or J.C. Penney
|
|
722
|
-
unless abbr[w.downcase] || [/\A[a-z]\z/i, /[a-z](?:\.[a-z])+\z/i].any? { |r| r.match? w }
|
|
723
|
-
words << w
|
|
724
|
-
words << "."
|
|
725
|
-
next
|
|
726
|
-
end
|
|
727
|
-
end
|
|
728
|
-
words << tokenized[i]
|
|
729
|
-
end
|
|
730
|
-
# If the final word ends in a period..
|
|
731
|
-
if words[-1] && words[-1] =~ /\A(.*\w)\.\z/
|
|
732
|
-
words[-1] = $1
|
|
733
|
-
words.push "."
|
|
734
|
-
end
|
|
735
|
-
words
|
|
736
|
-
end
|
|
737
|
-
|
|
738
|
-
# Separate punctuation from words, where appropriate. This leaves trailing
|
|
739
|
-
# periods in place to be dealt with later. Called by the clean_text method.
|
|
740
|
-
def split_punct(text)
|
|
741
|
-
# If there's no punctuation, return immediately
|
|
742
|
-
return [text] if /\A\w+\z/ =~ text
|
|
743
|
-
|
|
744
|
-
# Sanity checks
|
|
745
|
-
text = text.gsub(/\W{10,}/o, " ")
|
|
746
|
-
|
|
747
|
-
# Put quotes into a standard format
|
|
748
|
-
text = text.gsub(/`(?!`)(?=.*\w)/o, "` ") # Shift left quotes off text
|
|
749
|
-
text = text.gsub(/"(?=.*\w)/o, " `` ") # Convert left quotes to ``
|
|
750
|
-
text = text.gsub(/(\W|^)'(?=.*\w)/o) { $1 ? $1 + " ` " : " ` " } # Convert left quotes to `
|
|
751
|
-
text = text.gsub(/"/, " '' ") # Convert (remaining) quotes to ''
|
|
752
|
-
text = text.gsub(/(\w)'(?!')(?=\W|$)/o) { $1 + " ' " } # Separate right single quotes
|
|
753
|
-
|
|
754
|
-
# Handle all other punctuation
|
|
755
|
-
text = text.gsub(/--+/o, " - ") # Convert and separate dashes
|
|
756
|
-
text = text.gsub(/,(?!\d)/o, " , ") # Shift commas off everything but numbers
|
|
757
|
-
text = text.gsub(/:/o, " : ") # Shift semicolons off
|
|
758
|
-
text = text.gsub(/(\.\.\.+)/o) { " " + $1 + " " } # Shift ellipses off
|
|
759
|
-
text = text.gsub(/([(\[{}\])])/o) { " " + $1 + " " } # Shift off brackets
|
|
760
|
-
text = text.gsub(/([!?#$%;~|])/o) { " " + $1 + " " } # Shift off other ``standard'' punctuation
|
|
761
|
-
|
|
762
|
-
# English-specific contractions
|
|
763
|
-
text = text.gsub(/([A-Za-z])'([dms])\b/o) { $1 + " '" + $2 } # Separate off 'd 'm 's
|
|
764
|
-
text = text.gsub(/n't\b/o, " n't") # Separate off n't
|
|
765
|
-
text = text.gsub(/'(ve|ll|re)\b/o) { " '" + $1 } # Separate off 've, 'll, 're
|
|
766
|
-
text.split(" ")
|
|
767
|
-
end
|
|
768
|
-
|
|
769
|
-
# Given a preceding tag, assign a tag word. Called by the add_tags method.
|
|
770
|
-
# This method is a modified version of the Viterbi algorithm for part-of-speech tagging
|
|
771
|
-
def assign_tag(prev_tag, word)
|
|
772
|
-
case word
|
|
773
|
-
when "-unknown-"
|
|
774
|
-
# classify unknown words accordingly
|
|
775
|
-
return @conf[:unknown_word_tag]
|
|
776
|
-
when "-sym-"
|
|
777
|
-
# If this is a symbol, tag it as a symbol
|
|
778
|
-
return "sym"
|
|
779
|
-
end
|
|
780
|
-
best_so_far = 0
|
|
781
|
-
w = @@lexicon[word] || {}
|
|
782
|
-
t = @@hmm
|
|
783
|
-
|
|
784
|
-
# TAG THE TEXT: What follows is a modified version of the Viterbi algorithm
|
|
785
|
-
# which is used in most POS taggers
|
|
786
|
-
best_tag = ""
|
|
787
|
-
(t[prev_tag] || {}).each_key do |tag|
|
|
788
|
-
# With @config[:relax] set, this method
|
|
789
|
-
# will also include any `open classes' of POS tags
|
|
790
|
-
pw = 0
|
|
791
|
-
if w[tag]
|
|
792
|
-
pw = w[tag]
|
|
793
|
-
elsif @conf[:relax] && tag =~ /\A(?:jj|nn|rb|vb)/
|
|
794
|
-
pw = 0
|
|
795
|
-
else
|
|
796
|
-
next
|
|
797
|
-
end
|
|
798
|
-
|
|
799
|
-
# Bayesian logic:
|
|
800
|
-
# P = P( tag | prev_tag ) * P( tag | word )
|
|
801
|
-
probability = t[prev_tag][tag] * (pw + 1)
|
|
802
|
-
# Set the tag with maximal probability
|
|
803
|
-
if probability > best_so_far
|
|
804
|
-
best_so_far = probability
|
|
805
|
-
best_tag = tag
|
|
806
|
-
end
|
|
807
|
-
end
|
|
808
|
-
best_tag
|
|
809
|
-
end
|
|
810
|
-
|
|
811
|
-
# This method determines whether a word should be considered in its
|
|
812
|
-
# lower or upper case form. This is useful in considering proper nouns
|
|
813
|
-
# and words that begin sentences. Called by add_tags.
|
|
814
|
-
def clean_word(word)
|
|
815
|
-
lcf = lcfirst(word)
|
|
816
|
-
# seen this word as it appears (lower or upper case)
|
|
817
|
-
if @@lexicon[word]
|
|
818
|
-
word
|
|
819
|
-
elsif @@lexicon[lcf]
|
|
820
|
-
# seen this word only as lower case
|
|
821
|
-
lcf
|
|
822
|
-
else
|
|
823
|
-
# never seen this word. guess.
|
|
824
|
-
classify_unknown_word(word)
|
|
825
|
-
end
|
|
826
|
-
end
|
|
827
|
-
|
|
828
|
-
# This changes any word not appearing in the lexicon to identifiable
|
|
829
|
-
# classes of words handled by a simple unknown word classification
|
|
830
|
-
# metric. Called by the clean_word method.
|
|
831
|
-
def classify_unknown_word(word)
|
|
832
|
-
case word
|
|
833
|
-
when /[({\[]/ # Left brackets
|
|
834
|
-
"*LRB*"
|
|
835
|
-
when /[)}\]]/ # Right brackets
|
|
836
|
-
"*RRB*"
|
|
837
|
-
when /-?(?:\d+(?:\.\d*)?|\.\d+)\z/ # Floating point number
|
|
838
|
-
"*NUM*"
|
|
839
|
-
when %r{\A\d+[\d/:-]+\d\z} # Other number constructs
|
|
840
|
-
"*NUM*"
|
|
841
|
-
when /\A-?\d+\w+\z/o # Ordinal number
|
|
842
|
-
"*ORD*"
|
|
843
|
-
when /\A[A-Z][A-Z.-]*\z/o # Abbreviation (all caps)
|
|
844
|
-
"-abr-"
|
|
845
|
-
when /\w-\w/o # Hyphenated word
|
|
846
|
-
/-([^-]+)\z/ =~ word
|
|
847
|
-
h_suffix = $1
|
|
848
|
-
if h_suffix && @@lexicon[h_suffix] && @@lexicon[h_suffix]["jj"]
|
|
849
|
-
# last part of this is defined as an adjective
|
|
850
|
-
"-hyp-adj-"
|
|
851
|
-
else
|
|
852
|
-
# last part of this is not defined as an adjective
|
|
853
|
-
"-hyp-"
|
|
854
|
-
end
|
|
855
|
-
when /\A\W+\z/o
|
|
856
|
-
"-sym-" # Symbol
|
|
857
|
-
when ucfirst(word)
|
|
858
|
-
"-cap-" # Capitalized word
|
|
859
|
-
when /ing\z/o
|
|
860
|
-
"-ing-" # Ends in "ing"
|
|
861
|
-
when /s\z/o
|
|
862
|
-
"-s-" # Ends in "s"
|
|
863
|
-
when /tion\z/o
|
|
864
|
-
"-tion-" # Ends in "tion"
|
|
865
|
-
when /ly\z/o
|
|
866
|
-
"-ly-" # Ends in "ly"
|
|
867
|
-
when /ed\z/o
|
|
868
|
-
"-ed-" # Ends in "ed
|
|
869
|
-
else
|
|
870
|
-
"-unknown-" # Completely unknown
|
|
871
|
-
end
|
|
872
|
-
end
|
|
873
|
-
|
|
874
606
|
# This returns a compiled regexp for extracting maximal noun phrases
|
|
875
607
|
# from a POS-tagged text.
|
|
876
608
|
def get_max_noun_regex
|
|
@@ -889,56 +621,4 @@ class EngTagger
|
|
|
889
621
|
)*
|
|
890
622
|
/xo
|
|
891
623
|
end
|
|
892
|
-
|
|
893
|
-
# Load the 2-grams into a hash from YAML data: This is a naive (but fast)
|
|
894
|
-
# YAML data parser. It will load a YAML document with a collection of key:
|
|
895
|
-
# value entries ( {pos tag}: {probability} ) mapped onto single keys ( {tag} ).
|
|
896
|
-
# Each map is expected to be on a single line; i.e., det: { jj: 0.2, nn: 0.5, vb: 0.0002 }
|
|
897
|
-
def load_tags(lexicon, lexpath = DEFAULT_LEXPATH)
|
|
898
|
-
path = File.join(lexpath, lexicon)
|
|
899
|
-
fh = File.open(path, "r")
|
|
900
|
-
while (line = fh.gets)
|
|
901
|
-
/\A"?([^{"]+)"?: \{ (.*) \}/ =~ line
|
|
902
|
-
next unless $1 && $2
|
|
903
|
-
|
|
904
|
-
key = $1
|
|
905
|
-
data = $2
|
|
906
|
-
items = data.split(/,\s+/)
|
|
907
|
-
pairs = {}
|
|
908
|
-
items.each do |i|
|
|
909
|
-
/([^:]+):\s*(.+)/ =~ i
|
|
910
|
-
pairs[$1] = $2.to_f
|
|
911
|
-
end
|
|
912
|
-
@@hmm[key] = pairs
|
|
913
|
-
end
|
|
914
|
-
fh.close
|
|
915
|
-
end
|
|
916
|
-
|
|
917
|
-
# Load the 2-grams into a hash from YAML data: This is a naive (but fast)
|
|
918
|
-
# YAML data parser. It will load a YAML document with a collection of key:
|
|
919
|
-
# value entries ( {pos tag}: {count} ) mapped onto single keys ( {a word} ).
|
|
920
|
-
# Each map is expected to be on a single line; i.e., key: { jj: 103, nn: 34, vb: 1 }
|
|
921
|
-
def load_words(lexicon, lexpath = DEFAULT_LEXPATH)
|
|
922
|
-
path = File.join(lexpath, lexicon)
|
|
923
|
-
fh = File.open(path, "r")
|
|
924
|
-
while (line = fh.gets)
|
|
925
|
-
/\A"?([^{"]+)"?: \{ (.*) \}/ =~ line
|
|
926
|
-
next unless $1 && $2
|
|
927
|
-
|
|
928
|
-
key = $1
|
|
929
|
-
data = $2
|
|
930
|
-
items = data.split(/,\s+/)
|
|
931
|
-
pairs = {}
|
|
932
|
-
items.each do |i|
|
|
933
|
-
/([^:]+):\s*(.+)/ =~ i
|
|
934
|
-
pairs[$1] = $2.to_f
|
|
935
|
-
end
|
|
936
|
-
@@lexicon[key] = pairs
|
|
937
|
-
end
|
|
938
|
-
fh.close
|
|
939
|
-
end
|
|
940
|
-
|
|
941
|
-
# memoize the stem and assign_tag methods
|
|
942
|
-
memoize("stem")
|
|
943
|
-
memoize("assign_tag")
|
|
944
624
|
end
|
metadata
CHANGED
|
@@ -1,56 +1,35 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: engtagger
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 2.0.0.pre
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yoichiro Hasebe
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
-
dependencies:
|
|
12
|
-
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
requirements:
|
|
16
|
-
- - ">="
|
|
17
|
-
- !ruby/object:Gem::Version
|
|
18
|
-
version: '0'
|
|
19
|
-
type: :runtime
|
|
20
|
-
prerelease: false
|
|
21
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
-
requirements:
|
|
23
|
-
- - ">="
|
|
24
|
-
- !ruby/object:Gem::Version
|
|
25
|
-
version: '0'
|
|
26
|
-
description: A Ruby port of Perl Lingua::EN::Tagger, a probability based, corpus-trained
|
|
27
|
-
tagger that assigns POS tags to English text based on a lookup dictionary and a
|
|
28
|
-
set of probability values.
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: An English part-of-speech tagger with a bundled averaged-perceptron model
|
|
13
|
+
and no runtime dependencies. Tags text and extracts nouns and noun phrases, with
|
|
14
|
+
the methods of the 0.4 line.
|
|
29
15
|
email:
|
|
30
16
|
- yohasebe@gmail.com
|
|
31
17
|
executables: []
|
|
32
18
|
extensions: []
|
|
33
19
|
extra_rdoc_files: []
|
|
34
20
|
files:
|
|
35
|
-
- ".gitattributes"
|
|
36
|
-
- ".gitignore"
|
|
37
|
-
- ".rubocop.yml"
|
|
38
|
-
- ".solargraph.yml"
|
|
39
|
-
- ".yardopts"
|
|
40
21
|
- CHANGELOG.md
|
|
41
|
-
- Gemfile
|
|
42
22
|
- LICENSE
|
|
23
|
+
- NOTICE
|
|
43
24
|
- README.md
|
|
44
|
-
- Rakefile
|
|
45
|
-
- engtagger.gemspec
|
|
46
25
|
- lib/engtagger.rb
|
|
26
|
+
- lib/engtagger/model.etm.gz
|
|
27
|
+
- lib/engtagger/model.rb
|
|
28
|
+
- lib/engtagger/model_builder.rb
|
|
29
|
+
- lib/engtagger/perceptron.rb
|
|
47
30
|
- lib/engtagger/porter.rb
|
|
48
|
-
- lib/engtagger/
|
|
49
|
-
- lib/engtagger/pos_words.hash
|
|
50
|
-
- lib/engtagger/tags.yml
|
|
51
|
-
- lib/engtagger/unknown.yml
|
|
31
|
+
- lib/engtagger/tokenizer.rb
|
|
52
32
|
- lib/engtagger/version.rb
|
|
53
|
-
- lib/engtagger/words.yml
|
|
54
33
|
homepage: https://github.com/yohasebe/engtagger
|
|
55
34
|
licenses:
|
|
56
35
|
- GPL-3.0-only
|
|
@@ -59,13 +38,11 @@ metadata:
|
|
|
59
38
|
changelog_uri: https://github.com/yohasebe/engtagger/blob/master/CHANGELOG.md
|
|
60
39
|
bug_tracker_uri: https://github.com/yohasebe/engtagger/issues
|
|
61
40
|
post_install_message: |
|
|
62
|
-
EngTagger 0
|
|
41
|
+
EngTagger 2.0 is a rewrite: an averaged-perceptron model replaces the
|
|
42
|
+
classic HMM. Tagging and extraction methods keep their names, but tagging
|
|
43
|
+
output differs (it got more accurate) and Ruby >= 3.0 is required.
|
|
63
44
|
|
|
64
|
-
|
|
65
|
-
openly licensed data. It is considerably more accurate, and its tagging
|
|
66
|
-
output differs, so treat it as a rewrite rather than an upgrade.
|
|
67
|
-
|
|
68
|
-
To stay on this line, pin it: gem "engtagger", "~> 0.4"
|
|
45
|
+
For the previous engine and its exact output: gem "engtagger", "~> 0.4"
|
|
69
46
|
rdoc_options: []
|
|
70
47
|
require_paths:
|
|
71
48
|
- lib
|
|
@@ -73,7 +50,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
73
50
|
requirements:
|
|
74
51
|
- - ">="
|
|
75
52
|
- !ruby/object:Gem::Version
|
|
76
|
-
version: '
|
|
53
|
+
version: '3.0'
|
|
77
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
55
|
requirements:
|
|
79
56
|
- - ">="
|
|
@@ -82,5 +59,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
82
59
|
requirements: []
|
|
83
60
|
rubygems_version: 4.0.24
|
|
84
61
|
specification_version: 4
|
|
85
|
-
summary: A
|
|
62
|
+
summary: A corpus-trained English POS tagger in pure Ruby
|
|
86
63
|
test_files: []
|
data/.gitattributes
DELETED
data/.gitignore
DELETED