engtagger 2.0.0.pre → 2.0.0.pre2
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 +2 -1
- data/README.md +5 -3
- data/lib/engtagger/perceptron.rb +31 -5
- data/lib/engtagger/version.rb +1 -1
- data/lib/engtagger.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5ed56eae37e207da87cd709f4e8d5861912ece8ac0f6027fbc79be6341f03b94
|
|
4
|
+
data.tar.gz: 025e100f05c4a9bd034c279b6d4e7a663be6ee755aacad030b37acc1a745dd48
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 484c608422467566ae50f14f4cfb6995e180b2188ebef5ee48ea26ce106c0b2e96e56c82aa9b94ff7abaae7d43786ff7830a8058ce8f8e5f278afe97f153ac76
|
|
7
|
+
data.tar.gz: f0c00c4cac25f8a9d8173968716e057f685752b98884de6730ca76bbfc8455fb9bbc04f7dab822adcaefe8b5d3b3c0beade6236f02b98b1a89432e25d0d373e6
|
data/CHANGELOG.md
CHANGED
|
@@ -7,7 +7,8 @@ A rewrite of the tagging engine. Tagging and tokenization output differ from 0.4
|
|
|
7
7
|
- Replace the HMM tagger with an averaged perceptron and a bundled model trained on UD English-EWT and automatically tagged Wikipedia, Wikinews and Stack Exchange text (see NOTICE).
|
|
8
8
|
- Add a PTB/UD-compatible tokenizer whose tokens are substrings of the input.
|
|
9
9
|
- Add the `tagset: :ptb` option, which makes `tag_pairs` and `get_readable` emit standard Penn Treebank tags.
|
|
10
|
-
- Remove the HMM probability tables derived from the Penn Treebank, `install`, `
|
|
10
|
+
- Remove the HMM probability tables derived from the Penn Treebank, `install`, `EngTagger.hmm`, `EngTagger.lexicon` and the `DEFAULT_*PATH` constants.
|
|
11
|
+
- Keep `assign_tag(prev_tag, word)`, which now tags one word with the bundled model.
|
|
11
12
|
- Ignore the configuration keys of the HMM engine (`:relax`, `:unknown_word_tag`, lexicon paths) with a warning.
|
|
12
13
|
- Remove the `sin_lru_redux` dependency; the gem has no runtime dependencies.
|
|
13
14
|
- Require Ruby >= 3.0.
|
data/README.md
CHANGED
|
@@ -210,9 +210,11 @@ engine, so tagged output is not byte-identical to 0.4.x. Tokenization also
|
|
|
210
210
|
follows PTB/UD conventions more closely (e.g. `don't` splits as `do|n't`).
|
|
211
211
|
Configuration keys of the retired HMM engine
|
|
212
212
|
(`:relax`, `:unknown_word_tag`, lexicon paths) are accepted and ignored with
|
|
213
|
-
a warning; `install`, `
|
|
214
|
-
|
|
215
|
-
removed.
|
|
213
|
+
a warning; `install`, `EngTagger.hmm`, `EngTagger.lexicon` and the
|
|
214
|
+
`DEFAULT_LEXPATH`, `DEFAULT_WORDPATH` and `DEFAULT_TAGPATH` constants were
|
|
215
|
+
removed. `assign_tag(prev_tag, word)` still tags one word from the tag before
|
|
216
|
+
it, but it does not see the surrounding words; use `tag_pairs` or `add_tags`
|
|
217
|
+
wherever the whole text is available.
|
|
216
218
|
|
|
217
219
|
If you need the old engine's exact output, pin the previous line:
|
|
218
220
|
|
data/lib/engtagger/perceptron.rb
CHANGED
|
@@ -21,17 +21,28 @@ class EngTagger
|
|
|
21
21
|
|
|
22
22
|
DEFAULT_MODEL = File.join(File.dirname(__FILE__), "model.etm.gz")
|
|
23
23
|
|
|
24
|
-
# PTB
|
|
24
|
+
# PTB tag -> classic EngTagger tag. The classic tagset renames a few
|
|
25
25
|
# categories for XML-tag safety (DET for DT, PRPS for PRP$) and splits
|
|
26
26
|
# punctuation into named classes.
|
|
27
|
-
|
|
27
|
+
RENAMED_TAGS = {
|
|
28
28
|
"DT" => "det", "PRP$" => "prps", "WP$" => "wps", "PRP" => "prp",
|
|
29
29
|
"." => "pp", "," => "ppc", ":" => "pps", "``" => "ppl", "''" => "ppr",
|
|
30
|
-
"$" => "ppd", "#" => "ppd", "-LRB-" => "lrb", "-RRB-" => "rrb"
|
|
31
|
-
|
|
30
|
+
"$" => "ppd", "#" => "ppd", "-LRB-" => "lrb", "-RRB-" => "rrb"
|
|
31
|
+
}.freeze
|
|
32
|
+
|
|
33
|
+
# PTB/UD tag -> classic EngTagger tag, including the UD tags that have no
|
|
34
|
+
# classic equivalent.
|
|
35
|
+
LEGACY_TAGS = RENAMED_TAGS.merge(
|
|
32
36
|
"HYPH" => "ppc", "NFP" => "sym", "AFX" => "jj", "ADD" => "nn",
|
|
33
37
|
"GW" => "nn", "XX" => "sym"
|
|
34
|
-
|
|
38
|
+
).freeze
|
|
39
|
+
|
|
40
|
+
# Classic EngTagger tag -> PTB tag, for the renamed categories.
|
|
41
|
+
PTB_TAGS = RENAMED_TAGS.each_with_object({}) { |(ptb, classic), h| h[classic] ||= ptb }.freeze
|
|
42
|
+
|
|
43
|
+
# Features that read the words and tags around a token.
|
|
44
|
+
LEFT_CONTEXT = /\A(?:i first\z|i-1 (?:word|suffix) |i-2 |i tag\+i-2 )/
|
|
45
|
+
RIGHT_CONTEXT = /\Ai\+/
|
|
35
46
|
|
|
36
47
|
attr_reader :model
|
|
37
48
|
|
|
@@ -64,6 +75,16 @@ class EngTagger
|
|
|
64
75
|
end
|
|
65
76
|
end
|
|
66
77
|
|
|
78
|
+
# Tag one word from the tag before it alone, without the words around it.
|
|
79
|
+
# prev: PTB tag of the preceding token, or nil at the start of a sentence.
|
|
80
|
+
def tag_word(word, prev = nil)
|
|
81
|
+
known = @tagdict[normalize(word)] and return known
|
|
82
|
+
|
|
83
|
+
feats = features(0, word, build_context([word]), prev || START[0], START[1])
|
|
84
|
+
feats.reject! { |f| f.match?(RIGHT_CONTEXT) || (prev && f.match?(LEFT_CONTEXT)) }
|
|
85
|
+
predict(feats, Array.new(@classes.size, 0))
|
|
86
|
+
end
|
|
87
|
+
|
|
67
88
|
# XML-style output, matching the classic engine's shape:
|
|
68
89
|
# "<nnp>Alice</nnp> <vbd>chased</vbd> ..."
|
|
69
90
|
#
|
|
@@ -86,6 +107,11 @@ class EngTagger
|
|
|
86
107
|
LEGACY_TAGS[ptb_tag] || ptb_tag.downcase
|
|
87
108
|
end
|
|
88
109
|
|
|
110
|
+
# PTB equivalent of a classic tag: det -> DT, pp -> ".", nn -> NN.
|
|
111
|
+
def ptb_tag(classic_tag)
|
|
112
|
+
PTB_TAGS[classic_tag] || classic_tag.upcase
|
|
113
|
+
end
|
|
114
|
+
|
|
89
115
|
private
|
|
90
116
|
|
|
91
117
|
def normalize(word)
|
data/lib/engtagger/version.rb
CHANGED
data/lib/engtagger.rb
CHANGED
|
@@ -209,6 +209,20 @@ class EngTagger
|
|
|
209
209
|
end.join(" ")
|
|
210
210
|
end
|
|
211
211
|
|
|
212
|
+
# Tag one word from the tag before it, in the classic tagset. The words
|
|
213
|
+
# around it are not seen, so `tag_pairs` and `add_tags` are more accurate.
|
|
214
|
+
#
|
|
215
|
+
# @param prev_tag [String, nil] the classic tag of the preceding word; "pp"
|
|
216
|
+
# or nil at the start of a sentence
|
|
217
|
+
# @param word [String] a single token
|
|
218
|
+
# @return [String] the classic tag
|
|
219
|
+
#
|
|
220
|
+
def assign_tag(prev_tag, word)
|
|
221
|
+
prev = prev_tag.to_s
|
|
222
|
+
prev = nil if prev.empty? || prev == "pp"
|
|
223
|
+
@tagger.legacy_tag(@tagger.tag_word(word.encode(Encoding::UTF_8), prev && @tagger.ptb_tag(prev)))
|
|
224
|
+
end
|
|
225
|
+
|
|
212
226
|
# Given a text string, return as many nouns and noun phrases as possible.
|
|
213
227
|
# Applies add_tags and involves three stages:
|
|
214
228
|
#
|