package org.studiorailgun.conversation.parser;

import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.semgraph.*;
import java.util.*;

import org.studiorailgun.conversation.parser.depend.Clause;
import org.studiorailgun.conversation.tracking.Quote;
import org.studiorailgun.conversation.tracking.Sentence;

/**
 * Parses a sentence
 */
public class NLPParser {

    /**
     * The CoreNLP pipeline
     */
    static StanfordCoreNLP pipeline;

    /**
     * Initializes the part-of-speech Parser
     */
    public static void init(){
        System.out.println("Initializing NLP Parser");

        // set up pipeline properties
        Properties props = new Properties();
        // set the list of annotators to run
        props.setProperty("annotators", "tokenize,pos,lemma,ner,parse,depparse,coref,kbp,quote");
        // set a property for an annotator, in this case the coref annotator is being set to use the neural algorithm
        props.setProperty("coref.algorithm", "neural");
        // build pipeline
        pipeline = new StanfordCoreNLP(props);

        System.out.println("Finished Initializing NLP Parser");
    }
    
    /**
     * Parses the input sentence
     * @param input The input sentence
     */
    public static void parse(Quote quote){
        if(quote.getParsedDocument() == null){
            // annnotate the document and store
            CoreDocument document = NLPParser.parse(quote.getRaw());
            quote.setParsedDocument(document);

            for(CoreSentence coreSentence : document.sentences()){
                //store the semantic graph
                SemanticGraph graph = coreSentence.dependencyParse();
                Sentence sentence = new Sentence(coreSentence.text());
                sentence.setGraph(graph);
                quote.addSentence(sentence);

                //parse the higher order relations out of the sentence
                sentence.setMainClause(Clause.parse(graph));
            }
        }
    }

    /**
     * Parses the input sentence
     * @param input The input sentence
     */
    public static CoreDocument parse(String input){
        // annnotate the document and store
        CoreDocument document = new CoreDocument(input);
        pipeline.annotate(document);

        return document;
    }

}
