简体   繁体   中英

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.

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 .

!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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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