【问题标题】:Entity Recognition in Stanford NLP using Python斯坦福 NLP 中使用 Python 的实体识别
【发布时间】:2026-02-20 03:35:02
【问题描述】:

我正在使用 Python 使用 Stanford Core NLP。我已从 here 获取代码。
以下是代码:

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 给出以下结果

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

我的 Python 代码有 2 个问题:

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

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

【问题讨论】:

    标签: python python-3.x stanford-nlp


    【解决方案1】:

    这里有一个方法可以解决这个问题

    确保下载 Stanford CoreNLP 3.9.1 和必要的模型 jars

    在此文件“ner-server.properties”中设置服务器属性

    annotators = tokenize,ssplit,pos,lemma,ner
    ner.applyFineGrained = false
    

    使用以下命令启动服务器:

    java -Xmx12g edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000 -serverProperties ner-server.properties
    

    确保您已安装此 Python 包:

    https://github.com/stanfordnlp/python-stanford-corenlp

    运行此 Python 代码:

    import corenlp
    client = corenlp.CoreNLPClient(start_server=False, annotators=["tokenize", "ssplit", "pos", "lemma", "ner"])
    sample_text = "Joe Smith was born in Hawaii."
    ann = client.annotate(sample_text)
    for mention in ann.sentence[0].mentions:
        print([x.word for x in ann.sentence[0].token[mention.tokenStartInSentenceInclusive:mention.tokenEndInSentenceExclusive]])
    

    以下是 EntityMention 中每个实体的所有可用字段:

    sentenceIndex: 0
    tokenStartInSentenceInclusive: 5
    tokenEndInSentenceExclusive: 7
    ner: "MONEY"
    normalizedNER: "$5.0E10"
    entityType: "MONEY"
    

    【讨论】:

    • 我得到这个错误'NameError: name 'returned_annotation' is not defined'。文件“ner-server.properties”在哪里?
    • 对不起,我应该是 ann not returned_annotation
    • 那是你用我指定的行写的文件。你应该把它放在你启动服务器的工作目录中。