me.aatma.languagetologic.graph.nodes.KBNLAntiDStateNodeCloud.java Source code

Java tutorial

Introduction

Here is the source code for me.aatma.languagetologic.graph.nodes.KBNLAntiDStateNodeCloud.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package me.aatma.languagetologic.graph.nodes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.aatma.languagetologic.nl.NounPhrase;
import me.aatma.library.stanfordcorenlputils.NLPConstants;
import me.aatma.library.stanfordcorenlputils.NLPTools;
import me.aatma.languagetologic.nl.AnalyzePhraseImpl;
import me.aatma.languagetologic.BuildKBNLGraph;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.ling.Label;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.trees.Tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import me.aatma.library.sapi.SPredicate;

/**
 *
 * @author vijay
 */
public class KBNLAntiDStateNodeCloud extends KBNLStateNodeCloud {

    private static final Logger log = LoggerFactory.getLogger(KBNLAntiDStateNodeCloud.class.getCanonicalName());

    private static List<Object> parseNP(Tree nodeTree, Class retType) throws Exception {
        NounPhrase np = new NounPhrase(nodeTree, retType);
        //    return AnalyzePhraseImpl.parseNP(nodeTree, null);
        return np.parse();
    }

    public KBNLAntiDStateNodeCloud(SemanticGraph dependencies, Map<Label, Tree> phraseMap, Tree tree,
            IndexedWord root) throws Exception {
        super(dependencies, tree, root,
                parseNP(BuildKBNLGraph.findTreeWithHeadRetTree(phraseMap, root), SPredicate.class),
                BuildKBNLGraph.findTreeWithHeadRetTree(phraseMap, root));

        //////////// NOT REQUIRED: START
        //    List<String> prepStr = new ArrayList<String>();
        //    prepStr.add("prep_of");
        //    prepStr.add("prep_from");
        //    prepStr.add("prep_to");
        //    prepStr.add("prep_on");
        //    prepStr.add("prep_in");
        //    prepStr.add("prep_along");
        //    prepStr.add("prep_than");
        //    List<Tree> preps = KBNLAbstractStateOrEventNodeCloud.findDependencyEdge(dependencies, tree, root, prepStr);
        //    if (preps.isEmpty()) {
        //      log.error("No prepositional children found for:  " + root + ". This is better handled by " + KBNLStateNodeCloud.class.getSimpleName());
        //      throw new EventEntryException("No prepositional children found for:  " + root + ". This is better handled by " + KBNLStateNodeCloud.class.getSimpleName());
        //    }
        //////////// NOT REQUIRED: END
    }

    public static KBNLAntiDStateNodeCloud getInstance(SemanticGraph dependencies, Map<Label, Tree> phraseMap,
            Tree tree, IndexedWord localRoot) {
        try {
            return new KBNLAntiDStateNodeCloud(dependencies, phraseMap, tree, localRoot);
        } catch (Exception e) {
            return null;
        }
    }

    /*
    This will have handle "The father of the bride said something"
    The father will be made a 
    */
    public static boolean isKBNLAntiDStateNode(SemanticGraph dependencies, IndexedWord localRoot) {
        boolean isAntiD = false;
        List<GrammaticalRelation> preps = new ArrayList<GrammaticalRelation>();
        // This will force AntiD to have a subject
        preps.add(NLPTools.getGR("nsubj", null));
        preps.add(NLPTools.getGR("csubj", null));
        // and some preposition.
        preps.add(NLPTools.getGR("prep", "of"));
        preps.add(NLPTools.getGR("prep", "from"));
        preps.add(NLPTools.getGR("prep", "to"));
        preps.add(NLPTools.getGR("prep", "on"));
        preps.add(NLPTools.getGR("prep", "in"));
        preps.add(NLPTools.getGR("prep", "along"));
        preps.add(NLPTools.getGR("prep", "than"));
        preps.add(NLPConstants.prep_with);

        Set<IndexedWord> children = dependencies.getChildrenWithRelns(localRoot, preps);
        isAntiD = (children.size() >= 2);
        log.info("This node is " + (isAntiD ? "potentially a" : "not a") + " AntiDStateNode.");
        return isAntiD;
    }

    @Override
    public List<SPredicate> getSemanticPossibilities() {
        List<SPredicate> copy = new ArrayList<SPredicate>();
        for (Object o : super.getSemanticPossibilities()) {
            if (o instanceof SPredicate) {
                copy.add((SPredicate) o);
            } else {
                log.error("Object " + o + " is not an Predicate");
            }
        }
        return copy;
    }
}