当前位置: 首页>>代码示例>>Java>>正文


Java GrammaticalStructure类代码示例

本文整理汇总了Java中edu.stanford.nlp.trees.GrammaticalStructure的典型用法代码示例。如果您正苦于以下问题:Java GrammaticalStructure类的具体用法?Java GrammaticalStructure怎么用?Java GrammaticalStructure使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


GrammaticalStructure类属于edu.stanford.nlp.trees包,在下文中一共展示了GrammaticalStructure类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: demoDP

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
/**
 * demoDP demonstrates turning a file into tokens and then parse trees. Note
 * that the trees are printed by calling pennPrint on the Tree object. It is
 * also possible to pass a PrintWriter to pennPrint if you want to capture
 * the output.
 * 
 * file => tokens => parse trees
 */
public static void demoDP(LexicalizedParser lp, String filename) {
	// This option shows loading, sentence-segmenting and tokenizing
	// a file using DocumentPreprocessor.
	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
	// You could also create a tokenizer here (as below) and pass it
	// to DocumentPreprocessor
	for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
		Tree parse = lp.apply(sentence);
		parse.pennPrint();
		System.out.println();

		GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
		Collection tdl = gs.typedDependenciesCCprocessed();
		System.out.println(tdl);
		System.out.println();
	}
}
 
开发者ID:opinion-extraction-propagation,项目名称:TASC-Tuples,代码行数:27,代码来源:ParserDemo.java

示例2: getDep

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
public static String getDep(String parse) {
	Tree t;
	StringBuilder sb = new StringBuilder();
	try {
		t = tf.newTreeReader(new StringReader(parse)).readTree();
		GrammaticalStructure gs = gsf.newGrammaticalStructure(t);
		Iterator<TypedDependency> it = gs.typedDependenciesCollapsed()
				.iterator();
		while (it.hasNext()) {
			sb.append(it.next() + "\t");
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	return sb.toString();
}
 
开发者ID:zhangcongle,项目名称:NewsSpikeRe,代码行数:18,代码来源:Preprocessing.java

示例3: initTreeHelper

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
public void initTreeHelper(String sentence) {
		Tokenizer<? extends HasWord> toke = tlp.getTokenizerFactory()
				.getTokenizer(new StringReader(sentence));
		List<? extends HasWord> sentence_token = toke.tokenize();
		
		this.parse = lp.apply(sentence_token);
		
		// stanford pst
		StringBuilder sb = new StringBuilder();
		this.parse.toStringBuilder(sb);
//		System.out.println("PST:\n	" + sb.toString());
		
		// dependency tree
		GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
		this.tdl = gs.typedDependencies();
//		System.out.println("DT:\n	" + tdl);
		
	}
 
开发者ID:T-abide,项目名称:SocialEventExtraction,代码行数:19,代码来源:TreeHelper.java

示例4: testWriteImage

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
/**
 * 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());
}
 
开发者ID:awaisathar,项目名称:dependensee,代码行数:21,代码来源:MainTest.java

示例5: DepTree

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
public DepTree(List<TaggedWord> tag, GrammaticalStructure gs) {
	treestr = "";
	treedepth = 0;
	deepestLevel = 0;
	setDustbin = false;
	adjustLevel = false;
	
	sentence = new DepTreeNode[tag.size() + 1];

	sentence[0] = new DepTreeNode(0, 0, "ROOT", "-");
	for (int i = 0; i < tag.size(); ++i) {
		sentence[i + 1] = new DepTreeNode(i + 1, 1, tag.get(i).word(), tag.get(i)
				.tag());
	}

	for (TypedDependency td : gs.allTypedDependencies()) {
		int head = td.gov().index();
		int son = td.dep().index();
		sentence[son].setHead(sentence[head], td.reln().getShortName());
	}
	
	// set tree node's level
	for (int i = 1;i < sentence.length; i++){
		DepTreeNode cur = sentence[i];
		while (cur.head != null){
			sentence[i].level ++ ;
			cur = cur.head;
		}
	}

	for (int i = 1; i < sentence.length; i++){
		if (treedepth < sentence[i].level){
			treedepth = sentence[i].level;
		}
	}
	
}
 
开发者ID:cs-zyluo,项目名称:CausalNet,代码行数:38,代码来源:DepTree.java

示例6: depParseSentence

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
public static DepTree depParseSentence(String sentence){
	DepTree tree = null;
	DocumentPreprocessor dp = new DocumentPreprocessor(new StringReader(
			sentence));
	List<TaggedWord> tagged;

	for (List<HasWord> sen : dp) { // 只有一句话,只循环一次
		tagged = tagger.tagSentence(sen);
		GrammaticalStructure gs = parser.predict(tagged);
		tree = new DepTree(tagged, gs);
		//tree.printDepBranch(tree.getNode(0), 2);
	}
	
	return tree;
}
 
开发者ID:cs-zyluo,项目名称:CausalNet,代码行数:16,代码来源:DepBranchUtil.java

示例7: ParseTree

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
/**
 * 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]);
	}
}
 
开发者ID:DukeNLIDB,项目名称:NLIDB,代码行数:36,代码来源:ParseTree.java

示例8: main

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
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);
	}
	
}
 
开发者ID:DukeNLIDB,项目名称:NLIDB,代码行数:35,代码来源:ParserDemo.java

示例9: demoAPI

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
/**
 * 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.
 * 
 * difference: already tokenized text
 * 
 * 
 */
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 = "Hey @Apple, pretty much all your products are amazing. You blow minds every time you launch a new gizmo."
			+ " that said, your hold music is crap";
	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);
}
 
开发者ID:opinion-extraction-propagation,项目名称:TASC-Tuples,代码行数:42,代码来源:ParserDemo.java

示例10: map

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
@Override
public WikiDocumentOutput map(WikiDocumentOutput doc) throws Exception {
  System.out.println("Classifying " + doc.getTitle());
  Instances instances;
  WekaUtils wekaUtils = new WekaUtils();
  instances = wekaUtils.createInstances("AllRelations");
  Map<Sentence, GrammaticalStructure> precomputedGraphStore = wekaUtils.getPrecomputedGraphStore();
  Map<IdentifierDefinition, Relation> positiveClassifications = new HashMap<>();
  for (int i = 0; i < doc.getRelations().size(); i++) {
    Relation relation = doc.getRelations().get(i);
    wekaUtils.addRelationToInstances(parser, precomputedGraphStore, doc.getTitle(), doc.getqId(), instances, doc.getMaxSentenceLength(), relation);
    Instances toStringReplace = new Instances(instances, 1);
    toStringReplace.add(instances.get(i));
    Instances stringReplaced = Filter.useFilter(toStringReplace, stringToWordVector);
    Instance instance = stringReplaced.get(0);
    double[] distribution = svm.distributionForInstance(instance);
    String predictedClass = instances.classAttribute().value((int) svm.classifyInstance(instance));
    if (predictedClass.equals(MATCH)) {
      relation.setScore(distribution[instances.classAttribute().indexOfValue(MATCH)]);
      IdentifierDefinition extraction = new IdentifierDefinition(
        instance.stringValue(instance.attribute(instances.attribute(IDENTIFIER).index())),
        instance.stringValue(instance.attribute(instances.attribute(DEFINIEN).index())));
      //put in hashmap to deal with duplicates and preserve highest score.
      if (!positiveClassifications.containsKey(extraction)) {
        positiveClassifications.put(extraction, relation);
      } else {
        if (positiveClassifications.get(extraction).getScore() < relation.getScore()) {
          positiveClassifications.put(extraction, relation);
        }
      }
    }
  }
  //replace relations with positive ones
  doc.setRelations(new ArrayList<>(positiveClassifications.values()));
  System.out.println("Classifying done " + doc.getTitle() + " considered  " + instances.size() + " definiens");
  return doc;
}
 
开发者ID:ag-gipp,项目名称:mathosphere,代码行数:38,代码来源:WekaClassifier.java

示例11: toTypedDependencies

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
/**
 * Transform a parse tree into a list of TypedDependency instances
 * 
 * @param tree
 * @return
 */
public static List<TypedDependency> toTypedDependencies(Tree tree) {
	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	Filter<String> filter = Filters.acceptFilter();
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory(filter, tlp.typedDependencyHeadFinder());
	GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
	return (List<TypedDependency>) gs.typedDependencies();
}
 
开发者ID:hakchul77,项目名称:irnlp_toolkit,代码行数:14,代码来源:StanfordNlpWrapper.java

示例12: createSentence

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
private T2PSentence createSentence(ArrayList<Word> _list) {
	T2PSentence _s = new T2PSentence(_list);
	Tree _parse = f_parser.apply(_s);
	_s.setTree(_parse);
	GrammaticalStructure _gs = f_gsf.newGrammaticalStructure(_parse);
	_s.setGrammaticalStructure(_gs);
	return _s;
}
 
开发者ID:FabianFriedrich,项目名称:Text2Process,代码行数:9,代码来源:T2PStanfordWrapper.java

示例13: annote

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
@Override
void annote(Annotation annotation, List<CoreMap> sentences) {

	List<CoreLabel> labels;
	CoreLabel label;
	CoreMap sentence;
	DependencyTree[] dtree;
	List<Lexel> lexels;
	CompoundProcessor cProcessor;
	List<TaggedWord> tagged;
	GrammaticalStructure gs;
	String tokens[][], tags[][], lemmas[][], words[];
	int size, length;

	size = sentences.size();
	tokens = new String[size][];
	tags = new String[size][];
	lemmas = new String[size][];
	dtree = new DependencyTree[size];

	for (int i = 0; i < size; i++) {

		sentence = sentences.get(i);
		labels = sentence.get(TokensAnnotation.class);
		length = labels.size();
		words = new String[length];
		tags[i] = new String[length];
		lemmas[i] = new String[length];

		for (int j = 0; j < length; j++) {

			label = labels.get(j);
			words[j] = label.get(TextAnnotation.class);
			tags[i][j] = label.get(PartOfSpeechAnnotation.class);
			lemmas[i][j] = label.get(LemmaAnnotation.class);
		}

		tokens[i] = words;
	}

	cProcessor = new CompoundProcessor(annotation.getID());
	lexels = cProcessor.getCompounds(tokens, lemmas, tags);
	annotation.getLexels().addAll(lexels);

	if (mDepParse) {

		for (int i = 0; i < tokens.length; i++) {

			tagged = new ArrayList<TaggedWord>();

			for (int j = 0; j < tokens[i].length; j++)
				tagged.add(new TaggedWord(lemmas[i][j], tags[i][j]));

			gs = mParser.predict(tagged);

			if (gs != null)
				dtree[i] = getDependencyTree(gs);
		}
	}

	annotation.annote(tokens, mPOS ? tags : null, mLemma ? lemmas : null, mDepParse ? dtree : null);
}
 
开发者ID:SI3P,项目名称:supWSD,代码行数:63,代码来源:StanfordPlainPreprocessor.java

示例14: getDependencyTree

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
private DependencyTree getDependencyTree(GrammaticalStructure gs) {

		DependencyTree dtree = null;
		IndexedWord vertex, dep;

		dtree = new DependencyTree();

		for (TypedDependency dependency : gs.typedDependencies()) {

			vertex = dependency.gov();

			if (vertex.tag() != null) {

				dep = dependency.dep();
				dtree.add(vertex.index(), vertex.value(), vertex.tag(), dep.index(), dep.value(), dep.tag(),
						dependency.reln().getShortName().equals("nsubjpass") ? DependencyRelation.PASSIVE
								: DependencyRelation.ACTIVE);
			}
		}

		return dtree;
	}
 
开发者ID:SI3P,项目名称:supWSD,代码行数:23,代码来源:StanfordPlainPreprocessor.java

示例15: annotate

import edu.stanford.nlp.trees.GrammaticalStructure; //导入依赖的package包/类
@Override
public TokenizedCommunication annotate(TokenizedCommunication arg0) throws AnalyticException {
  final Communication root = new Communication(arg0.getRoot());
  if (!root.isSetText())
    throw new AnalyticException("communication.text must be set to run this analytic.");
  AnalyticUUIDGeneratorFactory f = new AnalyticUUIDGeneratorFactory(root);
  AnalyticUUIDGenerator g = f.create();
  final List<Section> sectList = root.getSectionList();
  final String commText = root.getText();

  List<CoreMap> allCoreMaps = new ArrayList<>();
  // String noMarkup = MarkupRewriter.removeMarkup(commText);
  String noMarkup = commText;
  sectList.forEach(sect -> {
    List<CoreMap> cmList = ConcreteToStanfordMapper.concreteSectionToCoreMapList(sect, commText);
    allCoreMaps.addAll(cmList);
  });

  allCoreMaps.forEach(cm -> LOGGER.trace("Got CoreMap pre-coref: {}", cm.toShorterString(new String[0])));
  Annotation anno = new Annotation(allCoreMaps);
  anno.set(TextAnnotation.class, noMarkup);

  // TODO: it's possible that fixNullDependencyGraphs needs to be called
  // before dcoref annotator is called. TB investigated further.
  for (String annotator : this.lang.getPostTokenizationAnnotators()) {
    LOGGER.debug("Running annotator: {}", annotator);
    (StanfordCoreNLP.getExistingAnnotator(annotator)).annotate(anno);
  }

  anno.get(SentencesAnnotation.class).forEach(cm -> LOGGER.trace("Got CoreMaps post-coref: {}", cm.toShorterString(new String[0])));
  // TODO: not sure if this is necessary - found it in the old code.
  anno.get(SentencesAnnotation.class).stream().filter(cm -> cm.containsKey(TreeAnnotation.class)).forEach(cm -> {
    Tree tree = cm.get(TreeAnnotation.class);
    List<Tree> treeList = new ArrayList<>();
    treeList.add(tree);
    ParserAnnotatorUtils.fillInParseAnnotations(false, true, this.lang.getGrammaticalFactory(), cm, treeList.get(0), GrammaticalStructure.Extras.NONE);
  });

  anno.get(SentencesAnnotation.class).forEach(cm -> LOGGER.trace("Got CoreMap post-fill-in: {}", cm.toShorterString(new String[0])));
  List<Sentence> postSentences = annotationToSentenceList(anno, hf, arg0.getSentences(), g);
  postSentences.forEach(st -> LOGGER.trace("Got pre-coref sentence: {}", st.toString()));
  Map<TextSpan, Sentence> tsToSentenceMap = new HashMap<>();
  postSentences.forEach(st -> tsToSentenceMap.put(st.getTextSpan(), st));
  tsToSentenceMap.keySet().forEach(k -> LOGGER.trace("Got TextSpan key: {}", k.toString()));

  sectList.forEach(sect -> {
    List<Sentence> sentList = sect.getSentenceList();
    sentList.forEach(st -> {
      TextSpan ts = st.getTextSpan();
      LOGGER.debug("Trying to find span: {}", ts.toString());
      if (tsToSentenceMap.containsKey(ts)) {
        Sentence newSent = tsToSentenceMap.get(ts);
        st.setTokenization(newSent.getTokenization());
      } else {
        throw new RuntimeException("Didn't find sentence in the new sentences. Old sentence UUID: " + st.getUuid().getUuidString());
      }
    });
  });

  try {
    // Coref.
    CorefManager coref = new CorefManager(new CachedTokenizationCommunication(root), anno);
    TokenizedCommunication tcWithCoref = coref.addCoreference();
    return tcWithCoref;
  } catch (MiscommunicationException e) {
    throw new AnalyticException(e);
  }
}
 
开发者ID:hltcoe,项目名称:concrete-stanford-deprecated2,代码行数:69,代码来源:ConcreteStanfordPreCorefAnalytic.java


注:本文中的edu.stanford.nlp.trees.GrammaticalStructure类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。