【问题标题】:TypeError: add() takes exactly 2 positional arguments (3 given)TypeError: add() 正好采用 2 个位置参数(给定 3 个)
【发布时间】:2021-12-12 07:18:18
【问题描述】:

为什么会出现这个错误,谁能告诉我 或者用简单的例子告诉我如何使用它

---------------------------------------------------------------------------
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)

在下面的链接中 https://spacy.io/api/matcher

【问题讨论】:

  • 如您截屏的文档中所述,您使用的是 v2 调用样式,但该 API 在 spaCy v3 中发生了更改。 ljdyer的回答是对的。

标签: python nlp spacy


【解决方案1】:

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

matcher.add("matching_1", pattern)

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

matcher.add("matching_1", pattern, on_match = my_callback_function)

【讨论】:

    【解决方案2】:

    这对我有用 如果您有其他答案,请随时回答

    # 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)
    

    【讨论】:

      猜你喜欢
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      相关资源
      最近更新 更多