首页 > 解决方案 > 错误:未找到 Python 模块“cleannlp”。安装:pip install cleannlp in R

问题描述

我在 ubuntu 18.4 上使用 R 工作室,特别是 cleannlp 包,根据此处找到的本教程分析文本 https://statsmaths.github.io/cleanNLP/state-of-union.html

根据此处找到的安装说明 - https://statsmaths.github.io/cleanNLP/,到目前为止,我已经install.packages("cleanNLP")在 Rstudio 的控制台中安装了 cleannlp。此外,我已经pip install cleannlp在终端中安装了它的模块。据我所知,这两种安装都没有遇到任何问题。

如安装说明所示,cleannlp 有 4 个后端:

cnlp_init_stringi(locale="en_GB")
cnlp_init_udpipe(model_name="english")
cnlp_init_spacy(model_name="en")
cnlp_init_corenlp(lang="en")

使用 stringi 或 udpipe 似乎没有任何问题。但是,使用 spacy 和 corenlp 后端似乎会引发此错误

Error: Python module 'cleannlp' not found. Install with: pip install cleannlp
4. stop(msg, call. = (msg == ""))
3. assert(!is.null(disc$required_module_path), "Python module 'cleannlp' not found. Install with:\n pip install cleannlp")
2. check_python()
1. cnlp_init_spacy("/usr/local/lib/python3.6/")

我相当肯定 python 包安装成功,从以下推断:

(base) redapemusic35@mythinker:~$ pip install cleannlp
Processing ./.cache/pip/wheels/1e/23/e6/a201f9e10a6664d0639453ee4f19bba7baf8544e9e82b18d1b/cleannlp-1.0.3-py3-  none-any.whl
Requirement already satisfied: spacy in ./anaconda3/lib/python3.7/site-packages (from cleannlp) (2.2.3)
Requirement already satisfied: plac<1.2.0,>=0.9.6 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (1.1.3)
Requirement already satisfied: setuptools in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (45.2.0.post20200210)
Requirement already satisfied: srsly<1.1.0,>=0.1.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (1.0.2)
Requirement already satisfied: blis<0.5.0,>=0.4.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (0.4.1)
Requirement already satisfied: murmurhash<1.1.0,>=0.28.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (1.0.2)
Requirement already satisfied: numpy>=1.15.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (1.18.1)
Requirement already satisfied: thinc<7.4.0,>=7.3.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (7.3.1)
Requirement already satisfied: wasabi<1.1.0,>=0.4.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (0.6.0)
Requirement already satisfied: requests<3.0.0,>=2.13.0 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (2.22.0)
Requirement already satisfied: preshed<3.1.0,>=3.0.2 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (3.0.2)
Requirement already satisfied: catalogue<1.1.0,>=0.0.7 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (1.0.0)
Requirement already satisfied: cymem<2.1.0,>=2.0.2 in ./anaconda3/lib/python3.7/site-packages (from spacy->cleannlp) (2.0.3)
Requirement already satisfied: tqdm<5.0.0,>=4.10.0 in ./anaconda3/lib/python3.7/site-packages (from thinc<7.4.0,>=7.3.0->spacy->cleannlp) (4.42.1)
Requirement already satisfied: idna<2.9,>=2.5 in ./anaconda3/lib/python3.7/site-packages (from requests<3.0.0,>=2.13.0->spacy->cleannlp) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./anaconda3/lib/python3.7/site-packages (from requests<3.0.0,>=2.13.0->spacy->cleannlp) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in ./anaconda3/lib/python3.7/site-packages (from requests<3.0.0,>=2.13.0->spacy->cleannlp) (2019.11.28)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in ./anaconda3/lib/python3.7/site-packages (from requests<3.0.0,>=2.13.0->spacy->cleannlp)(3.0.4)
Requirement already satisfied: importlib-metadata>=0.20; python_version< "3.8" in ./anaconda3/lib/python3.7/site-packages (from catalogue<1.1.0,>=0.0.7->spacy->cleannlp) (1.5.0)
Requirement already satisfied: zipp>=0.5 in ./anaconda3/lib/python3.7/site-packages (from importlib-metadata>=0.20; python_version < "3.8"->catalogue<1.1.0,>=0.0.7->spacy->cleannlp) (2.2.0)
Installing collected packages: cleannlp
Successfully installed cleannlp-1.0.3

我尝试过的一些事情包括

  1. 没有路径 cnlp_init_spacy()

这给了我:

Error: Python module 'cleannlp' not found. Install with: pip install cleannlp
4. stop(msg, call. = (msg == ""))
3.
assert(!is.null(disc$required_module_path), "Python module 'cleannlp' not found. Install with:\n pip install cleannlp")
2. check_python()
1. cnlp_init_spacy()

我是否与任何一个后端(spacy 和 corenlp)都给我带来麻烦没有区别。

  1. sudo pip install sudo -H pip install sudo -H pip3 install sudo pip3 install 所有这些似乎都工作正常 。 但是,我想知道 R 选择哪个安装是否有问题。所以我卸载并再次尝试。此外,我似乎对 R 之外的 python 有任何问题。

标签: rubunturstudio

解决方案


可能是 R 使用的 python 环境没有安装“cleannlp”。您可以通过以下方式检查:

install.packages("reticulate")
library(reticulate)
py_discover_config(required_module="cleannlp")

这应该指向您的 anaconda3 路径。如果它指向另一个环境,例如 r-miniconda,您可以使用以下命令进行更改:

Sys.setenv(RETICULATE_PYTHON = path)

其中 path 是您到 anaconda 的路径,在 Mac 中通常具有以下形式:“/Users/YOUR_NAME/anaconda3/bin/python”

现在运行:

py_discover_config(required_module="cleannlp")

再次检查它是否在您的 anaconda3 python 环境中找到“cleannlp”。如果可行,那么您现在可以运行:

cnlp_download_corenlp()
cnlp_init_corenlp(lang="en")

推荐阅读