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


Java GrammaticalStructureFactory类代码示例

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


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

示例1: demoDP

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的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: testWriteImage

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的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

示例3: demoAPI

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的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

示例4: toTypedDependencies

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的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

示例5: fillInParseAnnotations

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的package包/类
/**
 * Thread safety note: nothing special is done to ensure the thread
 * safety of the GrammaticalStructureFactory.  However, both the
 * EnglishGrammaticalStructureFactory and the
 * ChineseGrammaticalStructureFactory are thread safe.
 */
public static void fillInParseAnnotations(boolean verbose, boolean buildGraphs, GrammaticalStructureFactory gsf, CoreMap sentence, Tree tree) {
  // make sure all tree nodes are CoreLabels
  // TODO: why isn't this always true? something fishy is going on
  ParserAnnotatorUtils.convertToCoreLabels(tree);

  // index nodes, i.e., add start and end token positions to all nodes
  // this is needed by other annotators down stream, e.g., the NFLAnnotator
  tree.indexSpans(0);

  sentence.set(TreeCoreAnnotations.TreeAnnotation.class, tree);
  if (verbose) {
    System.err.println("Tree is:");
    tree.pennPrint(System.err);
  }

  if (buildGraphs) {
    String docID = sentence.get(CoreAnnotations.DocIDAnnotation.class);
    if (docID == null) {
      docID = "";
    }

    Integer sentenceIndex = sentence.get(CoreAnnotations.SentenceIndexAnnotation.class);
    int index = (sentenceIndex == null) ? 0 : sentenceIndex;

    // generate the dependency graph
    SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gsf.newGrammaticalStructure(tree), docID, index);
    SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gsf.newGrammaticalStructure(tree), docID, index);
    SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gsf.newGrammaticalStructure(tree), docID, index);
    if (verbose) {
      System.err.println("SDs:");
      System.err.println(deps.toString("plain"));
    }
    sentence.set(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class, deps);
    sentence.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, uncollapsedDeps);
    sentence.set(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class, ccDeps);
  }

  setMissingTags(sentence, tree);
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:46,代码来源:ParserAnnotatorUtils.java

示例6: FigerParsedSentence

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的package包/类
public FigerParsedSentence(int sectId, int sentId, CoreMap sentence, GrammaticalStructureFactory gsf) {
	this.sectId = sectId;
	this.sentId = sentId;

	List<String> tokenlist = new ArrayList<String>();
	List<String> poslist = new ArrayList<String>();
	List<String> morphlist = new ArrayList<String>();
	List<String> depslist = new ArrayList<String>();
	List<String> nelist = new ArrayList<String>();
	List<String[]> parsed_sentence = new ArrayList<String[]>();

	List<CoreLabel> labeledtokens = sentence.get(TokensAnnotation.class);
	this.len = labeledtokens.size();
	tkn = new String[len];
	pos = new String[len];
	ner = new String[len];
	lmma = new String[len];
	beginPos = new int[len];
	endPos = new int[len];
	deps = new ArrayList<SentDep>();

	for (int i = 0; i < labeledtokens.size(); i++) {
		CoreLabel token = labeledtokens.get(i);
		beginPos[i] = token.beginPosition();
		endPos[i] = token.endPosition();
		tkn[i] = token.get(TextAnnotation.class);
		pos[i] = token.get(PartOfSpeechAnnotation.class);
		lmma[i] = token.get(LemmaAnnotation.class);
		ner[i] = token.get(NamedEntityTagAnnotation.class);
	}
	Tree tree = sentence.get(TreeAnnotation.class);
	GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
	Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed(true);
	{
		StringBuilder sb = new StringBuilder();
		for (TypedDependency td : tdl) {
			// TypedDependency td = tdl.(i);
			String name = td.reln().getShortName();
			if (td.reln().getSpecific() != null)
				name += "-" + td.reln().getSpecific();

			int gov = td.gov().index();
			int dep = td.dep().index();
			if (gov == dep) {
				// System.err.println("same???");
			}
			if (gov < 1 || dep < 1) {
				continue;
			}
			SentDep sd = new SentDep(gov - 1, dep - 1, name);
			this.deps.add(sd);
		}
	}
}
 
开发者ID:zhangcongle,项目名称:NewsSpikeRe,代码行数:55,代码来源:FigerParsedSentence.java

示例7: ParsedSentence

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的package包/类
public ParsedSentence(int sectId, int sentId, CoreMap sentence, GrammaticalStructureFactory gsf) {
	this.sectId = sectId;
	this.sentId = sentId;

	List<String> tokenlist = new ArrayList<String>();
	List<String> poslist = new ArrayList<String>();
	List<String> morphlist = new ArrayList<String>();
	List<String> depslist = new ArrayList<String>();
	List<String> nelist = new ArrayList<String>();
	List<String[]> parsed_sentence = new ArrayList<String[]>();

	List<CoreLabel> labeledtokens = sentence.get(TokensAnnotation.class);
	this.len = labeledtokens.size();
	tkn = new String[len];
	pos = new String[len];
	ner = new String[len];
	lmma = new String[len];
	beginPos = new int[len];
	endPos = new int[len];
	deps = new ArrayList<SentDep>();

	for (int i = 0; i < labeledtokens.size(); i++) {
		CoreLabel token = labeledtokens.get(i);
		beginPos[i] = token.beginPosition();
		endPos[i] = token.endPosition();
		tkn[i] = token.get(TextAnnotation.class);
		pos[i] = token.get(PartOfSpeechAnnotation.class);
		lmma[i] = token.get(LemmaAnnotation.class);
		ner[i] = token.get(NamedEntityTagAnnotation.class);
	}
	Tree tree = sentence.get(TreeAnnotation.class);
	GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
	Collection<TypedDependency> tdl = gs.typedDependenciesCCprocessed(true);
	{
		StringBuilder sb = new StringBuilder();
		for (TypedDependency td : tdl) {
			// TypedDependency td = tdl.(i);
			String name = td.reln().getShortName();
			if (td.reln().getSpecific() != null)
				name += "-" + td.reln().getSpecific();

			int gov = td.gov().index();
			int dep = td.dep().index();
			if (gov == dep) {
				// System.err.println("same???");
			}
			if (gov < 1 || dep < 1) {
				continue;
			}
			SentDep sd = new SentDep(gov - 1, dep - 1, name);
			this.deps.add(sd);
		}
	}
}
 
开发者ID:zhangcongle,项目名称:NewsSpikeRe,代码行数:55,代码来源:ParsedSentence.java

示例8: getDependencyByLine

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的package包/类
public ArrayList<ArrayList<String>> getDependencyByLine(
		LexicalizedParser lp, String filename, String authorfilename) {
	ArrayList<ArrayList<String>> retArrayList = new ArrayList<ArrayList<String>>();

	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

	try {
		BufferedReader br = new BufferedReader(new FileReader(filename));
		BufferedReader authorReader = new BufferedReader(new FileReader(
				authorfilename));

		String line = "";
		String author = "";
		while ((line = br.readLine()) != null) {
			author = authorReader.readLine();
			Tokenizer<? extends HasWord> toke = tlp.getTokenizerFactory()
					.getTokenizer(new StringReader(line));
			List<? extends HasWord> sentence = toke.tokenize();
			Tree parse = lp.apply(sentence);
			List<Tree> childTrees = parse.getChildrenAsList();
			Stack<Tree> treeStack = new Stack<Tree>();
			treeStack.addAll(childTrees);

			Label prevLabel = null;
			Label curLabel = parse.label();
			HashMap<Integer, Pair<Label, Label>> wordTagMap = new HashMap<Integer, Pair<Label, Label>>();
			int depth = 1;
			while (!treeStack.isEmpty()) {
				Tree curTree = treeStack.pop();
				prevLabel = curLabel;
				curLabel = curTree.label();
				childTrees = curTree.getChildrenAsList();
				if (0 == childTrees.size()) {
					// word node
					wordTagMap.put(depth, new Pair<Label, Label>(curLabel,
							prevLabel));
					depth++;
				} else {
					treeStack.addAll(childTrees);
				}
			}

			final int numWord = wordTagMap.size();
			GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
			List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
			for (TypedDependency typedDep : tdl) {
				int govIndex = typedDep.gov().index();
				int depIndex = typedDep.dep().index();
				if (wordTagMap.containsKey(govIndex)
						&& wordTagMap.containsKey(depIndex)) {
					ArrayList<String> arrList = new ArrayList<String>();
					arrList.add(typedDep.dep().nodeString());
					arrList.add(wordTagMap.get(numWord
							- typedDep.dep().index() + 1).snd.toString());
					arrList.add(typedDep.reln().toString());
					arrList.add(typedDep.gov().nodeString());
					arrList.add(wordTagMap.get(numWord
							- typedDep.gov().index() + 1).snd.toString());
					arrList.add(author);
					arrList.add(line);

					retArrayList.add(arrList);
				}

			}
		}
		br.close();
		authorReader.close();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	return retArrayList;
}
 
开发者ID:opinion-extraction-propagation,项目名称:TASC-Tuples,代码行数:77,代码来源:DependencyParser.java

示例9: getDependencyBySentence

import edu.stanford.nlp.trees.GrammaticalStructureFactory; //导入依赖的package包/类
/**
 * file => tokens => parse trees
 * 
 * @param lp
 * @param filename
 *            tuples
 */
public ArrayList<ArrayList<String>> getDependencyBySentence(
		LexicalizedParser lp, String filename) {
	ArrayList<ArrayList<String>> retArrayList = new ArrayList<ArrayList<String>>();

	TreebankLanguagePack tlp = new PennTreebankLanguagePack();
	GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

	for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
		Tree parse = lp.apply(sentence);

		List<Tree> childTrees = parse.getChildrenAsList();
		Stack<Tree> treeStack = new Stack<Tree>();
		treeStack.addAll(childTrees);

		Label prevLabel = null;
		Label curLabel = parse.label();
		HashMap<Integer, Pair<Label, Label>> wordTagMap = new HashMap<Integer, Pair<Label, Label>>();
		int depth = 1;
		while (!treeStack.isEmpty()) {
			Tree curTree = treeStack.pop();
			prevLabel = curLabel;
			curLabel = curTree.label();
			childTrees = curTree.getChildrenAsList();
			if (0 == childTrees.size()) {
				// word node
				wordTagMap.put(depth, new Pair<Label, Label>(curLabel,
						prevLabel));
				depth++;
			} else {
				treeStack.addAll(childTrees);
			}
		}

		final int numWord = wordTagMap.size();
		GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
		List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
		for (TypedDependency typedDep : tdl) {
			int govIndex = typedDep.gov().index();
			int depIndex = typedDep.dep().index();
			if (wordTagMap.containsKey(govIndex)
					&& wordTagMap.containsKey(depIndex)) {
				ArrayList<String> arrList = new ArrayList<String>();
				arrList.add(typedDep.dep().nodeString());
				arrList.add(wordTagMap.get(numWord - typedDep.dep().index()
						+ 1).snd.toString());
				arrList.add(typedDep.reln().toString());
				arrList.add(typedDep.gov().nodeString());
				arrList.add(wordTagMap.get(numWord - typedDep.gov().index()
						+ 1).snd.toString());

				retArrayList.add(arrList);

			}

		}
	}
	return retArrayList;
}
 
开发者ID:opinion-extraction-propagation,项目名称:TASC-Tuples,代码行数:66,代码来源:DependencyParser.java


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