简体   繁体   中英

Stanford CoreNLP - How to setup another language

I am trying to setup my NLP parser using the stanford library. On the website I downloaded

  • stanford-corenlp-full-2015-12-09.zip
  • standford-french-corenlp-2016-01-14-models.jar

Now I am facing a problem, how can I indicate my app to use the French model to analyse my sentence.

I actually have this code (working for English sentences)

String text = "I am very sad";
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    Annotation annotation = pipeline.process(text);
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
        System.out.println(sentiment + "\t" + sentence);
    }

Is there a way to indicate in the code that I want the French model (and try to parse a sentence like "Bonjour, je m'appelle Jean".

Thanks, Alexi

The solution is to add the standford french .jar file in the classpath.

Following code is working

String sampleFrenchText = "Le chat mange la souris";
Annotation frenchAnnotation = new Annotation(sampleFrenchText);
Properties frenchProperties = StringUtils.argsToProperties(new String[]{"-props", "StanfordCoreNLP-french.properties"});
StanfordCoreNLP pipeline = new StanfordCoreNLP(frenchProperties);
pipeline.annotate(frenchAnnotation);
for (CoreMap sentence : frenchAnnotation.get(CoreAnnotations.SentencesAnnotation.class)) {
    Tree sentenceTree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
    System.out.println(sentenceTree);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM