PhraseMatcher SpaCy属性错误:“ spacy.matcher.phrasematcher.PhraseMatcher”对象属性“ add”为只读

时间:2019-10-03 06:38:19

标签: python

大家好

我使用Spacy PhraseMatcher为多个标签在文本中进行筛选。

e.g Steve Jobs is the CEO of Apple.

因此,我正在尝试获取此输出 Steve Jobs (PERSON), CEO (POSITION) and Apple (ENTITY)

我收到此错误-之前有人遇到过这个错误吗?

AttributeError:'spacy.matcher.phrasematcher.PhraseMatcher'对象属性'add'是只读的

谢谢...

1 个答案:

答案 0 :(得分:0)

PhraseMatcher.add is a method

matcher.add('POSITION', None, *position)
matcher.add('LOCATION', None, *location)
matcher.add('PERSON', None ,*person)

已修复其他错误的工作示例

import spacy
from spacy.matcher import PhraseMatcher
from spacy.lang.en import English
import json

nlp = spacy.load('en_core_web_sm')
matcher = PhraseMatcher(nlp.vocab, attr = "LOWER")

TEXTS = "Steve Jobs is the CEO of Apple"
nlp = English(nlp.vocab)
position = [nlp(text) for text in ('CEO','CFO','COO')]
company =[nlp(text) for text in ('APPLE','GOOGLE','TWITTER')]
person = [nlp(text) for text in ('STEVE JOBS', 'LARRY PAGE', 'JACK SPARROW')]

matcher.add('POSITION', None, *position)
matcher.add('LOCATION', None, *company)
matcher.add('PERSON', None ,*person)

doc = nlp(TEXTS)
matches = matcher(doc)
for match_id, start, end in matches:
    rule_id = nlp.vocab.strings[match_id]
    span = doc [start:end] #matched span
    print (match_id,rule_id, start,end, span.text)