Example usage for edu.stanford.nlp.trees GrammaticalStructure typedDependenciesCCprocessed

List of usage examples for edu.stanford.nlp.trees GrammaticalStructure typedDependenciesCCprocessed

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees GrammaticalStructure typedDependenciesCCprocessed.

Prototype

public List<TypedDependency> typedDependenciesCCprocessed() 

Source Link

Document

Get a list of the typed dependencies, including extras like control dependencies, collapsing them and distributing relations across coordination.

Usage

From source file:cc.vidr.parseviz.ParseViz.java

License:Open Source License

public static List<TypedDependency> typedDependencies(Tree tree) {
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    return gs.typedDependenciesCCprocessed();
}

From source file:com.parse.Dependency.java

public static void main(String[] args) {
    LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
    lp.setOptionFlags(new String[] { "-maxLength", "80", "-retainTmpSubcategories", });

    String[] sent = { "This", "is", "an", "easy", "sentence", "." };
    List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
    Tree parse = lp.apply(rawWords);/*from w  w w  .  j a  va2  s .  c o  m*/
    parse.pennPrint();
    System.out.println();

    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
    System.out.println(tdl);
    //System.out.println();

    //TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    // tp.printTree(parse);

    String sentence = "which movies were directed by Christopher Nolan";
    Tree t2 = lp.parse(sentence);
    System.out.println(t2.firstChild().toString());
    gs = gsf.newGrammaticalStructure(t2);
    tdl = gs.typedDependenciesCCprocessed();
    System.out.println(tdl);
    System.out.println(tdl.get(0).dep().nodeString());

}

From source file:com.search.MySearchHandler.java

License:Apache License

private static ParsedQuestion parseQues(String str) {
    // String str =
    // "what is the caption of European Council of Religious Leaders ? ";
    TermTokenStream queryStream = new TermTokenStream((String) null);
    String[] strlist = corelabeltostr(tokenizerFactory.getTokenizer(new StringReader(str)).tokenize());
    queryStream.append(strlist);/*from  www.  j  av  a 2  s .c om*/
    Iterator<List> it = classifier.classify(queryStream.toSingleString()).iterator();
    boolean NameFound = false;
    String buffer = "";
    String finalstr = "";
    while (it.hasNext()) {
        Iterator<CoreLabel> itr = it.next().iterator();
        while (itr.hasNext()) {
            if (!itr.next().get(CoreAnnotations.AnswerAnnotation.class).equals("O")) {
                NameFound = true;
            }
        }
    }

    finalstr = compressNames(queryStream);

    System.out.println(finalstr);

    Tree parse = null;
    parse = lp.apply(tokenizerFactory.getTokenizer(new StringReader(queryStream.toNLPString())).tokenize());

    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);

    // detect Answer Type START
    String whclause = null;
    for (int i = 0; i < queryStream.length(); i++) {
        if (gs.getNodeByIndex(i + 1).parent().value().equals("WRB")
                || (gs.getNodeByIndex(i + 1).parent().value().equals("WP"))
                || (gs.getNodeByIndex(i + 1).parent().value().equals("WDT"))) {
            whclause = queryStream.index(i);
        }
    }
    // detect Answer Type END
    if (whclause != null) {

        List<TypedDependency> tdl = getTyped(queryStream.toNLPString());
        System.out.println(tdl);

        // Compress based on Noun Compounds and Adjectives , Determiners
        String compressedstr = compress(queryStream);
        System.out.println(compressedstr);

        // Compress based on Caps in letters.
        String comstr = compressCaps(queryStream);
        System.out.println(comstr);

        parse = lp.apply(tokenizerFactory.getTokenizer(new StringReader(comstr)).tokenize());

        GrammaticalStructure gs2 = gsf.newGrammaticalStructure(parse);
        List<TypedDependency> tdl2 = gs2.typedDependenciesCCprocessed();

        Iterator<TypedDependency> itlist = GrammaticalStructure.getRoots(tdl2).iterator();
        // System.out.println(queryStream.toNLPString());

        // Identify ROOT in the Grammatical Structure
        String multi[][] = new String[queryStream.length()][queryStream.length()];
        int root = 0;
        while (itlist.hasNext()) {

            int tr = itlist.next().dep().index() - 1;
            root = tr;
            multi[tr][tr] = "root";

        }

        // Fill the Array based on Relations
        ListIterator<TypedDependency> tdllist = tdl2.listIterator();
        while (tdllist.hasNext()) {

            TypedDependency tr = tdllist.next();

            int govin = tr.gov().index() - 1;
            int depin = tr.dep().index() - 1;

            if (govin >= 0) {
                // Have to fix the multiple words in the sequence issue

                multi[govin][depin] = tr.reln().toString();
            }
        }

        // PRINT ARRAY
        // for (int i = 0; i < queryStream.length(); i++) {
        // for (int j = 0; j < queryStream.length(); j++) {
        // System.out.print((multi[i][j] == null ? "-" : multi[i][j])
        // + " ");
        // }
        // System.out.println();
        // }

        // parse.pennPrint();
        parse = lp.apply(tokenizerFactory.getTokenizer(new StringReader(str)).tokenize());
        List<Tree> nounPhrases = new LinkedList<Tree>();
        getNounPhrases(parse, nounPhrases);
        ListIterator<Tree> treit = nounPhrases.listIterator();
        String nounstr = "";
        while (treit.hasNext()) {
            nounstr += treit.next().value() + " ";
        }

        List<Tree> verbPhrases = new LinkedList<Tree>();
        getVerbPhrases(parse, verbPhrases);
        ListIterator<Tree> treverbit = verbPhrases.listIterator();
        String verbstr = "";
        while (treverbit.hasNext()) {
            verbstr += treverbit.next().value() + " ";
        }

        System.out.println(nounstr);

        HashMap<String, String> depHashMap = new HashMap<String, String>();
        HashMap<String, String> govHashMap = new HashMap<String, String>();

        for (int i = 0; i < queryStream.length(); i++) {
            for (int j = 0; j < queryStream.length(); j++) {
                if (multi[i][j] != null && i != j) {
                    if (!depHashMap.containsKey(multi[i][j]) && !govHashMap.containsKey(multi[i][j])) {
                        depHashMap.put(multi[i][j], queryStream.index(j));
                        govHashMap.put(multi[i][j], queryStream.index(i));
                    } else if (!depHashMap.containsKey(multi[i][j] + "1")
                            && !govHashMap.containsKey(multi[i][j] + "1")) {
                        depHashMap.put(multi[i][j] + "1", queryStream.index(j));
                        govHashMap.put(multi[i][j] + "1", queryStream.index(i));
                    } else {
                        depHashMap.put(multi[i][j] + "2", queryStream.index(j));
                        govHashMap.put(multi[i][j] + "2", queryStream.index(i));
                    }
                }
            }
        }

        // System.out.println(gs2.getNodeByIndex(root + 1).value());

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < queryStream.length(); i++) {
            sb.append(gs2.getNodeByIndex(i + 1).parent().value());
        }

        // System.out.println(queryStream.length());
        String template = sb.toString();
        System.out.println(template);
        System.out.println(getQuestionType(template, queryStream));

        // Initialize Required fields for Query
        ParsedQuestion parques = new ParsedQuestion();
        parques.setWhclause(whclause);

        String relation = "";
        TreeMap<String, Double> whtype = new TreeMap<String, Double>();
        String searchname = "";
        String relationkeyword = "";
        switch (getQuestionType(template, queryStream)) {

        case "fourtype1":
            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }
                // get Relation from ROOT of the sentence
                relationkeyword = gs2.getNodeByIndex(root + 1).value();
                relation = getRelation(gs2.getNodeByIndex(root + 1).value(), whtype.firstKey().toLowerCase());

                // get Name from the NN in the sentence
                for (int i = 0; i < queryStream.length(); i++) {
                    if (isNoun(gs2.getNodeByIndex(i + 1).parent().value())) {
                        searchname = queryStream.index(i);
                    }
                }
            }

            break;
        case "fivetype1":
            // get WH clause from the sentence
            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }
                // get Relation from ROOT of the sentence
                relationkeyword = gs2.getNodeByIndex(root + 1).value();
                relation = getRelation(gs2.getNodeByIndex(root + 1).value(), whtype.firstKey().toLowerCase());

                // get Name from the NN in the sentence
                for (int i = 0; i < queryStream.length(); i++) {
                    if (isNoun(gs2.getNodeByIndex(i + 1).parent().value())) {
                        searchname = queryStream.index(i);
                    }
                }
            }
            break;

        case "sixtype1":
            // get WH clause from the sentence

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }
                // get the nsubj / nsubjpass relation in the sentence
                if (depHashMap.containsKey("nsubj")) {
                    relationkeyword = depHashMap.get("nsubj");
                    relation = getRelation(depHashMap.get("nsubj"), whtype.firstKey().toLowerCase());
                } else if (depHashMap.containsKey("nsubjpass")) {
                    relationkeyword = depHashMap.get("nsubjpass");
                    relation = getRelation(depHashMap.get("nsubjpass"), whtype.firstKey().toLowerCase());
                }

                // get the possessive dependent relation in the sentence
                if (depHashMap.containsKey("poss")) {
                    searchname = depHashMap.get("poss");
                }
            }
            break;

        case "sixtype2":
            // WRBJJVBDNNPVB.
            // get WH clause from the sentence

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }
                // get the dep relation in the sentence
                if (depHashMap.containsKey("dep")) {
                    relationkeyword = depHashMap.get("dep");
                    relation = getRelation(depHashMap.get("dep"), whtype.firstKey().toLowerCase());
                }

                // get the nsubj dependent relation in the sentence
                if (depHashMap.containsKey("nsubj")) {
                    searchname = depHashMap.get("nsubj");
                } else if (depHashMap.containsKey("nsubjpass")) {
                    searchname = depHashMap.get("nsubjpass");
                }
            }
            break;

        case "sixtype3":
            // WRBJJVBDNNPVB.
            // get WH clause from the sentence

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }

                // get the dep relation in the sentence
                if (depHashMap.containsKey("dep")) {
                    relationkeyword = depHashMap.get("dep") + " " + govHashMap.get("dep");
                    relation = getRelation(depHashMap.get("dep") + " " + govHashMap.get("dep"),
                            whtype.firstKey().toLowerCase());
                }

                // get the nsubj dependent relation in the sentence
                if (depHashMap.containsKey("nsubj")) {
                    searchname = depHashMap.get("nsubj");
                } else if (depHashMap.containsKey("nsubjpass")) {
                    searchname = depHashMap.get("nsubjpass");
                }
            }
            break;

        case "sixtype4":
            // WRBVBDNNPVBNN.
            // when did CatchMeIfYouCan hit theboxoffice ?
            // not written yet
            // get WH clause from the sentence

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }

                // get the dep relation in the sentence
                if (depHashMap.containsKey("dobj")) {
                    relationkeyword = govHashMap.get("dobj") + " " + depHashMap.get("dobj");
                    relation = getRelation(govHashMap.get("dobj") + " " + depHashMap.get("dobj"),
                            whtype.firstKey().toLowerCase());
                }

                // get the nsubj dependent relation in the sentence
                if (depHashMap.containsKey("nsubj")) {
                    searchname = depHashMap.get("nsubj");
                } else if (depHashMap.containsKey("nsubjpass")) {
                    searchname = depHashMap.get("nsubjpass");
                }
            }
            break;

        case "sixtype5":
            // WPVBDNNINNNP.
            // who was thedirector of AClockworkOrange ?

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }

                // get the nsubj relation in the sentence
                if (depHashMap.containsKey("nsubj")) {
                    relationkeyword = depHashMap.get("nsubj");
                    relation = getRelation(depHashMap.get("nsubj"), whtype.firstKey().toLowerCase());
                } else if (depHashMap.containsKey("nsubjpass")) {
                    relationkeyword = depHashMap.get("nsubjpass");
                    relation = getRelation(depHashMap.get("nsubjpass"), whtype.firstKey().toLowerCase());
                }

                // get the nsubj dependent relation in the sentence
                if (depHashMap.containsKey("prep_" + queryStream.index(3).trim())) {
                    searchname = depHashMap.get("prep_" + queryStream.index(3).trim());
                }
            }
            break;

        case "seventype1":
            // WPVBPNNSVBNINNNP.
            // what are thenotableinstruments played by SamCollins ?

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }

                // get the nsubj relation in the sentence
                if (depHashMap.containsKey("nsubj")) {
                    relationkeyword = depHashMap.get("nsubj") + " " + govHashMap.get("nsubj");
                    relation = getRelation(depHashMap.get("nsubj") + " " + govHashMap.get("nsubj"),
                            whtype.firstKey().toLowerCase());
                } else if (depHashMap.containsKey("nsubjpass")) {
                    relationkeyword = depHashMap.get("nsubjpass") + " " + govHashMap.get("nsubjpass");
                    relation = getRelation(depHashMap.get("nsubjpass") + " " + govHashMap.get("nsubjpass"),
                            whtype.firstKey().toLowerCase());
                }

                // get the nsubj dependent relation in the sentence
                if (depHashMap.containsKey("agent")) {
                    searchname = depHashMap.get("agent");
                }
            }
            break;

        case "":
            // WPVBDNNINNNP.
            // who was thedirector of AClockworkOrange ?

            if (questypemap.get(whclause.toLowerCase().trim()) != null) {
                whtype = getWHMAP(questypemap.get(whclause.toLowerCase().trim()));
                if (whtype == null) {
                    break;
                }

                int s = identifylastleaf(multi, root, 0);
                if (s != 0) {
                    searchname = queryStream.index(s);
                    relationkeyword = getWords(s, queryStream, gs2);
                } else {
                    searchname = nounstr;
                    relationkeyword = verbstr + " " + nounstr;
                }

                // exclude s and 0 and get all the words
                // get the nsubj relation in the sentence
                relationkeyword = getWords(s, queryStream, gs2);

                for (int i = 0; i < queryStream.length(); i++) {
                    for (int j = 0; j < queryStream.length(); j++) {
                        if (multi[i][j] != null && i != j) {

                            System.out.println(multi[i][j] + ":" + type(multi[i][j]));
                            System.out.println(queryStream.index(i) + " --> " + queryStream.index(j));
                        }
                    }
                }
            }

            break;
        default:
            break;
        }
        // System.out.println("WH clause : "+whclause);

        // System.out.println("WH Type : "+whtype);
        // System.out.println("Relation : "+relation);
        // System.out.println("Relation Key word : "+ relationkeyword);
        // System.out.println("Search Name:"+searchname);
        parques.setRelationKeyWord(relationkeyword);
        parques.setSearchName(searchname);
        parques.setWhtype(whtype);

        if (whtype == null || searchname == null || relationkeyword == null) {

            for (int i = 0; i < queryStream.length(); i++) {
                for (int j = 0; j < queryStream.length(); j++) {
                    if (multi[i][j] != null && i != j) {

                        System.out.println(multi[i][j] + ":" + type(multi[i][j]));
                        System.out.println(queryStream.index(i) + " --> " + queryStream.index(j));
                    }
                }
            }
        }

        return parques;
    }
    return null;
}

From source file:com.search.MySearchHandler.java

License:Apache License

public static List<TypedDependency> getTyped(String str) {
    Tree parse = null;//w w w . j  ava 2 s .c o m
    parse = lp.apply(tokenizerFactory.getTokenizer(new StringReader(str)).tokenize());

    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();

    return tdl;

}

From source file:englishparser.EnglishParser.java

/**
 * demoAPI demonstrates other ways of calling the parser with already
 * tokenized text, or in some cases, raw text that needs to be tokenized as
 * a single sentence. Output is handled with a TreePrint object. Note that
 * the options used when creating the TreePrint can determine what results
 * to print out. Once again, one can capture the output by passing a
 * PrintWriter to TreePrint.printTree.//from  w  w  w .ja va 2s.c o m
 */
public static void demoAPI(LexicalizedParser lp) {
    // This option shows parsing a list of correctly tokenized words
    String[] sent = { "This", "is", "an", "easy", "sentence", "." };
    List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
    Tree parse = lp.apply(rawWords);
    parse.pennPrint();
    System.out.println();

    // This option shows loading and using an explicit tokenizer
    String sent2 = "This is another sentence.";
    TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    Tokenizer<CoreLabel> tok = tokenizerFactory.getTokenizer(new StringReader(sent2));
    List<CoreLabel> rawWords2 = tok.tokenize();
    parse = lp.apply(rawWords2);

    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
    System.out.println(tdl);
    System.out.println();

    // You can also use a TreePrint object to print trees and dependencies
    TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    tp.printTree(parse);
}

From source file:percobaan.ParserDemo.java

/**
 * demoAPI demonstrates other ways of calling the parser with
 * already tokenized text, or in some cases, raw text that needs to
 * be tokenized as a single sentence.  Output is handled with a
 * TreePrint object.  Note that the options used when creating the
 * TreePrint can determine what results to print out.  Once again,
 * one can capture the output by passing a PrintWriter to
 * TreePrint.printTree. This code is for English.
 *///from w  ww .j  a  v a 2  s .  c  o  m
//  public static void demoAPI(LexicalizedParser lp) {
public void demoAPI(LexicalizedParser lp) {
    // This option shows parsing a list of correctly tokenized words
    String[] sent = { "This", "is", "an", "easy", "sentence", "." };
    List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
    Tree parse = lp.apply(rawWords);
    //    parse.pennPrint();
    //    System.out.println();

    // This option shows loading and using an explicit tokenizer
    //    String sent2 = "Works great. Instructions were straightforward and easy to follow. Buying a second one just in case I need it in the future.";
    //    for (int i = 0; i < data.lemmaKumpulans.size(); i++){
    for (int i = 0; i < data.lemmaKumpulans.size(); i++) {
        String sent2 = data.lemmaKumpulans.get(i);
        TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
        Tokenizer<CoreLabel> tok = tokenizerFactory.getTokenizer(new StringReader(sent2));
        List<CoreLabel> rawWords2 = tok.tokenize();
        parse = lp.apply(rawWords2);

        TreebankLanguagePack tlp = lp.treebankLanguagePack(); // PennTreebankLanguagePack for English
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
        List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
        //    System.out.println(tdl);
        //    System.out.println();

        // You can also use a TreePrint object to print trees and dependencies
        TreePrint tp = new TreePrint("typedDependenciesCollapsed");
        System.out.println(i + 1);
        tp.printTree(parse); //mengoutputkan hasil parsing

    }
}

From source file:relnextraction.TrainingTool.java

public static void main(String[] args) {

    TokenizerFactory<CoreLabel> tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> rawWords = null;
    Connection c = Dao.getCaseStudy1Connection();
    Statement st1 = null, st2 = null, st3 = null, st4 = null;
    ResultSet rs1 = null, rs2 = null, rs3 = null, rs4 = null;
    int count = 1;

    try {/*from  w  ww  .ja v a  2  s. co m*/
        String stExtract = "select * from sentence where \"isTraining\" = true;";

        st1 = c.createStatement();
        rs1 = st1.executeQuery(stExtract);

        while (rs1.next()) {

            int sentId = rs1.getInt("id");
            String sentence = rs1.getString("content");
            sentence = sentence.replaceAll("\\.(?!.*\\.)", " ").replace("-", " ");
            System.out.println("\n" + sentId + " " + sentence);
            rawWords = tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
            //                System.out.println("\nRawWords : ");
            //                for (int i = 0; i < rawWords.size(); i++) {
            //                    System.out.print(i + "-"+rawWords.get(i) + " ");
            //                }
            Tree parse = lp.apply(rawWords);
            //                TreePrint tp2 = new TreePrint("typedDependenciesCollapsed");
            //                System.out.println("Typed Dependencies : ");
            //                tp2.printTree(parse);
            //                System.out.println("RawWords Size : " + rawWords.size());
            TreebankLanguagePack tlp = new PennTreebankLanguagePack();
            GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
            GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
            Collection<TypedDependency> td = gs.typedDependenciesCCprocessed();
            Object[] list = td.toArray();
            com.hp.hpl.jena.rdf.model.Statement s;
            com.hp.hpl.jena.rdf.model.Statement[][] statements;
            ArrayList<com.hp.hpl.jena.rdf.model.Statement> tripleList;
            Set<String> tempList;
            Model model = ModelFactory.createDefaultModel();
            statements = new com.hp.hpl.jena.rdf.model.Statement[rawWords.size() + 1][rawWords.size() + 1];
            HashMap<Integer, String> PositionCategoryList = new HashMap<>();
            ArrayList<String> criticalCats = new ArrayList<>();
            boolean available, transitive;

            st2 = c.createStatement();
            String wordPosExtract = "select * from wordposition where \"sentId\" = " + sentId + ";";
            //                System.out.println("Query : " + wordPosExtract);
            rs2 = st2.executeQuery(wordPosExtract);
            List<Integer> criticalNodes = new ArrayList<>();

            while (rs2.next()) {

                String category = null;
                int wordId = rs2.getInt("wordId");
                int wordPos = rs2.getInt("wordPos");
                //                    System.out.print("WordId : " + wordId + " Position : " + wordPos);
                //                    System.out.print(wordPos + " ");
                criticalNodes.add(wordPos);

                st3 = c.createStatement();
                String wordCatExtract = "select * from word where \"id\" = " + wordId + ";";
                //                System.out.println("Query : " + wordPosExtract);
                rs3 = st3.executeQuery(wordCatExtract);

                while (rs3.next()) {

                    int catId = rs3.getInt("catId");
                    String word = rs3.getString("name");
                    String wordStem = rs3.getString("stem");
                    st4 = c.createStatement();
                    String catNameExtract = "select * from category where \"id\" = " + catId + ";";
                    //                System.out.println("Query : " + wordPosExtract);
                    rs4 = st4.executeQuery(catNameExtract);
                    if (rs4.next()) {
                        category = rs4.getString("name");
                    }
                    //                        System.out.print(" Category : " + category + " Word : " + word + "\n");

                }
                PositionCategoryList.put(wordPos, category);
                criticalCats.add(category);
            }

            Grph g = new InMemoryGrph();
            Grph g1 = new InMemoryGrph();
            Property edgeProperty = g.getEdgeLabelProperty();
            Property nodeProperty = g.getVertexLabelProperty();
            tripleList = new ArrayList<>();
            tempList = new HashSet<>();
            int vertex1, vertex2;
            HashMap<String, String> replacementMap = new HashMap<>();

            for (int index = 0; index < rawWords.size(); index++) {
                g.addVertex(index + 1);
                String token = rawWords.get(index).toString().trim();
                nodeProperty.setValue(index + 1, token);

                if (PositionCategoryList.containsKey(index + 1)) {
                    replacementMap.put(PositionCategoryList.get(index + 1), token + "-" + (index + 1));
                }
            }
            g.setVerticesLabel(nodeProperty);

            for (int index = 0; index < list.length; index++) {
                //                    System.out.println(((TypedDependency) list[index]).gov().toString().substring(((TypedDependency) list[index]).gov().toString().lastIndexOf("-") + 1));
                //                    System.out.println(((TypedDependency) list[index]).dep().toString().substring(((TypedDependency) list[index]).dep().toString().lastIndexOf("-") + 1));
                vertex1 = Integer.parseInt(((TypedDependency) list[index]).gov().toString()
                        .substring(((TypedDependency) list[index]).gov().toString().lastIndexOf("-") + 1));
                vertex2 = Integer.parseInt(((TypedDependency) list[index]).dep().toString()
                        .substring(((TypedDependency) list[index]).dep().toString().lastIndexOf("-") + 1));

                //                System.out.println("Vertex1 " + vertex1 + " Vertex2 " + vertex2);
                g.addSimpleEdge(vertex1, index + 1, vertex2, false);
                edgeProperty.setValue(index + 1, ((TypedDependency) list[index]).reln().getShortName());

                statements[vertex1][vertex2] = statements[vertex2][vertex1] = ResourceFactory.createStatement(
                        ResourceFactory.createResource(
                                "http://example.uga.edu/" + ((TypedDependency) list[index]).gov().toString()),
                        ResourceFactory.createProperty("http://example.uga.edu/"
                                + ((TypedDependency) list[index]).reln().getShortName()),
                        ResourceFactory.createResource(
                                "http://example.uga.edu/" + ((TypedDependency) list[index]).dep().toString()));
                //                System.out.println("Statements : " + statements[vertex1][vertex2]);
            }
            g.setEdgesLabel(edgeProperty);

            //                System.out.println("HashMap : " + PositionCategoryList.entrySet());
            //                System.out.println("CriticalCats : " + Arrays.toString(criticalCats.toArray()));                
            IntSet abc = new IntHashSet();
            Path p1 = null;
            for (int i = 0; i < criticalNodes.size(); i++) {
                for (int j = 0; j < criticalNodes.size(); j++) {
                    if (i != j && g.containsAPath(criticalNodes.get(i), criticalNodes.get(j))) {
                        p1 = g.getShortestPath(criticalNodes.get(i), criticalNodes.get(j));
                        String pNodes = p1.toString();
                        String[] nds = pNodes.split(" ");
                        for (int k = 0; k < nds.length - 1; k++) {
                            abc.add(Integer.parseInt(nds[k].substring(1)));
                            //                        System.out.println("Vertex : " + Integer.parseInt(nds[k].substring(1)));
                        }
                    }
                }
            }
            //                System.out.println();
            //                System.out.println("TripleList : " + tripleList);
            //                System.out.println("ABC : " + abc);
            g1 = g.getSubgraphInducedByVertices(abc);
            Collection<VertexPair> edgePairs = g1.getEdgePairs();

            for (VertexPair edgePair : edgePairs) {
                //                    System.out.println("EdgePair : (" + edgePair.first + ", " + edgePair.second + ")");
                //                    System.out.println(statements[edgePair.first][edgePair.second]);
                tripleList.add(statements[edgePair.first][edgePair.second]);
            }
            //                g.highlight(g1, 1);
            //                g.display();

            //                System.out.println("TripleList : " + tripleList);
            model.add(tripleList);
            String query = "";
            StmtIterator sIter = model.listStatements(new SimpleSelector());
            while (sIter.hasNext()) {
                s = sIter.nextStatement();
                //                    System.out.println("stmt : " + s);
                query = query + s.getSubject().toString() + " " + s.getPredicate().toString() + " "
                        + s.getObject().toString() + ". ";

                tempList.add(s.getSubject().toString());
                tempList.add(s.getObject().toString());
            }
            //                System.out.println("Query : " + query);
            //                System.out.println("tempList : " + tempList);

            model.remove(tripleList);

            for (String key : replacementMap.keySet()) {
                query = query.replace("http://example.uga.edu/" + replacementMap.get(key), "?" + key);
            }

            int i = 65;
            String toReplace = "";
            for (String replace : tempList) {
                // String toReplace="?"+Character.toString ((char) i);
                toReplace = "?" + String.valueOf(Character.toChars(i));
                query = query.replace(replace, toReplace);
                i++;
            }
            String preQuery = "", postQuery = "";
            for (String cat : criticalCats) {
                preQuery = preQuery + " ?" + cat;
                postQuery = postQuery + " ?" + cat + " textMine:type " + "textMine:" + cat + ".";
            }
            query = query.replace("http://example.uga.edu/", "textMine:");

            //                available = checkAvailability(query);
            //                transitive = transitivityCheck(query);
            if (!checkAvailability(query) && !transitivityCheck(query)) {
                addInTripleList(query);
                String finalQuery = "PREFIX textMine: <http://example.uga.edu/> " + "Select" + preQuery
                        + " where { " + query + postQuery + " }";
                System.out.println(count++);
                //                    System.out.println(count++ + " " + finalQuery + "\n");
                //                    Pattern.Insert3(finalQuery);
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(TrainingTool.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            c.close();
            if (st1 != null) {
                st1.close();
            }
            if (st2 != null) {
                st2.close();
            }
            if (st3 != null) {
                st3.close();
            }
            if (st4 != null) {
                st4.close();
            }
            if (rs1 != null) {
                rs1.close();
            }
            if (rs2 != null) {
                rs2.close();
            }
            if (rs3 != null) {
                rs3.close();
            }
            if (rs4 != null) {
                rs4.close();
            }
        } catch (SQLException e) {
            e.getMessage();
        }
    }

}

From source file:sentenceParser.sentenceParser.java

/**
 *
 * @param input/*  ww w. ja va  2  s. c o m*/
 * @return
 */
public List<String> sentenceDeparser(String input) {
    //String sent2 = ("animals were divided into three groups: 1) rats with alloxan-induced diabetes; 2) diabetic rats treated with isophane insulin (2 iu/day); and 3) matching controls");

    // Use the default tokenizer for this TreebankLanguagePack
    Tokenizer<? extends HasWord> toke = tlp.getTokenizerFactory().getTokenizer(new StringReader(input));
    List<? extends HasWord> sentence = toke.tokenize();

    Tree parseTree = lp.parse(sentence);
    GrammaticalStructure gs = gsf.newGrammaticalStructure(parseTree);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
    List<String> val = new ArrayList<String>();
    ;
    for (TypedDependency tmp : tdl) {
        val.add(tmp.toString());
    }
    return val;
}

From source file:wtute.parser.EssayParser.java

private String parse(Tree toParse) {
    GrammaticalStructure gs = gsf.newGrammaticalStructure(toParse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
    toParse.pennPrint();/*  www. j  av  a2s. c  o  m*/
    System.out.println(tdl);
    return toParse.pennString() + "\n" + tdl + "\n" + toParse.taggedYield() + "\n\n";
}