我正在尝试做回指解析,下面是我的代码。在
首先,我浏览到下载斯坦福模块的文件夹。然后我在命令提示符下运行命令来初始化stanford nlp模块
java -mx4g -cp "*;stanford-corenlp-full-2017-06-09/*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000
之后,我用Python执行下面的代码
^{pr2}$我想把Tom is a smart boy. He know a lot of thing.
改成Tom is a smart boy. Tom know a lot of thing.
,Python中没有教程或任何帮助。在
我所能做的就是用下面的Python代码进行注释
共指分辨率
output = nlp.annotate(sentence, properties={'annotators':'dcoref','outputFormat':'json','ner.useSUTime':'false'})
通过解析coref
coreferences = output['corefs']
我在JSON下面
coreferences
{u'1': [{u'animacy': u'ANIMATE',
u'endIndex': 2,
u'gender': u'MALE',
u'headIndex': 1,
u'id': 1,
u'isRepresentativeMention': True,
u'number': u'SINGULAR',
u'position': [1, 1],
u'sentNum': 1,
u'startIndex': 1,
u'text': u'Tom',
u'type': u'PROPER'},
{u'animacy': u'ANIMATE',
u'endIndex': 6,
u'gender': u'MALE',
u'headIndex': 5,
u'id': 2,
u'isRepresentativeMention': False,
u'number': u'SINGULAR',
u'position': [1, 2],
u'sentNum': 1,
u'startIndex': 3,
u'text': u'a smart boy',
u'type': u'NOMINAL'},
{u'animacy': u'ANIMATE',
u'endIndex': 2,
u'gender': u'MALE',
u'headIndex': 1,
u'id': 3,
u'isRepresentativeMention': False,
u'number': u'SINGULAR',
u'position': [2, 1],
u'sentNum': 2,
u'startIndex': 1,
u'text': u'He',
u'type': u'PRONOMINAL'}],
u'4': [{u'animacy': u'INANIMATE',
u'endIndex': 7,
u'gender': u'NEUTRAL',
u'headIndex': 4,
u'id': 4,
u'isRepresentativeMention': True,
u'number': u'SINGULAR',
u'position': [2, 2],
u'sentNum': 2,
u'startIndex': 3,
u'text': u'a lot of thing',
u'type': u'NOMINAL'}]}
有什么帮助吗?在
这里有一个可能的解决方案,它使用CoreNLP输出的数据结构。提供所有信息。这不是一个完整的解决方案,可能需要扩展来处理所有情况,但这是一个很好的起点。在
这将产生以下输出:
^{pr2}$正如你所看到的,当代词有一个句子首字母(标题大小写)先行词时,这个解决方案并不处理大小写的更正(“大猫咪”而不是最后一个句子中的“大猫”)。这取决于先行词的类别-普通名词先行词需要小写,而专有名词先行词则不需要。 其他一些特殊的处理可能是必要的(比如我测试句子中的所有格)。它还假定您不希望重用原始输出标记,因为它们是由此代码修改的。一种解决方法是复制原始数据结构或创建一个新属性,并相应地更改
print_resolved
函数。 纠正任何分辨率错误也是另一个挑战!在我也有类似的问题。在尝试了核心nlp之后,我用neural coref解决了这个问题。通过使用以下代码,您可以轻松地通过neural coref完成这项工作:
导入空间
nlp=空间负荷('en'u coref'u md')
doc=nlp(只有满足以下所有条件时,u’电话区号才有效。不能为空。它应该是数字。不能小于200。最小位数应为3')在
打印(doc.\uE.coref_群集)
打印(已解决文档核心问题)
上述代码的输出为: [电话区号:[电话区号,It,It,It]]
电话区号只有在满足以下所有条件时才有效。电话区号不能为空。电话区号应为数字。电话区号不能小于200。最小位数应为3。在
为此,您将需要有空间,以及英国模型,可以是en_coref_md或en_coref_lg或en_coref_sm。您可以参考以下链接以获得更好的解释:
https://github.com/huggingface/neuralcoref
输出:“汤姆是个聪明的男孩。汤姆知道很多事情
相关问题 更多 >
编程相关推荐