天天看點

StanfordCoreNLP配置方法

  1. pip install stanfordcorenlp

  2. 去官網下載下傳CoreNLP
    StanfordCoreNLP配置方法
  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的簡單使用