Lemmatisation

Fermé
informaticienne - Modifié par mamiemando le 24/04/2016 à 10:41
Bonjour,

Voila le code source de stanford pour lemmatiser un fichier texte ; mais je veux utiliser ce code pour lemmatiser un fichier texte et récupérer le résultat dans un autre fichier texte.

Mais je n'arrive pas à modifier ce code : alors s'il vous plait aidez -oi pour lemmatiser un fichier texte que-dois-je-faire comme modification !

package elimination;

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Elimination {

    protected StanfordCoreNLP pipeline;
    private Annotation document;
    private Collection<CoreMap> sentences;

    public Elimination() {
        // Create StanfordCoreNLP object properties, with POS tagging
        // (required for lemmatization), and lemmatization
        Properties props;
        props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma");

        /*
         * This is a pipeline that takes in a string and returns various analyzed linguistic forms. The String is tokenized via a tokenizer (such as PTBTokenizerAnnotator), and then other sequence model style annotation can be used to add things like lemmas, POS tags, and named entities. These are returned as a list of CoreLabels. Other analysis components build and store parse trees, dependency graphs, etc.
         * This class is designed to apply multiple Annotators to an Annotation. The idea is that you first build up the pipeline by adding Annotators, and then you take the objects you wish to annotate and pass them in and get in return a fully annotated object.
         * StanfordCoreNLP loads a lot of models, so you probably only want to do this once per execution
         */
        this.pipeline = new StanfordCoreNLP(props);
    }

    public List<String> lemmatize(String documentText) {
        List<String> lemmas = new LinkedList<String>();// Create an empty Annotation just with the given textAnnotation
        document = new Annotation(documentText);// run all Annotators on this text
        this.pipeline.annotate(document);// Iterate over all of the sentences foundList<CoreMap>
        sentences = document.get(SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {// Iterate over all tokens in a sentence
            for (CoreLabel token : sentence.get(TokensAnnotation.class)) {// Retrieve and add the lemma for each word into the list of lemmas
                lemmas.add(token.get(LemmaAnnotation.class));
            }
        }
        return lemmas;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        System.out.println("Starting Stanford Lemmatizer");
        String text = "How could you be seeing into my eyes like open doors? \n" 
        + "You led me down into my core where I've became so numb \n" 
        + "Without a soul my spirit's sleeping somewhere cold \n"
        + "Until you find it there and led it back home \n"
        + "You woke me up inside \n"
        + "Called my name and saved me from the dark \n"
        + "You have bidden my blood and it ran \n"
        + "Before I would become undone \n"
        + "You saved me from the nothing I've almost become \n"
        + "You were bringing me to life \n"
        + "Now that I knew what I'm without \n"
        + "You can've just left me \n"
        + "You breathed into me and made me real \n"
        + "Frozen inside without your touch \n"
        + "Without your love, darling \n"
        + "Only you are the life among the dead \n"
        + "I've been living a lie, there's nothing inside \n"
        + "You were bringing me to life.";
        StanfordLemmatizer slem = new StanfordLemmatizer();
        System.out.println(slem.lemmatize(text));
        List<String> w = slem.lemmatize(text);
    }
}