嗨,大家好
我可以使用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'是只读的
谢谢...
最佳答案
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)
关于python - PhraseMatcher SpaCy属性错误:“spacy.matcher.phrasematcher.PhraseMatcher”对象属性“add”为只读,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58213550/