Installing coreNLP in R

后端 未结 2 842
名媛妹妹
名媛妹妹 2020-12-21 10:58

I\'m following the instructions on this link to use coreNLP https://github.com/statsmaths/coreNLP

However, I found this error

> library(coreNLP)

         


        
2条回答
  •  没有蜡笔的小新
    2020-12-21 11:33

    After encountering the java.lang.UnsupportedClassVersionError: edu/stanford/nlp/pipeline/StanfordCoreNLP : Unsupported major.minor version 52.0 error message:

    You need to

    • install java 8, (as superuser),
    • change the default jvm the operating system uses to this jvm (* see below),
    • run R CMD javareconf on the command line, and then
    • set the environment variable LD_LIBRARY_PATH to the directory where libjvm.so is stored.
    • restart R / RStudio

    • make sure that a swap file (or swap partition) exists on your machine. call free to check if there is a line in the output that starts with swap and the values on that line are not zero.

    I use ubuntu, my java 8 libjvm.so is here: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so

    You can do this in your .Rprofile file. Add this line, perhaps at the bottom of the file:

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

    When I do this in 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()
    

    I get this result:

    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
    

    (*) How to see the default jvm on Linux:

    update-alternatives --display java
    

    Result

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

    To show all available alternatives, use

    update-alternatives --list java
    

    Result (on my machine):

    /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
    

    Change alternatives:

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

    Just play a bit with update-alternatives.

提交回复
热议问题