python - 如何在 Google Colab 上运行 Stanford CoreNLP 进行词形还原?
问题描述
有一个类似的问题,但是从那时起 google colab 发生了很大变化,我想知道如何在 Google Colab 上使用 Stanford CoreNLP,特别是用于词形还原。
预期答案:
- 导入模块
- 使用示例代码进行词形还原
使用代码:
!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)
任何改进问题的建议都将被考虑
解决方案
也许最好使用新的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')
你会得到这个输出
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: .
这是一个示例笔记本。
推荐阅读
- flutter - dart Grpc 的拦截器
- google-cloud-platform - 由于 VPC 连接器注释,部署 Cloud Run 服务失败
- php - Symfony 5 缓存组件 AWS elasticache 无效 DSN
- php - Yii2 - 下载损坏的文件
- delphi - Indy TIdHashMessageDigest5 HashStringAsHex 编译时错误:未声明的标识符:'HashStringAsHex'
- java - 为什么握手后我的套接字关闭了,我该如何解决这个问题?
- c# - 如何对将在多个步骤中创建的聚合进行建模,例如向导样式
- azure - 将 Azure 数字孪生与现有云环境连接
- verilog - 使用带负数的系统函数 $urandom_range(minval, maxval) 时的未定义行为
- r - 使用 R 处理多个光栅文件时保留原始文件名