Reputation: 101
I am getting an error when trying to use spacy matcher
:
~\Anaconda3\lib\site-packages\spacy\matcher\matcher.pyx in spacy.matcher.matcher.Matcher.add()
TypeError: add() takes exactly 2 positional arguments (3 given)
Is there any alternate function for spacy.matcher.matcher.Matcher.add()
?
Upvotes: 10
Views: 8698
Reputation: 8258
The Error message clearly says that you should have only 2 arguments, So just
matcher.add("xxxxxxx", ['pattern1', 'pattern2'])
NOTE: pattern should be a list or it will throw error
Upvotes: 0
Reputation: 1216
The pattern should be surrounded by 2 outside square brackets to work (bold for clarification). It then works and finds pattern in text
pattern = [[{"TEXT": "iPhone"}, {"TEXT": "X"}]]
matcher.add("IPHONE_X_PATTERN", pattern)
Upvotes: -1
Reputation: 893
Instead of using matcher.add('Relation_name', None, pattern)
You can use: matcher.add('Relation_name', [pattern], on_match=None)
Upvotes: 7
Reputation: 549
In addition, if you have multiple patterns to be extracted, an example would be as below.
import spacy
nlp = spacy.load('en_core_web_sm')
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
pattern1 = [{'LOWER':'solarpower'}]
pattern2 = [{'LOWER':'solar'},{'IS_PUNCT':True},{'LOWER':'power'}]
pattern3 = [{'LOWER':'solar'},{'LOWER':'power'}]
matcher.add('SolarPower', [pattern1,pattern2,pattern3])
doc = nlp(u"The Solar Power industry continues to grow a solarpower increases. Solar-power is good")
found_matches = matcher(doc)
for _,start,end in found_matches:
span = doc[start:end]
print(span)
Solar Power
solarpower
Solar-power
Upvotes: 3
Reputation: 627128
See the SpaCy Matcher.add()
documentation:
Changed in v3.0
As of spaCy v3.0,Matcher.add
takes a list of patterns as the second argument (instead of a variable number of arguments). Theon_match
callback becomes an optional keyword argument.
patterns = [[{"TEXT": "Google"}, {"TEXT": "Now"}], [{"TEXT": "GoogleNow"}]]
- matcher.add("GoogleNow", on_match, *patterns)
+ matcher.add("GoogleNow", patterns, on_match=on_match)
Example usage:
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
pattern = [{"LOWER": "hello"}, {"LOWER": "world"}]
matcher.add("HelloWorld", [pattern])
doc = nlp("hello world!")
matches = matcher(doc)
Upvotes: 11