Class: Spacy::Matcher
- Inherits:
-
Object
- Object
- Spacy::Matcher
- Defined in:
- lib/ruby-spacy.rb
Overview
See also spaCy Python API document for [‘Matcher`](spacy.io/api/matcher).
Instance Attribute Summary collapse
-
#py_matcher ⇒ Object
readonly
A Python ‘Matcher` instance accessible via `PyCall`.
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
488 489 490 |
# File 'lib/ruby-spacy.rb', line 488 def initialize(nlp) @py_matcher = PyMatcher.call(nlp.vocab) end |
Instance Attribute Details
#py_matcher ⇒ Object (readonly)
484 485 486 |
# File 'lib/ruby-spacy.rb', line 484 def py_matcher @py_matcher end |
Instance Method Details
#add(text, pattern) ⇒ Object
Adds a label string and a text pattern.
495 496 497 |
# File 'lib/ruby-spacy.rb', line 495 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.
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/ruby-spacy.rb', line 502 def match(doc) str_results = @py_matcher.call(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 |