用于运行Stanford CoreNLP中文NER的代码片段

时间:2016-01-21 01:16:10

标签: stanford-nlp

我正在使用maven依赖项在Java程序中运行CoreNLP。我需要在原始中文文本上运行NER。有人可以提供一个代码片段吗?

我找到了指令:“......你首先需要运行斯坦福词语分段器或其他一些中文单词分段器,然后在输出上运行NER!”但我无法弄清楚如何做到这一点。你是否以某种方式将ChineseSegmenterAnnotator拼接到英语NER管道中?在那之前你需要ChineseDocumentToSentenceProcessor吗?所有这些都可以使用StanfordCoreNLP和正确的属性集来完成,还是需要其他什么?我有中国模特。

感谢。

1 个答案:

答案 0 :(得分:4)

您可以在中文文本上运行整个管道。关键区别在于使用段注释器而不是标记化注释器。

以下是您用于整个中国管道的属性。您可以删除任何不需要的注释器。因此,在您的情况下,您可以停止在ner并删除解析,提及和coref的属性。

# Pipeline options - lemma is no-op for Chinese but currently needed because coref demands it (bad old requirements system)
annotators = segment, ssplit, pos, lemma, ner, parse, mention, coref

# segment
customAnnotatorClass.segment = edu.stanford.nlp.pipeline.ChineseSegmenterAnnotator

segment.model = edu/stanford/nlp/models/segmenter/chinese/ctb.gz
segment.sighanCorporaDict = edu/stanford/nlp/models/segmenter/chinese
segment.serDictionary = edu/stanford/nlp/models/segmenter/chinese/dict-chris6.ser.gz
segment.sighanPostProcessing = true

# sentence split
ssplit.boundaryTokenRegex = [.]|[!?]+|[。]|[!?]+

# pos
pos.model = edu/stanford/nlp/models/pos-tagger/chinese-distsim/chinese-distsim.tagger

# ner
ner.model = edu/stanford/nlp/models/ner/chinese.misc.distsim.crf.ser.gz
ner.applyNumericClassifiers = false
ner.useSUTime = false

# parse
parse.model = edu/stanford/nlp/models/lexparser/chineseFactored.ser.gz

# coref and mention
coref.sieves = ChineseHeadMatch, ExactStringMatch, PreciseConstructs, StrictHeadMatch1, StrictHeadMatch2, StrictHeadMatch3, StrictHeadMatch4, PronounMatch
coref.input.type = raw
coref.postprocessing = true
coref.calculateFeatureImportance = false
coref.useConstituencyTree = true
coref.useSemantics = false
coref.md.type = RULE
coref.mode = hybrid
coref.path.word2vec =
coref.language = zh
coref.print.md.log = false
coref.defaultPronounAgreement = true
coref.zh.dict = edu/stanford/nlp/models/dcoref/zh-attributes.txt.gz

当我有机会时,我会尝试为您编写一个完整的演示类,包括正确的导入。但这是一段代码,将在中文文本上运行管道。确保在类路径中有中文模型jar。你可以去here了解如何在Maven中添加中文模型jar。

Properties props = new Properties();
props = StringUtils.propFileToProperties("StanfordCoreNLP-chinese.properties");
// that properties file will run the entire pipeline
// if you uncomment the following line it will just go up to ner
//props.setProperty("annotators","segment,ssplit,pos,lemma,ner");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation annotation = new Annotation("whatever your Chinese text is");
pipeline.annotate(annotation);