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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af1551df132a2b3d992f8de0bdb2cefa7ddbba8a5a8e06da200dad9ef6f0d909
4
- data.tar.gz: d434df66126fad9b79fb896e527d0e30ab60f920a7eefd1ea37fc2fab8a64503
3
+ metadata.gz: 5ed56eae37e207da87cd709f4e8d5861912ece8ac0f6027fbc79be6341f03b94
4
+ data.tar.gz: 025e100f05c4a9bd034c279b6d4e7a663be6ee755aacad030b37acc1a745dd48
5
5
  SHA512:
6
- metadata.gz: 58647f6531a2a8271144e4a928af6abb477b4039386f94208f4eb841eaad2e309aafbbd23e758994640e777af0acc9c4b3c1b0b29cb735b6ee2330ee18696098
7
- data.tar.gz: 1dde796134c2a1f6a6403be9f49d4944ee993abe95d4d470d50c2bcc995a65bf19c32c90b56143529d64991871fd9aa7bc3ad99180edaecf403d99175fb9b00c
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`, `assign_tag`, `EngTagger.hmm`, `EngTagger.lexicon` and the `DEFAULT_*PATH` constants.
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`, `assign_tag`, `EngTagger.hmm`, `EngTagger.lexicon` and
214
- the `DEFAULT_LEXPATH`, `DEFAULT_WORDPATH` and `DEFAULT_TAGPATH` constants were
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
 
@@ -21,17 +21,28 @@ class EngTagger
21
21
 
22
22
  DEFAULT_MODEL = File.join(File.dirname(__FILE__), "model.etm.gz")
23
23
 
24
- # PTB/UD tag -> classic EngTagger tag. The classic tagset renames a few
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
- LEGACY_TAGS = {
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
- # UD additions with no classic equivalent
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
- }.freeze
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class EngTagger
4
- VERSION = "2.0.0.pre"
4
+ VERSION = "2.0.0.pre2"
5
5
  end
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
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: engtagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre
4
+ version: 2.0.0.pre2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoichiro Hasebe