class Spacy::Matcher
See also spaCy Python API document for [`Matcher`](spacy.io/api/matcher).
Attributes
py_matcher[R]
@return [Object] a Python `Matcher` instance accessible via `PyCall`
Public Class Methods
new(nlp)
click to toggle source
Creates a {Matcher} instance @param nlp [Language] an instance of {Language} class
# File lib/ruby-spacy.rb, line 322 def initialize(nlp) @py_matcher = PyMatcher.(nlp.vocab) end
Public Instance Methods
add(text, pattern)
click to toggle source
Adds a label string and a text pattern. @param text [String] a label string given to the pattern @param pattern [Array<Array<Hash>>] sequences of text patterns that are alternative to each other
# File lib/ruby-spacy.rb, line 329 def add(text, pattern) @py_matcher.add(text, pattern) end
match(doc)
click to toggle source
Execute the match. @param doc [Doc] an {Doc} instance @return [Array<Hash{:match_id => Integer, :start_index => Integer, :end_index => Integer}>] the id of the matched pattern, the starting position, and the end position
# 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