在R中安装coreNLP

时间:2016-01-11 07:30:00

标签: r nlp stanford-nlp devtools r-package

我按照此链接上的说明使用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’

2 个答案:

答案 0 :(得分:3)

遇到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,我的java 8 libjvm.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即可。

答案 1 :(得分:2)

> 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

中安装库

重新运行这些命令以重新安装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应该重新安装包。