Пример #1
0
 public virtual void UpwardDownward()
 {
     NUnit.Framework.Assert.AreEqual(true, multiplicative.IsUpwards());
     NUnit.Framework.Assert.AreEqual(true, additive.IsUpwards());
     NUnit.Framework.Assert.AreEqual(false, antimultiplicative.IsUpwards());
     NUnit.Framework.Assert.AreEqual(false, multiplicativeAntiMultiplicative.IsUpwards());
     NUnit.Framework.Assert.AreEqual(false, additiveAntiMultiplicative.IsUpwards());
     NUnit.Framework.Assert.AreEqual(false, multiplicative.IsDownwards());
     NUnit.Framework.Assert.AreEqual(false, additive.IsDownwards());
     NUnit.Framework.Assert.AreEqual(true, antimultiplicative.IsDownwards());
     NUnit.Framework.Assert.AreEqual(true, multiplicativeAntiMultiplicative.IsDownwards());
     NUnit.Framework.Assert.AreEqual(true, additiveAntiMultiplicative.IsDownwards());
 }
        /// <summary>Annotate every token for its polarity, based on the operators found.</summary>
        /// <remarks>
        /// Annotate every token for its polarity, based on the operators found. This function will set the
        /// <see cref="PolarityAnnotation"/>
        /// for every token.
        /// </remarks>
        /// <param name="sentence">
        /// As in
        /// <see cref="DoOneSentence(Edu.Stanford.Nlp.Pipeline.Annotation, Edu.Stanford.Nlp.Util.ICoreMap)"/>
        /// </param>
        private static void AnnotatePolarity(ICoreMap sentence)
        {
            // Collect all the operators in this sentence
            IList <OperatorSpec> operators = new List <OperatorSpec>();
            IList <CoreLabel>    tokens    = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));

            foreach (CoreLabel token in tokens)
            {
                OperatorSpec specOrNull = token.Get(typeof(NaturalLogicAnnotations.OperatorAnnotation));
                if (specOrNull != null)
                {
                    operators.Add(specOrNull);
                }
            }
            // Make sure every node of the dependency tree has a polarity.
            // This is separate from the code below in case the tokens in the dependency
            // tree don't correspond to the tokens in the sentence. This happens at least
            // when the constituency parser craps out on a long sentence, and the
            // dependency tree is put together haphazardly.
            if (sentence.ContainsKey(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)))
            {
                foreach (IndexedWord token_1 in sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)).VertexSet())
                {
                    token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), Polarity.Default);
                }
            }
            if (sentence.ContainsKey(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)))
            {
                foreach (IndexedWord token_1 in sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation)).VertexSet())
                {
                    token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), Polarity.Default);
                }
            }
            if (sentence.ContainsKey(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)))
            {
                foreach (IndexedWord token_1 in sentence.Get(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation)).VertexSet())
                {
                    token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), Polarity.Default);
                }
            }
            // Set polarity for each token
            for (int i = 0; i < tokens.Count; ++i)
            {
                CoreLabel token_1 = tokens[i];
                // Get operators in scope
                IList <Triple <int, Monotonicity, MonotonicityType> > inScope = new List <Triple <int, Monotonicity, MonotonicityType> >(4);
                foreach (OperatorSpec @operator in operators)
                {
                    if (i >= @operator.subjectBegin && i < @operator.subjectEnd)
                    {
                        inScope.Add(Triple.MakeTriple(@operator.subjectEnd - @operator.subjectBegin, @operator.instance.subjMono, @operator.instance.subjType));
                    }
                    else
                    {
                        if (i >= @operator.objectBegin && i < @operator.objectEnd)
                        {
                            inScope.Add(Triple.MakeTriple(@operator.objectEnd - @operator.objectBegin, @operator.instance.objMono, @operator.instance.objType));
                        }
                    }
                }
                // Sort the operators by their scope (approximated by the size of their argument span)
                inScope.Sort(null);
                // Create polarity
                IList <Pair <Monotonicity, MonotonicityType> > info = new List <Pair <Monotonicity, MonotonicityType> >(inScope.Count);
                foreach (Triple <int, Monotonicity, MonotonicityType> term in inScope)
                {
                    info.Add(Pair.MakePair(term.second, term.third));
                }
                Polarity polarity = new Polarity(info);
                // Set polarity
                token_1.Set(typeof(NaturalLogicAnnotations.PolarityAnnotation), polarity);
            }
            // Set the PolarityDirectionAnnotation
            foreach (CoreLabel token_2 in tokens)
            {
                Polarity polarity = token_2.Get(typeof(NaturalLogicAnnotations.PolarityAnnotation));
                if (polarity != null)
                {
                    if (polarity.IsUpwards())
                    {
                        token_2.Set(typeof(NaturalLogicAnnotations.PolarityDirectionAnnotation), "up");
                    }
                    else
                    {
                        if (polarity.IsDownwards())
                        {
                            token_2.Set(typeof(NaturalLogicAnnotations.PolarityDirectionAnnotation), "down");
                        }
                        else
                        {
                            token_2.Set(typeof(NaturalLogicAnnotations.PolarityDirectionAnnotation), "flat");
                        }
                    }
                }
            }
        }