Class: Kabosu::PosMatcher
- Inherits:
-
Object
- Object
- Kabosu::PosMatcher
- Defined in:
- lib/kabosu/pos_matcher.rb
Class Method Summary collapse
- .adjectives ⇒ Object
- .adverbs ⇒ Object
- .auxiliary_verbs ⇒ Object
-
.nouns ⇒ Object
── Pre-built matchers ──.
- .particles ⇒ Object
- .proper_nouns ⇒ Object
- .verbs ⇒ Object
Instance Method Summary collapse
-
#&(other) ⇒ Object
Intersection: matches if both matchers match.
-
#-(other) ⇒ Object
Difference: matches self but not other.
- #difference(other) ⇒ Object
-
#filter(morphemes) ⇒ Object
Return matching morphemes as an Array.
-
#initialize(*patterns, &block) ⇒ PosMatcher
constructor
Build a matcher from POS patterns or a block.
-
#match?(morpheme_or_pos) ⇒ Boolean
Returns true if the morpheme (or raw POS array) matches this matcher.
-
#reject(morphemes) ⇒ Object
Return non-matching morphemes as an Array.
-
#|(other) ⇒ Object
Union: matches if either matcher matches.
Constructor Details
#initialize(*patterns, &block) ⇒ PosMatcher
Build a matcher from POS patterns or a block.
# From a block
PosMatcher.new { |pos| pos[0] == "名詞" }
# Single pattern (array of strings; "*" or nil = match anything)
PosMatcher.new(["名詞", "固有名詞", "*", "*"])
# Multiple patterns (matches if any pattern matches)
PosMatcher.new(["名詞", "固有名詞"], ["動詞", "*", "*", "*"])
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kabosu/pos_matcher.rb', line 14 def initialize(*patterns, &block) if block raise ArgumentError, "cannot supply both patterns and a block" unless patterns.empty? @proc = block elsif patterns.empty? raise ArgumentError, "must supply at least one pattern or a block" else @patterns = patterns.map(&:freeze).freeze end freeze end |
Class Method Details
.adjectives ⇒ Object
81 82 83 |
# File 'lib/kabosu/pos_matcher.rb', line 81 def self.adjectives @adjectives ||= new(["形容詞"]) end |
.adverbs ⇒ Object
93 94 95 |
# File 'lib/kabosu/pos_matcher.rb', line 93 def self.adverbs @adverbs ||= new(["副詞"]) end |
.auxiliary_verbs ⇒ Object
89 90 91 |
# File 'lib/kabosu/pos_matcher.rb', line 89 def self.auxiliary_verbs @auxiliary_verbs ||= new(["助動詞"]) end |
.nouns ⇒ Object
── Pre-built matchers ──
73 74 75 |
# File 'lib/kabosu/pos_matcher.rb', line 73 def self.nouns @nouns ||= new(["名詞"]) end |
.particles ⇒ Object
85 86 87 |
# File 'lib/kabosu/pos_matcher.rb', line 85 def self.particles @particles ||= new(["助詞"]) end |
.proper_nouns ⇒ Object
97 98 99 |
# File 'lib/kabosu/pos_matcher.rb', line 97 def self.proper_nouns @proper_nouns ||= new(["名詞", "固有名詞"]) end |
.verbs ⇒ Object
77 78 79 |
# File 'lib/kabosu/pos_matcher.rb', line 77 def self.verbs @verbs ||= new(["動詞"]) end |
Instance Method Details
#&(other) ⇒ Object
Intersection: matches if both matchers match.
56 57 58 59 |
# File 'lib/kabosu/pos_matcher.rb', line 56 def &(other) a, b = self, other PosMatcher.new { |pos| a.match?(pos) && b.match?(pos) } end |
#-(other) ⇒ Object
Difference: matches self but not other.
62 63 64 |
# File 'lib/kabosu/pos_matcher.rb', line 62 def -(other) difference(other) end |
#difference(other) ⇒ Object
66 67 68 69 |
# File 'lib/kabosu/pos_matcher.rb', line 66 def difference(other) a, b = self, other PosMatcher.new { |pos| a.match?(pos) && !b.match?(pos) } end |
#filter(morphemes) ⇒ Object
Return matching morphemes as an Array.
40 41 42 |
# File 'lib/kabosu/pos_matcher.rb', line 40 def filter(morphemes) morphemes.select { |m| match?(m) } end |
#match?(morpheme_or_pos) ⇒ Boolean
Returns true if the morpheme (or raw POS array) matches this matcher.
29 30 31 32 33 34 35 36 37 |
# File 'lib/kabosu/pos_matcher.rb', line 29 def match?(morpheme_or_pos) pos = extract_pos(morpheme_or_pos) if @proc @proc.call(pos) else @patterns.any? { |pattern| pattern_match?(pattern, pos) } end end |
#reject(morphemes) ⇒ Object
Return non-matching morphemes as an Array.
45 46 47 |
# File 'lib/kabosu/pos_matcher.rb', line 45 def reject(morphemes) morphemes.reject { |m| match?(m) } end |
#|(other) ⇒ Object
Union: matches if either matcher matches.
50 51 52 53 |
# File 'lib/kabosu/pos_matcher.rb', line 50 def |(other) a, b = self, other PosMatcher.new { |pos| a.match?(pos) || b.match?(pos) } end |