Exemplo n.º 1
0
 /// <summary>Annotate a document (which is usually just a sentence).</summary>
 public virtual void Annotate(StanfordCoreNLP pipeline, Annotation ann)
 {
     if (ann.Get(typeof(CoreAnnotations.SentencesAnnotation)) == null)
     {
         pipeline.Annotate(ann);
     }
     else
     {
         if (ann.Get(typeof(CoreAnnotations.SentencesAnnotation)).Count == 1)
         {
             ICoreMap sentence = ann.Get(typeof(CoreAnnotations.SentencesAnnotation))[0];
             foreach (CoreLabel token in sentence.Get(typeof(CoreAnnotations.TokensAnnotation)))
             {
                 token.Remove(typeof(NaturalLogicAnnotations.OperatorAnnotation));
                 token.Remove(typeof(NaturalLogicAnnotations.PolarityAnnotation));
             }
             sentence.Remove(typeof(NaturalLogicAnnotations.RelationTriplesAnnotation));
             sentence.Remove(typeof(NaturalLogicAnnotations.EntailedSentencesAnnotation));
             sentence.Remove(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
             sentence.Remove(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation));
             sentence.Remove(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation));
             pipeline.Annotate(ann);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>Annotate a single sentence.</summary>
        /// <remarks>
        /// Annotate a single sentence.
        /// This annotator will, in particular, set the
        /// <see cref="EntailedSentencesAnnotation"/>
        /// and
        /// <see cref="RelationTriplesAnnotation"/>
        /// annotations.
        /// </remarks>
        public virtual void AnnotateSentence(ICoreMap sentence, IDictionary <CoreLabel, IList <CoreLabel> > canonicalMentionMap)
        {
            IList <CoreLabel> tokens = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));

            if (tokens.Count < 2)
            {
                // Short sentence. Skip annotating it.
                sentence.Set(typeof(NaturalLogicAnnotations.RelationTriplesAnnotation), Java.Util.Collections.EmptyList());
                if (!stripEntailments)
                {
                    sentence.Set(typeof(NaturalLogicAnnotations.EntailedSentencesAnnotation), Java.Util.Collections.EmptySet());
                }
            }
            else
            {
                // Get the dependency tree
                SemanticGraph parse = sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation));
                if (parse == null)
                {
                    parse = sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                }
                if (parse == null)
                {
                    throw new InvalidOperationException("Cannot run OpenIE without a parse tree!");
                }
                // Clean the tree
                parse = new SemanticGraph(parse);
                Edu.Stanford.Nlp.Naturalli.Util.CleanTree(parse);
                // Resolve Coreference
                SemanticGraph canonicalizedParse = parse;
                if (resolveCoref && !canonicalMentionMap.IsEmpty())
                {
                    canonicalizedParse = CanonicalizeCoref(parse, canonicalMentionMap);
                }
                // Run OpenIE
                // (clauses)
                IList <SentenceFragment> clauses = ClausesInSentence(canonicalizedParse, true);
                // note: uses coref-canonicalized parse
                // (entailment)
                ICollection <SentenceFragment> fragments = EntailmentsFromClauses(clauses);
                // (segment)
                IList <RelationTriple> extractions = segmenter.Extract(parse, tokens);
                // note: uses non-coref-canonicalized parse!
                Sharpen.Collections.AddAll(extractions, RelationsInFragments(fragments, sentence));
                // Set the annotations
                sentence.Set(typeof(NaturalLogicAnnotations.EntailedClausesAnnotation), new HashSet <SentenceFragment>(clauses));
                sentence.Set(typeof(NaturalLogicAnnotations.EntailedSentencesAnnotation), fragments);
                sentence.Set(typeof(NaturalLogicAnnotations.RelationTriplesAnnotation), new List <RelationTriple>(new HashSet <RelationTriple>(extractions)));
                // uniq the extractions
                if (stripEntailments)
                {
                    sentence.Remove(typeof(NaturalLogicAnnotations.EntailedSentencesAnnotation));
                }
            }
        }
 private void CleanupTags(ICoreMap cm, IDictionary <object, bool> cleaned)
 {
     cm.Remove(typeof(Tags.TagsAnnotation));
     foreach (Type key in cm.KeySet())
     {
         object obj = cm.Get(key);
         if (!cleaned.Contains(obj))
         {
             cleaned[obj] = false;
             if (obj is ICoreMap)
             {
                 CleanupTags((ICoreMap)obj, cleaned);
             }
             else
             {
                 if (obj is ICollection)
                 {
                     CleanupTags((ICollection)obj, cleaned);
                 }
             }
             cleaned[obj] = true;
         }
     }
 }