问题描述
我想做什么
我想使用Python 3.6和spaCy neurocoref将代词替换为以下名词。
#input
'My sister has a dog. She loves him.'
'Angela lives in Boston. She is quite happy in that city.'
#output
'My sister has a dog. My sister loves a dog.'
'Angela lives in Boston. Angela is quite happy in Boston.'
错误
如何解决该错误以获取适当的输出? 如果您有任何想法,请与我分享。
AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'replace'
当前代码
使用neuralcoref
的示例代码是以下URL:https://spacy.io/universe/project/neuralcoref
import spacy
import neuralcoref
nlp = spacy.load('en')
neuralcoref.add_to_pipe(nlp)
doc1 = nlp('My sister has a dog. She loves him.')
print(doc1._.coref_clusters)
print(doc1._.coref_clusters[0][1])
print(len(doc1._.coref_clusters))
for i in range(1,len(doc1._.coref_clusters)+1):
doc_new = doc1.replace(doc1._.coref_clusters[0][i],doc1._.coref_clusters[1][i])
print(doc_new)
#output
[My sister: [My sister,She],a dog: [a dog,him]]
She
2
解决方法
neuralcoref
具有专门用于以下任务的doc._.coref_resolved
方法:
import spacy
import neuralcoref
nlp = spacy.load('en_core_web_sm')
neuralcoref.add_to_pipe(nlp)
texts = ['My sister has a dog. She loves him.','Angela lives in Boston. She is quite happy in that city.']
docs = nlp.pipe(texts)
inp = []
out = []
for doc in docs:
inp.append(doc.text)
out.append(doc._.coref_resolved)
# desired output
print("Input:",inp)
print("Output:",out)
Input: ['My sister has a dog. She loves him.','Angela lives in Boston. She is quite happy in that city.']
Output: ['My sister has a dog. My sister loves a dog.','Angela lives in Boston. Angela is quite happy in Boston.']