Class: Spacy::Matcher
- Inherits:
-
Object
- Object
- Spacy::Matcher
- Defined in:
- lib/ruby-spacy.rb
Overview
See also spaCy Python API document for Matcher.
Instance Attribute Summary collapse
-
#py_matcher ⇒ Object
readonly
A Python
Matcherinstance accessible viaPyCall.
Instance Method Summary collapse
-
#add(text, pattern) ⇒ Object
Adds a label string and a text pattern.
-
#initialize(nlp) ⇒ Matcher
constructor
Creates a Matcher instance.
-
#match(doc) ⇒ Array<Hash{:match_id => Integer, :start_index => Integer, :end_index => Integer}>
Execute the match.
Constructor Details
#initialize(nlp) ⇒ Matcher
Creates a Spacy::Matcher instance
322 323 324 |
# File 'lib/ruby-spacy.rb', line 322 def initialize(nlp) @py_matcher = PyMatcher.(nlp.vocab) end |
Instance Attribute Details
#py_matcher ⇒ Object (readonly)
Returns a Python Matcher instance accessible via PyCall.
318 319 320 |
# File 'lib/ruby-spacy.rb', line 318 def py_matcher @py_matcher end |
Instance Method Details
#add(text, pattern) ⇒ Object
Adds a label string and a text pattern.
329 330 331 |
# File 'lib/ruby-spacy.rb', line 329 def add(text, pattern) @py_matcher.add(text, pattern) end |
#match(doc) ⇒ Array<Hash{:match_id => Integer, :start_index => Integer, :end_index => Integer}>
Execute the match.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/ruby-spacy.rb', line 336 def match(doc) str_results = @py_matcher.(doc.py_doc).to_s s = StringScanner.new(str_results[1..-2]) results = [] while s.scan_until(/(\d+), (\d+), (\d+)/) next unless s.matched triple = s.matched.split(", ") match_id = triple[0].to_i start_index = triple[1].to_i end_index = triple[2].to_i - 1 results << {match_id: match_id, start_index: start_index, end_index: end_index} end results end |