Alexi Coard
Alexi Coard

Reputation: 7742

Stanford CoreNLP - How to setup another language

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

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

Upvotes: 1

Views: 1274

Answers (1)

Alexi Coard
Alexi Coard

Reputation: 7742

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);
}

Upvotes: 2

Related Questions