gpt4 book ai didi

python - PhraseMatcher SpaCy 属性错误 : 'spacy.matcher.phrasematcher.PhraseMatcher' object attribute 'add' is read-only

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:14 24 4
gpt4 key购买 nike

大家好

我使用 Spacy PhraseMatcher 筛选文本以获取多个标签。

例如史蒂夫·乔布斯是苹果公司的首席执行官。

所以我想得到这个输出史蒂夫·乔布斯(人员)、首席执行官(职位)和苹果(实体)

我收到此错误 - 有人以前遇到过此错误吗?

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' object attribute 'add' is read-only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58213550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com