Class: Spacy::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-spacy.rb

Overview

See also spaCy Python API document for Matcher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nlp) ⇒ Matcher

Creates a Spacy::Matcher instance



730
731
732
# File 'lib/ruby-spacy.rb', line 730

def initialize(nlp)
  @py_matcher = PyMatcher.call(nlp.vocab)
end

Instance Attribute Details

#py_matcherObject (readonly)



726
727
728
# File 'lib/ruby-spacy.rb', line 726

def py_matcher
  @py_matcher
end

Instance Method Details

#add(text, pattern) ⇒ Object

Adds a label string and a text pattern.



737
738
739
# File 'lib/ruby-spacy.rb', line 737

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.



744
745
746
747
748
# File 'lib/ruby-spacy.rb', line 744

def match(doc)
  PyCall::List.call(@py_matcher.call(doc.py_doc)).map do |py_match|
    { match_id: py_match[0].to_i, start_index: py_match[1].to_i, end_index: py_match[2].to_i - 1 }
  end
end