'TypeError: add() takes exactly 2 positional arguments (3 given)
Why I am getting this error Can anyone tell please or explain me how to use it using simple example
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_33/3577035061.py in <module>
6 # Matcher class object
7 matcher = Matcher(nlp.vocab)
----> 8 matcher.add("matching_1", None, pattern)
9
10 matches = matcher(doc)
/opt/conda/lib/python3.7/site-packages/spacy/matcher/matcher.pyx in spacy.matcher.matcher.Matcher.add()
TypeError: add() takes exactly 2 positional arguments (3 given)
In Below link https://spacy.io/api/matcher
Solution 1:[1]
What are you passing the None
for? It looks like you just need:
matcher.add("matching_1", pattern)
You are getting the error because the function takes 2 unnamed arguments but you are trying to pass 3. If you want to pass a callback function as well you would need to write:
matcher.add("matching_1", pattern, on_match = my_callback_function)
Solution 2:[2]
The new version of spacy needs square brackets around pattern. Also, callback became an optional term.
Therefore in your case you just need:
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
matcher.add("matching_1", [pattern])
matches = matcher(doc)
Solution 3:[3]
This works for me If you anyother answer feel free to answer
# load spaCy model
nlp = spacy.load("en_core_web_sm")
text = "GDP in developing countries such as Vietnam will continue growing at a high rate."
# create a spaCy object
doc = nlp(text)
#define the pattern
pattern = [[{'POS':'NOUN'},
{'LOWER': 'such'},
{'LOWER': 'as'},
{'POS': 'PROPN'}] ]#proper noun
# Matcher class object
matcher = Matcher(nlp.vocab)
matcher.add("matching_1",patterns=pattern)
matches = matcher(doc)
span = doc[matches[0][1]:matches[0][2]]
print(span.text)
Solution 4:[4]
matcher.add("matching_1", [pattern])
This worked for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ljdyer |
Solution 2 | user19129889 |
Solution 3 | PlutoSenthil |
Solution 4 | Kshitij K |