类型错误:add() 恰好需要 2 个位置参数(给定 3 个)

Plu*_*hil 5 python nlp spacy

为什么我收到此错误任何人都可以告诉我或使用简单的示例解释如何使用它

---------------------------------------------------------------------------
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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在下面的链接中 https://spacy.io/api/matcher 在此输入图像描述

小智 9

新版本的 spacy需要在模式周围加上方括号。此外,回调也成为一个可选术语。

因此,在您的情况下,您只需要:

from spacy.matcher import Matcher

matcher = Matcher(nlp.vocab)
matcher.add("matching_1", [pattern])
matches = matcher(doc)
Run Code Online (Sandbox Code Playgroud)


小智 3

你传递这个是None为了什么?看起来你只需要:

matcher.add("matching_1", pattern)
Run Code Online (Sandbox Code Playgroud)

您收到错误是因为该函数需要 2 个未命名参数,但您试图传递 3 个参数。如果您还想传递回调函数,则需要编写:

matcher.add("matching_1", pattern, on_match = my_callback_function)
Run Code Online (Sandbox Code Playgroud)