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


Java Tree.indexLeaves方法代码示例

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


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

示例1: complexityOf

import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
/**
 * Syntactic complexity as defined by Lin (1996).
 * 
 * @param tree
 * @return
 */
private static int complexityOf(Tree tree) {
  tree.indexLeaves();
  tree.percolateHeads(new CollinsHeadFinder());
  tree.percolateHeadIndices();
  
  Set<Dependency<Label,Label,Object>> deps = tree.dependencies();
  int complexity = 0;
  for (Dependency<Label,Label,Object> dep : deps) {
    if (!(dep instanceof UnnamedConcreteDependency)) {
      throw new RuntimeException("Cannot measure syntactic complexity.");
    }
    UnnamedConcreteDependency uDep = (UnnamedConcreteDependency) dep;
    int headIndex = uDep.getGovernorIndex();
    int depIndex = uDep.getDependentIndex();
    complexity += Math.abs(headIndex - depIndex);
  }
  
  return complexity;
}
 
开发者ID:stanfordnlp,项目名称:phrasal,代码行数:26,代码来源:SourceTextAnalyzer.java

示例2: main

import edu.stanford.nlp.trees.Tree; //导入方法依赖的package包/类
/**
 * Process an English text file.
 * 
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
  if (args.length < 1) {
    System.err.printf("Usage: java %s file [inputproperties_str] > json_output%n", CoreNLPToJSON.class.getName());
    System.exit(-1);
  }
  String textFile = args[0];
  InputProperties inputProperties = args.length > 1 ? InputProperties.fromString(args[1]) : new InputProperties();

  StanfordCoreNLP coreNLP = new StanfordCoreNLP(properties);
  
  // Configure tokenizer
  EnglishPreprocessor preprocessor = new EnglishPreprocessor(true);
  
  // Use a map with ordered keys so that the output is ordered by segmentId.
  Map<Integer,SourceSegment> annotations = new TreeMap<Integer,SourceSegment>();
  LineNumberReader reader = IOTools.getReaderFromFile(textFile);
  for (String line; (line = reader.readLine()) != null;) {
    Annotation annotation = coreNLP.process(line);
    List<CoreMap> sentences = annotation.get(SentencesAnnotation.class);
    if (sentences.size() != 1) {
      throw new RuntimeException("Sentence splitting on line: " + String.valueOf(reader.getLineNumber()));
    }
    CoreMap sentence = sentences.get(0);
    Tree tree = sentence.get(TreeAnnotation.class);
    tree.indexLeaves();
    int[] chunkVector = getChunkVector(tree);
    List<CoreLabel> tokens = sentence.get(TokensAnnotation.class);
    int numTokens = tokens.size();
    SymmetricalWordAlignment alignment = preprocessor.processAndAlign(line);
    if (alignment.e().size() != numTokens) {
      throw new RuntimeException(String.format("Tokenizer configurations differ: %d/%d", alignment.e().size(), numTokens));
    }
    SourceSegment segment = new SourceSegment(numTokens);
    segment.layoutSpec.addAll(makeLayoutSpec(alignment));
    segment.inputProperties = inputProperties.toString();
    for (int j = 0; j < numTokens; ++j) {
      CoreLabel token = tokens.get(j);
      String word = token.get(TextAnnotation.class);
      segment.tokens.add(unescape(word));
      String pos = mapPOS(token.get(PartOfSpeechAnnotation.class));
      segment.pos.add(pos);
      String ne = token.get(NamedEntityTagAnnotation.class);
      segment.ner.add(ne);
      segment.chunkVector[j] = chunkVector[j];
    }
    annotations.put(reader.getLineNumber()-1, segment);
  }
  reader.close();
  System.err.printf("Processed %d sentences%n", reader.getLineNumber());
  
  final SourceDocument jsonDocument = new SourceDocument(textFile, annotations);
  
  // Convert to json
  Gson gson = new Gson();
  String json = gson.toJson(jsonDocument);
  System.out.println(json);
}
 
开发者ID:stanfordnlp,项目名称:phrasal,代码行数:64,代码来源:CoreNLPToJSON.java


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