首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用斯坦福coreNLP实现python中的协参解析

用斯坦福coreNLP实现python中的协参解析
EN

Stack Overflow用户
提问于 2016-09-09 11:12:47
回答 4查看 17.3K关注 0票数 12

斯坦福大学( Stanford )提供了共同参考解决方案( coreference CoreNLP 如前所述 ),这条线也提供了一些关于它在Java中实现的见解。

但是,我使用的是python和NLTK,我不知道如何在我的python代码中使用CoreNLP的协引用解析功能。我已经能够在NLTK中设置StanfordParser,这是我的代码。

代码语言:javascript
复制
from nltk.parse.stanford import StanfordDependencyParser
stanford_parser_dir = 'stanford-parser/'
eng_model_path = stanford_parser_dir  + "stanford-parser-models/edu/stanford/nlp/models/lexparser/englishRNN.ser.gz"
my_path_to_models_jar = stanford_parser_dir  + "stanford-parser-3.5.2-models.jar"
my_path_to_jar = stanford_parser_dir  + "stanford-parser.jar"

如何在python中使用CoreNLP的共同引用分辨率?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-16 13:09:39

正如@Igor所提到的,您可以尝试在这个GitHub repo:https://github.com/dasmith/stanford-corenlp-python中实现的python包装器。

此回购文件包含两个主要文件: corenlp.py client.py

执行以下更改以使coreNLP工作:

  1. 在corenlp.py中,更改corenlp文件夹的路径。设置本地机器包含corenlp文件夹的路径,并在corenlp.py的第144行中添加路径 if not corenlp_path: corenlp_path = <path to the corenlp file>
  2. "corenlp.py“中的jar文件版本号是不同的。根据您所拥有的corenlp版本设置它。在corenlp.py的第135行更改它 jars = ["stanford-corenlp-3.4.1.jar", "stanford-corenlp-3.4.1-models.jar", "joda-time.jar", "xom.jar", "jollyday.jar"]

在此中,将3.4.1替换为您下载的jar版本。

  1. 运行以下命令: python corenlp.py

这将启动一个服务器。

  1. 现在运行主客户端程序。 python client.py

这提供了一个字典,您可以使用' coref‘作为键访问coref:

例如:约翰是计算机科学家。他喜欢编码。

代码语言:javascript
复制
{
     "coref": [[[["a Computer Scientist", 0, 4, 2, 5], ["John", 0, 0, 0, 1]], [["He", 1, 0, 0, 1], ["John", 0, 0, 0, 1]]]]
}

我在Ubuntu 16.04上试过这个。使用7或8。

票数 10
EN

Stack Overflow用户

发布于 2018-01-15 11:12:28

史坦福是一种相对较新的包装器,可能对您有用。

假设文本是“巴拉克奥巴马出生在夏威夷,他是总统,奥巴马于2008年当选。”

守则:

代码语言:javascript
复制
# coding=utf-8

import json
from stanfordcorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP(r'G:\JavaLibraries\stanford-corenlp-full-2017-06-09', quiet=False)
props = {'annotators': 'coref', 'pipelineLanguage': 'en'}

text = 'Barack Obama was born in Hawaii.  He is the president. Obama was elected in 2008.'
result = json.loads(nlp.annotate(text, properties=props))

num, mentions = result['corefs'].items()[0]
for mention in mentions:
    print(mention)

上面的每一个“提到”都是Python的一个分支,如下所示:

代码语言:javascript
复制
{
  "id": 0,
  "text": "Barack Obama",
  "type": "PROPER",
  "number": "SINGULAR",
  "gender": "MALE",
  "animacy": "ANIMATE",
  "startIndex": 1,
  "endIndex": 3,
  "headIndex": 2,
  "sentNum": 1,
  "position": [
    1,
    1
  ],
  "isRepresentativeMention": true
}
票数 7
EN

Stack Overflow用户

发布于 2019-07-18 19:05:08

斯坦福大学的CoreNLP现在有一个名为StanfordNLP的正式Python绑定,你可以在StanfordNLP网站里读到。

本机似乎支持coref处理器,但是您可以使用CoreNLPClient接口从CoreNLP调用“标准”CoreNLP(原始CoreNLP软件)。

因此,在按照设置Python包装器这里的说明之后,您可以获得如下所示的协引用链:

代码语言:javascript
复制
from stanfordnlp.server import CoreNLPClient

text = 'Barack was born in Hawaii. His wife Michelle was born in Milan. He says that she is very smart.'
print(f"Input text: {text}")

# set up the client
client = CoreNLPClient(properties={'annotators': 'coref', 'coref.algorithm' : 'statistical'}, timeout=60000, memory='16G')

# submit the request to the server
ann = client.annotate(text)    

mychains = list()
chains = ann.corefChain
for chain in chains:
    mychain = list()
    # Loop through every mention of this chain
    for mention in chain.mention:
        # Get the sentence in which this mention is located, and get the words which are part of this mention
        # (we can have more than one word, for example, a mention can be a pronoun like "he", but also a compound noun like "His wife Michelle")
        words_list = ann.sentence[mention.sentenceIndex].token[mention.beginIndex:mention.endIndex]
        #build a string out of the words of this mention
        ment_word = ' '.join([x.word for x in words_list])
        mychain.append(ment_word)
    mychains.append(mychain)

for chain in mychains:
    print(' <-> '.join(chain))
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39410282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档