在 R 中安装 coreNLP

Installing coreNLP in R

我正在按照 link 上的说明使用 coreNLP https://github.com/statsmaths/coreNLP

但是,我发现了这个错误

> library(coreNLP)

Error in get(method, envir = home) : 
lazy-load database '/Users/apple/Library/R/3.2/library/coreNLP/R/coreNLP.rdb is  corrupt
In addition: Warning messages:
 1: In .registerS3method(fin[i, 1], fin[i, 2], fin[i, 3], fin[i, 4],  :
 restarting interrupted promise evaluation
 2: In get(method, envir = home) :
 restarting interrupted promise evaluation
 3: In get(method, envir = home) : internal error -3 in R_decompress1
 Error: package or namespace load failed for ‘coreNLP’
> install.packages('devtools')
> devtools::install_github("statsmaths/coreNLP")
> download.file("http://nlp.stanford.edu/software/stanford-corenlp-full-2015-01-29.zip", '/path/to/save/stanford-corenlp-full-2015-01-29.zip')
> unzip('/path/to/save/stanford-corenlp-full-2015-01-29.zip')

上述 https://github.com/statsmaths/coreNLP 的说明有效,可能是在 R

中安装库

Re-run 这些命令重新安装 corenlp 包装器:

> install.packages('devtools')
> devtools::install_github("statsmaths/coreNLP")

如果包没有损坏,您应该会看到:

> devtools::install_github("statsmaths/coreNLP")
Downloading GitHub repo statsmaths/coreNLP@master
Installing coreNLP
'/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore CMD  \
  INSTALL '/tmp/RtmpFS9LWl/devtools667a3cdbc084/statsmaths-coreNLP-3a667c6'  \
  --library='/home/expert/R/x86_64-pc-linux-gnu-library/3.2' --install-tests 

* installing *source* package ‘coreNLP’ ...
** R
** data
*** moving datasets to lazyload DB
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (coreNLP)
Reloading installed coreNLP

否则 devtools 应该重新安装软件包。

遇到java.lang.UnsupportedClassVersionError: edu/stanford/nlp/pipeline/StanfordCoreNLP : Unsupported major.minor version 52.0错误信息后:

你需要

  • 安装 java 8,(作为超级用户),
  • 将操作系统使用的默认 jvm 更改为此 jvm(* 见下文),
  • 运行 R CMD javareconf 在命令行,然后
  • 设置环境变量LD_LIBRARY_PATH为存放libjvm.so的目录。
  • 重启 R/RStudio

  • 确保您的计算机上存在交换文件(或交换分区)。调用 free 检查输出中是否有以 swap 开头的行并且该行上的值不为零。

我用ubuntu,我的java8libjvm.so在这里:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so

您可以在 .Rprofile 文件中执行此操作。添加这一行,也许在文件的底部:

Sys.setenv(LD_LIBRARY_PATH=paste0(Sys.getenv("LD_LIBRARY_PATH"), ":", "/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/" ))

当我在 R 中执行此操作时:

R> Sys.getenv("LD_LIBRARY_PATH")
[1] "/usr/local/lib64/R/lib:/usr/local/lib64:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/"
R> library(coreNLP)
R> initCoreNLP()

我得到这个结果:

Searching for resource: config.properties
Adding annotator tokenize
TokenizerAnnotator: No tokenizer type provided. Defaulting to PTBTokenizer.
Adding annotator ssplit
Adding annotator pos
Reading POS tagger model from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [1.1 sec].
Adding annotator lemma
Adding annotator ner
Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... done [5.6 sec].
Loading classifier from edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz ... done [2.1 sec].
Loading classifier from edu/stanford/nlp/models/ner/english.conll.4class.distsim.crf.ser.gz ... done [3.8 sec].
Initializing JollyDayHoliday for SUTime from classpath: edu/stanford/nlp/models/sutime/jollyday/Holidays_sutime.xml as sutime.binder.1.
Reading TokensRegex rules from edu/stanford/nlp/models/sutime/defs.sutime.txt
Reading TokensRegex rules from edu/stanford/nlp/models/sutime/english.sutime.txt
Reading TokensRegex rules from edu/stanford/nlp/models/sutime/english.holidays.sutime.txt
Adding annotator parse
Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [0.6 sec].
Adding annotator dcoref
Adding annotator sentiment

R> example(getSentiment)

gtSntmR> getSentiment(annoEtranger) # first Sentence of L'Etranger by A.Camus
  id sentimentValue sentiment
1  1              1  Negative
2  2              2   Neutral

gtSntmR> getSentiment(annoHp) # first Sentence of Harry Potter V1
  id sentimentValue    sentiment
1  1              4 Verypositive

(*) 如何查看 Linux 上的默认 jvm:

update-alternatives --display java

结果

java - auto mode
  link currently points to /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

要显示所有可用的备选方案,请使用

update-alternatives --list java

结果(在我的机器上):

/usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

更改备选方案:

sudo update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

稍微玩一下 update-alternatives。