本文整理汇总了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;
}