斯坦福 NLP 中使用 Python 的实体识别

数据挖掘 Python nlp 斯坦福-nlp
2022-03-11 06:14:17

我正在使用 Python 使用 Stanford Core NLP。我从这里获取了代码。

这是代码:

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=True, logging_level=logging.DEBUG)
    self.props = {
        'annotators': 'tokenize,ssplit,pos,lemma,ner,parse,depparse,dcoref,relation,sentiment',
        '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))

@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 = r'China on Wednesday issued a $50-billion list of U.S. goods  including soybeans and small aircraft for possible tariff hikes in an escalating technology dispute with Washington that companies worry could set back the global economic recovery.The country\'s tax agency gave no date for the 25 percent increase...'
ANNOTATE =  sNLP.annotate(text)
POS = sNLP.pos(text)
TOKENS = sNLP.word_tokenize(text)
NER = sNLP.ner(text)
PARSE = sNLP.parse(text)
DEP_PARSE = sNLP.dependency_parse(text)    

我只对保存在变量 NER 中的实体识别感兴趣。命令 NER 给出以下结果:

NER

如果我在Stanford Website上运行同样的事情,NER 的输出是: NER斯坦福

我的 Python 代码有 2 个问题:

1. “$”和“500亿”应该合并命名为一个实体。同样,我希望 '25' 和 'percent' 作为一个单一的实体,因为它显示在在线 stanford 输出中。
2. 在我的输出中,'Washington' 显示为 State,'China' 显示为 Country。我希望它们在斯坦福网站输出中都显示为“Loc”。此问题的可能解决方案在于文档文件

但我不知道我使用的是哪种型号以及如何更改型号。

2个回答

从以下链接中,我了解到我们可以通过doing来使用特定的分类器。

  1. 加载特定的分类器:

    java -mx600m -cp "*;lib\*" edu.stanford.nlp.ie.crf.CRFClassifier -loadClassifier classifiers/english.all.3class.distsim.crf.ser.gz
    
  2. ner模型设置为代码中的分类器:

    { "ner.model", "english.all.3class.distsim.crf.ser.gz" }
    

斯坦福大学堆栈溢出

斯坦福大学 NLP 研究小组发布的 Stanford CoreNLP。它提供了基于 Java 的模块,用于解决一系列基本的 NLP 任务,例如

  • 词性标注(词性标注)
  • NER(名称实体识别)
  • 依赖解析、情感分析等

在执行上述所有任务之前,您应该首先设置 stanford CoreNlp。

如果您想清楚地了解 stanford coreNlp 从setup core nlp for python、NER、POS 到Sentiment ,您可以查看以下链接。

斯坦福 CoreNLP 的高级自然语言处理