面对在PYTHON中设置Stanford-corenlp的困难时间

问题描述 投票:0回答:1

我找不到使用python在我的系统(Windows)上使用stanford corenlp的完整教程。经过大量搜索后,我正在使用StanfordCoreNLP软件包并在系统上使用。我找不到任何文档可以更有效地使用它。 我想提取RELATIONS和OPENIE由于任何地方都没有文档,我只是尝试将OpenIE放在属性中

self.props = {
            'annotators': 'tokenize,ssplit,pos,lemma,ner,parse,depparse,dcoref,relation,OpenIE',
            'pipelineLanguage': 'en',
            'outputFormat': 'json'
        }

self.nlp.relations(sentence)
self.nlp.relations(sentence)

但是它不起作用! (明显)。我收到此错误

AttributeError:'StanfordCoreNLP'对象没有属性'relations'AttributeError:'StanfordCoreNLP'对象没有属性'openie'

[能否将我引导至可以获取PYTHON详细教程的资源?我听说StanfordCoreNLP包不具有Stanford-corenlp的所有功能吗?太混乱了!包太多了,很难决定要使用哪个包,哪个才是正确的包!请帮助!

from stanfordcorenlp import StanfordCoreNLP
import logging
import json

class StanfordNLP:
    def __init__(self, host='http://localhost', port=9000):
        self.nlp = StanfordCoreNLP(host, port=port,
                                   timeout=30000)  # , quiet=False, logging_level=logging.DEBUG)
        self.props = {
            'annotators': 'tokenize,ssplit,pos,lemma,ner,parse,depparse,dcoref,relation,OpenIE',
            'pipelineLanguage': 'en',
            'outputFormat': 'json'
        }

    def word_tokenize(self, sentence):
        return self.nlp.word_tokenize(sentence)

    def pos(self, sentence):
        return self.nlp.pos_tag(sentence)

    def ner(self, sentence):
        return self.nlp.ner(sentence)

    def parse(self, sentence):
        return self.nlp.parse(sentence)

    def dependency_parse(self, sentence):
        return self.nlp.dependency_parse(sentence)

    def annotate(self, sentence):
        return json.loads(self.nlp.annotate(sentence, properties=self.props))

    def cor(self, sentence):
        return (self.nlp.coref(sentence))

    # **I tried to get relations and OpenIE**
    def relations(self, sentence):
        return (self.nlp.relations(sentence))

     def openie(self, sentence):
        return (self.nlp.openie(sentence))




    @staticmethod
    def tokens_to_dict(_tokens):
        tokens = defaultdict(dict)
        for token in _tokens:
            tokens[int(token['index'])] = {
                'word': token['word'],
                'lemma': token['lemma'],
                'pos': token['pos'],
                'ner': token['ner']
            }
        return tokens

if __name__ == '__main__':
    sNLP = StanfordNLP()
    text = 'John likes apple. Mary Likes Him'


    s=sNLP.relations(text)
    print(s)
    print("OpenIE:", sNLP.openie(text))
    ```
nlp stanford-nlp
1个回答
0
投票

Stanford CoreNLP(Java代码库)提供对OpenIE和关系提取的访问。

您可以启动服务器(Java)并使用Python对其进行访问。您可以使用JSON获取结果。

这里有有关使用服务器的详细说明:

https://stanfordnlp.github.io/stanfordnlp/corenlp_client.html

© www.soinside.com 2019 - 2024. All rights reserved.