Java Code Examples for edu.stanford.nlp.util.CoreMap#set()

The following examples show how to use edu.stanford.nlp.util.CoreMap#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StanfordRNNDParser.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public void process(JCas jCas) throws AnalysisEngineProcessException {
  mappingProvider.configure(jCas.getCas());
  DKPro2CoreNlp converter = new DKPro2CoreNlp();
  Annotation annotatios = converter.convert(jCas, new Annotation());
  List<CoreMap> sentences = annotatios.get(CoreAnnotations.SentencesAnnotation.class);
  for (CoreMap sentence : sentences) {
    GrammaticalStructure gs = parser.predict(sentence);
    SemanticGraph semanticGraph = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.CCPROCESSED, GrammaticalStructure.Extras.MAXIMAL, null);;
    semanticGraph.prettyPrint();
    semanticGraph = semanticGraphUniversalEnglishToEnglish(semanticGraph);
    sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, semanticGraph);
    for(SemanticGraphEdge edge: semanticGraph.edgeListSorted()) {
      System.out.println(edge);
    }
  }
  convertDependencies(jCas, annotatios, true);
}
 
Example 2
Source File: ParserAnnotatorUtils.java    From Heracles with GNU General Public License v3.0 4 votes vote down vote up
/** Put the tree in the CoreMap for the sentence, also add any
 *  dependency graphs to the sentence, and fill in missing tag annotations.
 *
 *  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,
                                          List<Tree> trees, GrammaticalStructure.Extras extras) {

  boolean first = true;
  for (Tree tree : trees) {

    // make sure all tree nodes are CoreLabels
    // TODO: why isn't this always true? something fishy is going on
    Trees.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);

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

      setMissingTags(sentence, tree);

      if (buildGraphs) {
        // generate the dependency graph
        // unfortunately, it is necessary to make the
        // GrammaticalStructure three times, as the dependency
        // conversion changes the given data structure
        SemanticGraph deps = SemanticGraphFactory.generateCollapsedDependencies(gsf.newGrammaticalStructure(tree), extras);
        SemanticGraph uncollapsedDeps = SemanticGraphFactory.generateUncollapsedDependencies(gsf.newGrammaticalStructure(tree), extras);
        SemanticGraph ccDeps = SemanticGraphFactory.generateCCProcessedDependencies(gsf.newGrammaticalStructure(tree), extras);
        SemanticGraph enhancedDeps = SemanticGraphFactory.generateEnhancedDependencies(gsf.newGrammaticalStructure(tree));
        SemanticGraph enhancedPlusPlusDeps = SemanticGraphFactory.generateEnhancedPlusPlusDependencies(gsf.newGrammaticalStructure(tree));


        if (verbose) {
          log.info("SDs:");
          log.info(deps.toString(SemanticGraph.OutputFormat.LIST));
        }
        sentence.set(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class, deps);
        sentence.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, uncollapsedDeps);
        sentence.set(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class, ccDeps);
        sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, enhancedDeps);
        sentence.set(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class, enhancedPlusPlusDeps);
      }

      first = false;
    }
  }
  if (trees.size() > 1) {
    sentence.set(TreeCoreAnnotations.KBestTreesAnnotation.class, trees);
  }
}
 
Example 3
Source File: VerbAnnotator.java    From tint with GNU General Public License v3.0 4 votes vote down vote up
@Override public void annotate(Annotation annotation) {
        if (annotation.containsKey(CoreAnnotations.SentencesAnnotation.class)) {
            for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {

                List<CoreLabel> lastVerb = new ArrayList<>();

                List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
                boolean followedByExMark = tokens.get(tokens.size() - 1).word().equals("!");
//                boolean preceededByNot = false;

                List<VerbMultiToken> verbs = new ArrayList<>();

                for (int i = 0; i < tokens.size(); i++) {
                    CoreLabel token = tokens.get(i);

                    String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
//                    System.out.println(token);
//                    System.out.println(pos);
//                    System.out.println();
//                    String form = token.word().toLowerCase();
//                    if (noWords.contains(form)) {
//                        preceededByNot = true;
//                    }

                    if (isSatisfied(pos, verbTags, usePrefix) || isSatisfied(pos, modalTags, modalUsePrefix)) {
                        lastVerb.add(token);
                    }
                    if (isSatisfied(pos, skipTags, usePrefix)) {
                        continue;
                    }
                    if (isSatisfied(pos, auxTags, auxUsePrefix)) {
                        continue;
                    }

                    if (lastVerb.size() > 0) {
                        addVerbs(lastVerb, verbs, followedByExMark);
                        lastVerb = new ArrayList<>();
                    }
                }

                if (lastVerb.size() > 0) {
                    addVerbs(lastVerb, verbs, followedByExMark);
                }

                sentence.set(VerbAnnotations.VerbsAnnotation.class, verbs);
            }
        }
    }