简体   繁体   English

如何修复错误以使用库实现引用解析?

[英]How can I fix the bug to realize reference resolution using a library?

what I would like to do我想做什么

I would like to replace pronouns to nouns like below using Python 3.6 and spaCy neuralcoref.我想使用 Python 3.6 和 spaCy neuralcoref 将代词替换为如下所示的名词。

#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.'

error错误

How can I fix the bug to acquire appropriate output?如何修复错误以获取适当的 output? If you have any ideas, please share them with me.如果您有任何想法,请与我分享。

AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'replace'

current code当前代码

The sample code of the use of neuralcoref is the following URL: https://spacy.io/universe/project/neuralcoref使用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 has a specially dedicated doc._.coref_resolved method for tasks like this:对于这样的任务, 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.']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM