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

Parameters:



903
904
905
# File 'lib/ruby-spacy.rb', line 903

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

Instance Attribute Details

#py_matcher ⇒ Object (readonly)

Returns a Python Matcher instance accessible via PyCall.

Returns:

  • (Object) —

    a Python Matcher instance accessible via PyCall



899
900
901
# File 'lib/ruby-spacy.rb', line 899

def py_matcher
  @py_matcher
end

Instance Method Details

#add(text, pattern) ⇒ Object

Adds a label string and a text pattern.

Parameters:

  • text (String) —

    a label string given to the pattern

  • pattern (Array<Array<Hash>>) —

    sequences of text patterns that are alternative to each other



910
911
912
# File 'lib/ruby-spacy.rb', line 910

def add(text, pattern)
  @py_matcher.add(text, pattern)
end

#match(doc) ⇒ Array<Hash{:match_id => Integer, :start_index => Integer, :end_index => Integer, :label => String}>

Execute the match.

Parameters:

  • doc (Doc) —

    an Doc instance

Returns:

  • (Array<Hash{:match_id => Integer, :start_index => Integer, :end_index => Integer, :label => String}>) —

    the id of the matched pattern, the starting position, the end position, and the label string of the matched pattern



917
918
919
920
921
# File 'lib/ruby-spacy.rb', line 917

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