简体   繁体   English

如何在 Google Colab 上运行斯坦福 CoreNLP 进行词形还原?

[英]How to run Stanford CoreNLP for lemmatization on Google Colab?

There is a similar question made, however google colab change a lot since that time, I was wondering how to use Stanford CoreNLP on Google Colab, specially for lemmatization.有一个类似的问题,但是从那时起 google colab 发生了很大变化,我想知道如何在 Google Colab 上使用斯坦福 CoreNLP,专门用于词形还原。

Expected answer:预期答案:

  • import the module导入模块
  • lemmatize with a sample code使用示例代码进行词形还原

Using the code:使用代码:

!pip install stanfordnlp
import stanfordnlp
stanfordnlp.download("es")
nlp = stanfordnlp.Pipeline(processors='tokenize,mwt,pos,lemma')
doc = nlp("Barack Obama was born in Hawaii.")
print(*[f'word: {word.text+" "}\tlemma: {word.lemma}' for sent in doc.sentences for word in sent.words], sep='\n')

%tb

------------
Loading: tokenize
With settings: 
{'model_path': '/root/stanfordnlp_resources/en_ewt_models/en_ewt_tokenizer.pt', 'lang': 'en', 'shorthand': 'en_ewt', 'mode': 'predict'}
Cannot load model from /root/stanfordnlp_resources/en_ewt_models/en_ewt_tokenizer.pt
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py:2890: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

any advice to improve the question will be considered将考虑任何改进问题的建议

Maybe it's better to use the new StanfordNLP instead of their old CoreNLP .也许最好使用新的StanfordNLP而不是旧的CoreNLP

!pip install stanfordnlp
import stanfordnlp
stanfordnlp.download("en")
nlp = stanfordnlp.Pipeline(processors='tokenize,mwt,pos,lemma')
doc = nlp("Barack Obama was born in Hawaii.")
print(*[f'word: {word.text+" "}\tlemma: {word.lemma}' for sent in doc.sentences for word in sent.words], sep='\n')

You will get this output你会得到这个输出

word: Barack    lemma: Barack
word: Obama     lemma: Obama
word: was   lemma: be
word: born  lemma: bear
word: in    lemma: in
word: Hawaii    lemma: Hawaii
word: .     lemma: .

Here's an example notebook .这是一个示例笔记本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM