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


Java SemanticGraph.getNodeByIndexSafe方法代码示例

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


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

示例1: analyze

import edu.stanford.nlp.semgraph.SemanticGraph; //导入方法依赖的package包/类
public LanguageInfo analyze(String utterance) {
    LanguageInfo languageInfo = new LanguageInfo();

    // Clear these so that analyze can hypothetically be called
    // multiple times.
    languageInfo.tokens.clear();
    languageInfo.posTags.clear();
    languageInfo.nerTags.clear();
    languageInfo.nerValues.clear();
    languageInfo.lemmaTokens.clear();
    languageInfo.dependencyChildren.clear();
    languageInfo.oriTokens = new ArrayList<>();

    // Break hyphens
//    utterance = breakHyphens(utterance);

    // Run Stanford CoreNLP
    initModels();
    Annotation annotation = pipeline.process(utterance);

    for (CoreLabel token : annotation.get(CoreAnnotations.TokensAnnotation.class)) {
      String word = token.get(TextAnnotation.class);
      String wordLower = word.toLowerCase();
      if (LanguageAnalyzer.opts.lowerCaseTokens) {
        languageInfo.tokens.add(wordLower);
      } else {
        languageInfo.tokens.add(word);
      }
      languageInfo.oriTokens.add(word);
      languageInfo.posTags.add(
          AUX_VERBS.contains(wordLower) ?
              AUX_VERB_TAG :
                token.get(PartOfSpeechAnnotation.class));
      languageInfo.nerTags.add(token.get(NamedEntityTagAnnotation.class));
      languageInfo.lemmaTokens.add(token.get(LemmaAnnotation.class));
      languageInfo.nerValues.add(token.get(NormalizedNamedEntityTagAnnotation.class));
    }

    // Fills in a stanford dependency graph for constructing a feature
    for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
      SemanticGraph ccDeps = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
      if (ccDeps == null) continue;
      int sentenceBegin = sentence.get(CoreAnnotations.TokenBeginAnnotation.class);

      // Iterate over all tokens and their dependencies
      for (int sourceTokenIndex = sentenceBegin;
           sourceTokenIndex < sentence.get(CoreAnnotations.TokenEndAnnotation.class);
           sourceTokenIndex++) {
        final ArrayList<DependencyEdge> outgoing = new ArrayList<DependencyEdge>();
        languageInfo.dependencyChildren.add(outgoing);
        IndexedWord node = ccDeps.getNodeByIndexSafe(sourceTokenIndex - sentenceBegin + 1);  // + 1 for ROOT
        if (node != null) {
          for (SemanticGraphEdge edge : ccDeps.outgoingEdgeList(node)) {
            final String relation = edge.getRelation().toString();
            final int targetTokenIndex = sentenceBegin + edge.getTarget().index() - 1;
            outgoing.add(new DependencyEdge(relation, targetTokenIndex));
          }
        }
      }
    }
    return languageInfo;
  }
 
开发者ID:cgraywang,项目名称:TextHIN,代码行数:63,代码来源:CoreNLPAnalyzer.java


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