1. pip install stanfordcorenlp
  2. 官网下载CoreNLP
    CoreNLP
  3. 解压CoreNLP,得到stanford-corenlp-4.2.0
    unzip stanford-corenlp-latest.zip

简单实用示例:

>>> from stanfordcorenlp import StanfordCoreNLP
>>> nlp = StanfordCoreNLP('./stanford-corenlp-4.2.0')
>>> s = 'show from flights me san francisco to minneapolis'
>>> print(nlp.pos_tag(s))
[('show', 'NN'), ('from', 'IN'), ('flights', 'NNS'), ('me', 'PRP'), ('san', 'FW'), ('francisco', 'FW'), ('to', 'TO'),
('minneapolis', 'NNP')]
>>> print(nlp.ner(s))
[('show', 'O'), ('from', 'O'), ('flights', 'O'), ('me', 'O'), ('san', 'O'), ('francisco', 'O'), ('to', 'O'), ('minneapolis', 'O')]
>>> print(nlp.parse(s))
(ROOT
  (NP
    (NP
      (NP (NN show))
      (PP (IN from)
        (NP
          (NP (NNS flights))
          (NP (PRP me)))))
    (NP
      (NP (FW san) (FW francisco))
      (TO to)
      (NP (NNP minneapolis)))))
>>> print(nlp.dependency_parse(s))
[('ROOT', 0, 1), ('case', 3, 2), ('nmod', 1, 3), ('dep', 3, 4), ('compound', 6, 5), ('dep', 4, 6), ('case', 8, 7), ('nmod', 6, 8)]

如需中文扩展,请参考以下链接

Reference

StanfordCoreNLP的简单使用

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐