Python 用前面提到的人替换人称代词(喧闹的coref)
我想做一个嘈杂的解析,这样在给出一个个人的prounoun时,这个代词会被前一个(最近的)人替换掉 例如: 亚历克斯正在考虑以10亿美元的价格收购一家英国的初创公司。他非常有信心这会发生。苏桑也处于同样的情况。然而,她已经失去了希望。 输出为: 亚历克斯正在考虑以10亿美元的价格收购一家英国的初创公司。亚历克斯非常有信心这会发生。苏桑也处于同样的情况。然而,苏珊已经失去了希望。 再比如, 彼得是盖茨的朋友。但是盖茨不喜欢他代码> 在这种情况下,输出为: 彼得是盖茨的朋友。但是盖茨不喜欢盖茨。 对!!这太吵了 使用spacy: 我已经使用NER提取了Python 用前面提到的人替换人称代词(喧闹的coref),python,python-3.x,nlp,spacy,coreference-resolution,Python,Python 3.x,Nlp,Spacy,Coreference Resolution,我想做一个嘈杂的解析,这样在给出一个个人的prounoun时,这个代词会被前一个(最近的)人替换掉 例如: 亚历克斯正在考虑以10亿美元的价格收购一家英国的初创公司。他非常有信心这会发生。苏桑也处于同样的情况。然而,她已经失去了希望。 输出为: 亚历克斯正在考虑以10亿美元的价格收购一家英国的初创公司。亚历克斯非常有信心这会发生。苏桑也处于同样的情况。然而,苏珊已经失去了希望。 再比如, 彼得是盖茨的朋友。但是盖茨不喜欢他 在这种情况下,输出为: 彼得是盖茨的朋友。但是盖茨不喜欢盖茨。 对!!这
Person
,但是如何适当地替换代词呢
代码:
我为您的两个示例编写了一个函数: 考虑使用更大的模型,例如
en_core\u web\u lg
,以获得更精确的标记
import spacy
from string import punctuation
nlp = spacy.load("en_core_web_lg")
def pronoun_coref(text):
doc = nlp(text)
pronouns = [(tok, tok.i) for tok in doc if (tok.tag_ == "PRP")]
names = [(ent.text, ent[0].i) for ent in doc.ents if ent.label_ == 'PERSON']
doc = [tok.text_with_ws for tok in doc]
for p in pronouns:
replace = max(filter(lambda x: x[1] < p[1], names),
key=lambda x: x[1], default=False)
if replace:
replace = replace[0]
if doc[p[1] - 1] in punctuation:
replace = ' ' + replace
if doc[p[1] + 1] not in punctuation:
replace = replace + ' '
doc[p[1]] = replace
doc = ''.join(doc)
return doc
导入空间
从字符串导入标点符号
nlp=空间负荷(“核心网络负荷”)
def代词_coref(文本):
doc=nlp(文本)
doc if中tok的代词=[(tok,tok.i)(tok.tag==“PRP”)]
name=[(ent.text,ent[0].i)对于doc.ents中的ent,如果ent.label==“PERSON”]
doc=[tok.text_,带文档中tok的w]
对于代词中的p:
替换=最大值(过滤器(λx:x[1]
有专门的库来解决相互引用问题。请参见下面的最小可复制示例:
import spacy
import neuralcoref
nlp = spacy.load('en_core_web_sm')
neuralcoref.add_to_pipe(nlp)
doc = nlp(
'''Alex is looking at buying a U.K. startup for $1 billion.
He is very confident that this is going to happen.
Sussan is also in the same situation.
However, she has lost hope.
Peter is a friend of Gates. But Gates does not like him.
''')
print(doc._.coref_resolved)
Alex is looking at buying a U.K. startup for $1 billion.
Alex is very confident that this is going to happen.
Sussan is also in the same situation.
However, Sussan has lost hope.
Peter is a friend of Gates. But Gates does not like Peter.
注意,如果您pip安装了neuralRef,您可能会遇到一些问题,因此最好从源代码构建它,正如我所概述的
import spacy
import neuralcoref
nlp = spacy.load('en_core_web_sm')
neuralcoref.add_to_pipe(nlp)
doc = nlp(
'''Alex is looking at buying a U.K. startup for $1 billion.
He is very confident that this is going to happen.
Sussan is also in the same situation.
However, she has lost hope.
Peter is a friend of Gates. But Gates does not like him.
''')
print(doc._.coref_resolved)
Alex is looking at buying a U.K. startup for $1 billion.
Alex is very confident that this is going to happen.
Sussan is also in the same situation.
However, Sussan has lost hope.
Peter is a friend of Gates. But Gates does not like Peter.