edu.stanford.nlp.trees.GrammaticalStructure Java Examples

The following examples show how to use edu.stanford.nlp.trees.GrammaticalStructure. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StanfordRNNDParser.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public void process(JCas jCas) throws AnalysisEngineProcessException {
  mappingProvider.configure(jCas.getCas());
  DKPro2CoreNlp converter = new DKPro2CoreNlp();
  Annotation annotatios = converter.convert(jCas, new Annotation());
  List<CoreMap> sentences = annotatios.get(CoreAnnotations.SentencesAnnotation.class);
  for (CoreMap sentence : sentences) {
    GrammaticalStructure gs = parser.predict(sentence);
    SemanticGraph semanticGraph = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.CCPROCESSED, GrammaticalStructure.Extras.MAXIMAL, null);;
    semanticGraph.prettyPrint();
    semanticGraph = semanticGraphUniversalEnglishToEnglish(semanticGraph);
    sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, semanticGraph);
    for(SemanticGraphEdge edge: semanticGraph.edgeListSorted()) {
      System.out.println(edge);
    }
  }
  convertDependencies(jCas, annotatios, true);
}
 
Example #2
Source File: StanfordParser.java    From gAnswer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public GrammaticalStructure getGrammaticalStructure (String sentence) {
    List<CoreLabel> rawWords2 = 
	      tokenizerFactory.getTokenizer(new StringReader(sentence)).tokenize();
    // Converts a Sentence/List/String into a Tree.
    // In all circumstances, the input will be treated as a single sentence to be parsed.
    Tree parse = lp.apply(rawWords2);

    return gsf.newGrammaticalStructure(parse);
    /*List<TypedDependency> tdl = gs.typedDependencies(false);
    for (TypedDependency td : tdl) {
    	System.out.println(td.reln().getShortName()+"("+td.gov()+","+td.dep()+")");
    	System.out.println("gov="+td.gov()
    			+"\tgov.index="
    			+td.gov().index()
    			+"\tgov.value="
    			+td.gov().value()
    			+"\tgov.pos="
    			+((TreeGraphNode)td.gov().parent()).value());
    }*/
    //System.out.println(tdl);
}
 
Example #3
Source File: NERTool.java    From Criteria2Query with Apache License 2.0 6 votes vote down vote up
/**
 * Word Dependency Author:chi Date:2017-3-22
 * 
 */
public Collection<TypedDependency> outputDependency(Tree t) {
	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	// tlp.setGenerateOriginalDependencies(true); Standford Dependency
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
	GrammaticalStructure gs = gsf.newGrammaticalStructure(t);

	Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed();

	int countforitem = 0;
	int source = 0;
	int target = 0;
	for (TypedDependency item : tdl) {
		System.out.println(item);
	}
	
	return tdl;

}
 
Example #4
Source File: MainTest.java    From dependensee with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of writeImage method, of class Main.
 */

@Test
public void testWriteImage() throws Exception {
    String text = "A quick brown fox jumped over the lazy dog.";
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    LexicalizedParser lp = LexicalizedParser.loadModel();
    lp.setOptionFlags(new String[]{"-maxLength", "500", "-retainTmpSubcategories"});
    TokenizerFactory<CoreLabel> tokenizerFactory =
            PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    List<CoreLabel> wordList = tokenizerFactory.getTokenizer(new StringReader(text)).tokenize();
    Tree tree = lp.apply(wordList);
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
    Main.writeImage(tdl, "image.png", 3);
    assert (new File("image.png").exists());
}
 
Example #5
Source File: StanfordLexicalDemo.java    From Natural-Language-Processing-with-Java-Second-Edition with MIT License 5 votes vote down vote up
public static void main(String args[]){
    String parseModel = getResourcePath() + "englishPCFG.ser.gz";
    LexicalizedParser lexicalizedParser = LexicalizedParser.loadModel(parseModel);
    String [] sentenceArray = {"The", "cow" ,"jumped", "over", "the", "moon", "."};
    List<CoreLabel> words = SentenceUtils.toCoreLabelList(sentenceArray);
    Tree parseTree = lexicalizedParser.apply(words); 
    parseTree.pennPrint(); 
    
    TreePrint treePrint =  new TreePrint("typedDependenciesCollapsed"); 
    treePrint.printTree(parseTree); 
    
    
    String sentence = "The cow jumped over the moon."; 
    TokenizerFactory<CoreLabel> tokenizerFactory =  PTBTokenizer.factory(new CoreLabelTokenFactory(), ""); 
    Tokenizer<CoreLabel> tokenizer =  tokenizerFactory.getTokenizer(new StringReader(sentence)); 
    List<CoreLabel> wordList = tokenizer.tokenize(); 
    parseTree = lexicalizedParser.apply(wordList); 
    TreebankLanguagePack tlp =  lexicalizedParser.treebankLanguagePack(); 
    GrammaticalStructureFactory gsf =  tlp.grammaticalStructureFactory(); 
    GrammaticalStructure gs =  gsf.newGrammaticalStructure(parseTree); 
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed(); 
    System.out.println(tdl); 
    
    for(TypedDependency dependency : tdl) { 
        System.out.println("Governor Word: [" + dependency.gov()  
            + "] Relation: [" + dependency.reln().getLongName() 
            + "] Dependent Word: [" + dependency.dep() + "]"); 
    } 
    
}
 
Example #6
Source File: CoreNLP.java    From Criteria2Query with Apache License 2.0 5 votes vote down vote up
/**
 * Word Dependency Author:chi Date:2017-3-22
 * 
 */
public Collection<TypedDependency> outputDependency(Tree t) {
	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	// tlp.setGenerateOriginalDependencies(true); Standford Dependency
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
	GrammaticalStructure gs = gsf.newGrammaticalStructure(t);
	
	Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
	
	int countforitem = 0;
	int source = 0;
	int target = 0;
	return tdl;

}
 
Example #7
Source File: ParseTree.java    From NLIDB with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a parse tree using the stanford NLP parser. Only one sentence.
 * Here we are omitting the information of dependency labels (tags).
 * @param text input text.
 */
public ParseTree(String text, NLParser parser) {
	// pre-processing the input text
	DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(text));
	List<HasWord> sentence = null;
	for (List<HasWord> sentenceHasWord : tokenizer) {
		sentence = sentenceHasWord;
		break;
	}
	// part-of-speech tagging
	List<TaggedWord> tagged = parser.tagger.tagSentence(sentence);
	// dependency syntax parsing
	GrammaticalStructure gs = parser.parser.predict(tagged);
	
	// Reading the parsed sentence into ParseTree
	int N = sentence.size()+1;
	Node[] nodes = new Node[N];
	root = new Node(0, "ROOT", "ROOT");
	nodes[0] = root;
	for (int i = 0; i < N-1; i++) {
		nodes[i+1] = new Node(i+1, 
				sentence.get(i).word(), tagged.get(i).tag());
	}
	for (TypedDependency typedDep : gs.allTypedDependencies()) {
		int from = typedDep.gov().index();
		int to   = typedDep.dep().index();
		// String label = typedDep.reln().getShortName(); // omitting the label
		nodes[to].parent = nodes[from];
		nodes[from].children.add(nodes[to]);
	}
}
 
Example #8
Source File: ParserDemo.java    From NLIDB with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	String modelPath = DependencyParser.DEFAULT_MODEL;
	String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger";

	for (int argIndex = 0; argIndex < args.length;) {
		switch (args[argIndex]) {
		case "-tagger":
			taggerPath = args[argIndex + 1];
			argIndex += 2;
			break;
		case "-com.dukenlidb.nlidb.model":
			modelPath = args[argIndex + 1];
			argIndex += 2;
			break;
		default:
			throw new RuntimeException("Unknown argument " + args[argIndex]);
		}
	}

	String text = "Return authors who have more papers than Bob in VLDB after 2000";

	MaxentTagger tagger = new MaxentTagger(taggerPath);
	DependencyParser parser = DependencyParser.loadFromModelFile(modelPath);

	DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(text));
	for (List<HasWord> sentence : tokenizer) {
		List<TaggedWord> tagged = tagger.tagSentence(sentence);
		GrammaticalStructure gs = parser.predict(tagged);

		// Print typed dependencies
		log.info(gs);
	}
	
}
 
Example #9
Source File: Chapter7.java    From Natural-Language-Processing-with-Java-Second-Edition with MIT License 4 votes vote down vote up
private static void usingStanfordLexicalizedParser() {
        String parserModel = "C:/Current Books in Progress/NLP and Java/Models/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
        LexicalizedParser lexicalizedParser = LexicalizedParser.loadModel(parserModel);

        // This option shows parsing a list of correctly tokenized words
        System.out.println("---First option");
        String[] senetenceArray = {"The", "cow", "jumped", "over", "the", "moon", "."};
        List<CoreLabel> words = Sentence.toCoreLabelList(senetenceArray);

        Tree parseTree = lexicalizedParser.apply(words);
        parseTree.pennPrint();
        System.out.println();

        // This option shows loading and using an explicit tokenizer
        System.out.println("---Second option");
        String sentence = "The cow jumped over the moon.";
        TokenizerFactory<CoreLabel> tokenizerFactory
                = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
        Tokenizer<CoreLabel> tokenizer
                = tokenizerFactory.getTokenizer(new StringReader(sentence));
        List<CoreLabel> wordList = tokenizer.tokenize();
        parseTree = lexicalizedParser.apply(wordList);

        TreebankLanguagePack tlp = lexicalizedParser.treebankLanguagePack(); // PennTreebankLanguagePack for English
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        GrammaticalStructure gs = gsf.newGrammaticalStructure(parseTree);
        List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
        System.out.println(tdl);
        for (TypedDependency dependency : tdl) {
            System.out.println("Governor Word: [" + dependency.gov() + "] Relation: [" + dependency.reln().getLongName()
                    + "] Dependent Word: [" + dependency.dep() + "]");
        }
        System.out.println();

        // You can also use a TreePrint object to print trees and dependencies
//        System.out.println("---Using TreePrint");
//        TreePrint treePrint = new TreePrint("penn,typedDependenciesCollapsed");
//        treePrint.printTree(parseTree);
//        System.out.println("TreePrint Formats");
//        for (String format : TreePrint.outputTreeFormats) {
//            System.out.println(format);
//        }
//        System.out.println();
    }
 
Example #10
Source File: MainPartExtracter.java    From QuestionAnsweringSystem with Apache License 2.0 4 votes vote down vote up
/**
 * 获取句子的主谓宾
 *
 * @param question 问题
 * @param words HasWord列表
 * @return 问题结构
 */
public QuestionStructure getMainPart(String question, List<edu.stanford.nlp.ling.Word> words) {
    QuestionStructure questionStructure = new QuestionStructure();
    questionStructure.setQuestion(question);

    Tree tree = LP.apply(words);
    LOG.info("句法树: ");
    tree.pennPrint();
    questionStructure.setTree(tree);

    GrammaticalStructure gs = GSF.newGrammaticalStructure(tree);
    if(gs == null){
        return null;
    }
    //获取依存关系
    Collection<TypedDependency> tdls = gs.typedDependenciesCCprocessed(true);
    questionStructure.setTdls(tdls);
    Map<String, String> map = new HashMap<>();
    String top = null;
    String root = null;
    LOG.info("句子依存关系:");
    //依存关系
    List<String> dependencies = new ArrayList<>();
    for (TypedDependency tdl : tdls) {
        String item = tdl.toString();
        dependencies.add(item);
        LOG.info("\t" + item);
        if (item.startsWith("top")) {
            top = item;
        }
        if (item.startsWith("root")) {
            root = item;
        }
        int start = item.indexOf("(");
        int end = item.lastIndexOf(")");
        item = item.substring(start + 1, end);
        String[] attr = item.split(",");
        String k = attr[0].trim();
        String v = attr[1].trim();
        String value = map.get(k);
        if (value == null) {
            map.put(k, v);
        } else {
            //有值
            value += ":";
            value += v;
            map.put(k, value);
        }
    }
    questionStructure.setDependencies(dependencies);

    String mainPartForTop = null;
    String mainPartForRoot = null;
    if (top != null) {
        mainPartForTop = topPattern(top, map);
    }
    if (root != null) {
        mainPartForRoot = rootPattern(root, map);
    }
    questionStructure.setMainPartForTop(mainPartForTop);
    questionStructure.setMainPartForRoot(mainPartForRoot);

    if (questionStructure.getMainPart() == null) {
        LOG.error("未能识别主谓宾:" + question);
    } else {
        LOG.info("主谓宾:" + questionStructure.getMainPart());
    }
    return questionStructure;
}