public virtual void FixedIndex()
 {
     foreach (NaturalLogicRelation rel in NaturalLogicRelation.Values())
     {
         NUnit.Framework.Assert.AreEqual(rel, NaturalLogicRelation.ByFixedIndex(rel.fixedIndex));
     }
 }
コード例 #2
0
        internal Operator(string surfaceForm, NaturalLogicRelation deleteRelation, string subjMono)
        {
            this.surfaceForm    = surfaceForm;
            this.deleteRelation = deleteRelation;
            Pair <Monotonicity, MonotonicityType> subj = MonoFromString(subjMono);

            this.subjMono = subj.first;
            this.subjType = subj.second;
            this.objMono  = Monotonicity.Invalid;
            this.objType  = MonotonicityType.None;
        }
 public virtual void SpotTestJoinTable()
 {
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.Cover, NaturalLogicRelation.Negation.Join(NaturalLogicRelation.ForwardEntailment));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ForwardEntailment, NaturalLogicRelation.Alternation.Join(NaturalLogicRelation.Negation));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ReverseEntailment, NaturalLogicRelation.Cover.Join(NaturalLogicRelation.Alternation));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.Equivalent, NaturalLogicRelation.Negation.Join(NaturalLogicRelation.Negation));
     foreach (NaturalLogicRelation rel in NaturalLogicRelation.Values())
     {
         NUnit.Framework.Assert.AreEqual(rel, NaturalLogicRelation.Equivalent.Join(rel));
         NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.Independence, NaturalLogicRelation.Independence.Join(rel));
         NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.Independence, rel.Join(NaturalLogicRelation.Independence));
     }
 }
コード例 #4
0
 /// <summary>Create a polarity from a list of operators in scope</summary>
 protected internal Polarity(IList <Pair <Monotonicity, MonotonicityType> > operatorsInNarrowingScopeOrder)
 {
     if (operatorsInNarrowingScopeOrder.IsEmpty())
     {
         for (byte i = 0; ((sbyte)i) < projectionFunction.Length; ++i)
         {
             projectionFunction[i] = i;
         }
     }
     else
     {
         for (int rel = 0; rel < 7; ++rel)
         {
             NaturalLogicRelation relation = NaturalLogicRelation.ByFixedIndex(rel);
             for (int op = operatorsInNarrowingScopeOrder.Count - 1; op >= 0; --op)
             {
                 relation = Project(relation, operatorsInNarrowingScopeOrder[op].first, operatorsInNarrowingScopeOrder[op].second);
             }
             projectionFunction[rel] = unchecked ((byte)relation.fixedIndex);
         }
     }
 }
コード例 #5
0
        /// <summary>The search algorithm, starting with a full sentence and iteratively shortening it to its entailed sentences.</summary>
        /// <returns>A list of search results, corresponding to shortenings of the sentence.</returns>
        private IList <ForwardEntailerSearchProblem.SearchResult> SearchImplementation()
        {
            // Pre-process the tree
            SemanticGraph parseTree = new SemanticGraph(this.parseTree);

            System.Diagnostics.Debug.Assert(Edu.Stanford.Nlp.Naturalli.Util.IsTree(parseTree));
            // (remove common determiners)
            IList <string> determinerRemovals = new List <string>();

            parseTree.GetLeafVertices().Stream().Filter(null).ForEach(null);
            // (cut conj_and nodes)
            ICollection <SemanticGraphEdge> andsToAdd = new HashSet <SemanticGraphEdge>();

            foreach (IndexedWord vertex in parseTree.VertexSet())
            {
                if (parseTree.InDegree(vertex) > 1)
                {
                    SemanticGraphEdge conjAnd = null;
                    foreach (SemanticGraphEdge edge in parseTree.IncomingEdgeIterable(vertex))
                    {
                        if ("conj:and".Equals(edge.GetRelation().ToString()))
                        {
                            conjAnd = edge;
                        }
                    }
                    if (conjAnd != null)
                    {
                        parseTree.RemoveEdge(conjAnd);
                        System.Diagnostics.Debug.Assert(Edu.Stanford.Nlp.Naturalli.Util.IsTree(parseTree));
                        andsToAdd.Add(conjAnd);
                    }
                }
            }
            // Clean the tree
            Edu.Stanford.Nlp.Naturalli.Util.CleanTree(parseTree);
            System.Diagnostics.Debug.Assert(Edu.Stanford.Nlp.Naturalli.Util.IsTree(parseTree));
            // Find the subject / object split
            // This takes max O(n^2) time, expected O(n*log(n)) time.
            // Optimal is O(n), but I'm too lazy to implement it.
            BitSet isSubject = new BitSet(256);

            foreach (IndexedWord vertex_1 in parseTree.VertexSet())
            {
                // Search up the tree for a subj node; if found, mark that vertex as a subject.
                IEnumerator <SemanticGraphEdge> incomingEdges = parseTree.IncomingEdgeIterator(vertex_1);
                SemanticGraphEdge edge = null;
                if (incomingEdges.MoveNext())
                {
                    edge = incomingEdges.Current;
                }
                int numIters = 0;
                while (edge != null)
                {
                    if (edge.GetRelation().ToString().EndsWith("subj"))
                    {
                        System.Diagnostics.Debug.Assert(vertex_1.Index() > 0);
                        isSubject.Set(vertex_1.Index() - 1);
                        break;
                    }
                    incomingEdges = parseTree.IncomingEdgeIterator(edge.GetGovernor());
                    if (incomingEdges.MoveNext())
                    {
                        edge = incomingEdges.Current;
                    }
                    else
                    {
                        edge = null;
                    }
                    numIters += 1;
                    if (numIters > 100)
                    {
                        //          log.error("tree has apparent depth > 100");
                        return(Java.Util.Collections.EmptyList);
                    }
                }
            }
            // Outputs
            IList <ForwardEntailerSearchProblem.SearchResult> results = new List <ForwardEntailerSearchProblem.SearchResult>();

            if (!determinerRemovals.IsEmpty())
            {
                if (andsToAdd.IsEmpty())
                {
                    double score = Math.Pow(weights.DeletionProbability("det"), (double)determinerRemovals.Count);
                    System.Diagnostics.Debug.Assert(!double.IsNaN(score));
                    System.Diagnostics.Debug.Assert(!double.IsInfinite(score));
                    results.Add(new ForwardEntailerSearchProblem.SearchResult(parseTree, determinerRemovals, score));
                }
                else
                {
                    SemanticGraph treeWithAnds = new SemanticGraph(parseTree);
                    System.Diagnostics.Debug.Assert(Edu.Stanford.Nlp.Naturalli.Util.IsTree(treeWithAnds));
                    foreach (SemanticGraphEdge and in andsToAdd)
                    {
                        treeWithAnds.AddEdge(and.GetGovernor(), and.GetDependent(), and.GetRelation(), double.NegativeInfinity, false);
                    }
                    System.Diagnostics.Debug.Assert(Edu.Stanford.Nlp.Naturalli.Util.IsTree(treeWithAnds));
                    results.Add(new ForwardEntailerSearchProblem.SearchResult(treeWithAnds, determinerRemovals, Math.Pow(weights.DeletionProbability("det"), (double)determinerRemovals.Count)));
                }
            }
            // Initialize the search
            System.Diagnostics.Debug.Assert(Edu.Stanford.Nlp.Naturalli.Util.IsTree(parseTree));
            IList <IndexedWord> topologicalVertices;

            try
            {
                topologicalVertices = parseTree.TopologicalSort();
            }
            catch (InvalidOperationException)
            {
                //      log.info("Could not topologically sort the vertices! Using left-to-right traversal.");
                topologicalVertices = parseTree.VertexListSorted();
            }
            if (topologicalVertices.IsEmpty())
            {
                return(results);
            }
            Stack <ForwardEntailerSearchProblem.SearchState> fringe = new Stack <ForwardEntailerSearchProblem.SearchState>();

            fringe.Push(new ForwardEntailerSearchProblem.SearchState(new BitSet(256), 0, parseTree, null, null, 1.0));
            // Start the search
            int numTicks = 0;

            while (!fringe.IsEmpty())
            {
                // Overhead with popping a node.
                if (numTicks >= maxTicks)
                {
                    return(results);
                }
                numTicks += 1;
                if (results.Count >= maxResults)
                {
                    return(results);
                }
                ForwardEntailerSearchProblem.SearchState state = fringe.Pop();
                System.Diagnostics.Debug.Assert(state.score > 0.0);
                IndexedWord currentWord = topologicalVertices[state.currentIndex];
                // Push the case where we don't delete
                int nextIndex = state.currentIndex + 1;
                int numIters  = 0;
                while (nextIndex < topologicalVertices.Count)
                {
                    IndexedWord nextWord = topologicalVertices[nextIndex];
                    System.Diagnostics.Debug.Assert(nextWord.Index() > 0);
                    if (!state.deletionMask.Get(nextWord.Index() - 1))
                    {
                        fringe.Push(new ForwardEntailerSearchProblem.SearchState(state.deletionMask, nextIndex, state.tree, null, state, state.score));
                        break;
                    }
                    else
                    {
                        nextIndex += 1;
                    }
                    numIters += 1;
                    if (numIters > 10000)
                    {
                        //          log.error("logic error (apparent infinite loop); returning");
                        return(results);
                    }
                }
                // Check if we can delete this subtree
                bool canDelete = !state.tree.GetFirstRoot().Equals(currentWord);
                foreach (SemanticGraphEdge edge in state.tree.IncomingEdgeIterable(currentWord))
                {
                    if ("CD".Equals(edge.GetGovernor().Tag()))
                    {
                        canDelete = false;
                    }
                    else
                    {
                        // Get token information
                        CoreLabel            token = edge.GetDependent().BackingLabel();
                        OperatorSpec         @operator;
                        NaturalLogicRelation lexicalRelation;
                        Polarity             tokenPolarity = token.Get(typeof(NaturalLogicAnnotations.PolarityAnnotation));
                        if (tokenPolarity == null)
                        {
                            tokenPolarity = Polarity.Default;
                        }
                        // Get the relation for this deletion
                        if ((@operator = token.Get(typeof(NaturalLogicAnnotations.OperatorAnnotation))) != null)
                        {
                            lexicalRelation = @operator.instance.deleteRelation;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(edge.GetDependent().Index() > 0);
                            lexicalRelation = NaturalLogicRelation.ForDependencyDeletion(edge.GetRelation().ToString(), isSubject.Get(edge.GetDependent().Index() - 1));
                        }
                        NaturalLogicRelation projectedRelation = tokenPolarity.ProjectLexicalRelation(lexicalRelation);
                        // Make sure this is a valid entailment
                        if (!projectedRelation.ApplyToTruthValue(truthOfPremise).IsTrue())
                        {
                            canDelete = false;
                        }
                    }
                }
                if (canDelete)
                {
                    // Register the deletion
                    Lazy <Pair <SemanticGraph, BitSet> > treeWithDeletionsAndNewMask = Lazy.Of(null);
                    // Compute the score of the sentence
                    double newScore = state.score;
                    foreach (SemanticGraphEdge edge_1 in state.tree.IncomingEdgeIterable(currentWord))
                    {
                        double multiplier = weights.DeletionProbability(edge_1, state.tree.OutgoingEdgeIterable(edge_1.GetGovernor()));
                        System.Diagnostics.Debug.Assert(!double.IsNaN(multiplier));
                        System.Diagnostics.Debug.Assert(!double.IsInfinite(multiplier));
                        newScore *= multiplier;
                    }
                    // Register the result
                    if (newScore > 0.0)
                    {
                        SemanticGraph resultTree = new SemanticGraph(treeWithDeletionsAndNewMask.Get().first);
                        andsToAdd.Stream().Filter(null).ForEach(null);
                        results.Add(new ForwardEntailerSearchProblem.SearchResult(resultTree, AggregateDeletedEdges(state, state.tree.IncomingEdgeIterable(currentWord), determinerRemovals), newScore));
                        // Push the state with this subtree deleted
                        nextIndex = state.currentIndex + 1;
                        numIters  = 0;
                        while (nextIndex < topologicalVertices.Count)
                        {
                            IndexedWord   nextWord          = topologicalVertices[nextIndex];
                            BitSet        newMask           = treeWithDeletionsAndNewMask.Get().second;
                            SemanticGraph treeWithDeletions = treeWithDeletionsAndNewMask.Get().first;
                            if (!newMask.Get(nextWord.Index() - 1))
                            {
                                System.Diagnostics.Debug.Assert(treeWithDeletions.ContainsVertex(topologicalVertices[nextIndex]));
                                fringe.Push(new ForwardEntailerSearchProblem.SearchState(newMask, nextIndex, treeWithDeletions, null, state, newScore));
                                break;
                            }
                            else
                            {
                                nextIndex += 1;
                            }
                            numIters += 1;
                            if (numIters > 10000)
                            {
                                //              log.error("logic error (apparent infinite loop); returning");
                                return(results);
                            }
                        }
                    }
                }
            }
            // Return
            return(results);
        }
コード例 #6
0
        /// <summary>Encode the projection table in painful detail.</summary>
        /// <param name="input">The input natural logic relation to project up through the operator.</param>
        /// <param name="mono">The monotonicity of the operator we are projecting through.</param>
        /// <param name="type">The monotonicity type of the operator we are projecting through.</param>
        /// <returns>The projected relation, once passed through an operator with the given specifications.</returns>
        private NaturalLogicRelation Project(NaturalLogicRelation input, Monotonicity mono, MonotonicityType type)
        {
            switch (input)
            {
            case NaturalLogicRelation.Equivalent:
            {
                return(NaturalLogicRelation.Equivalent);
            }

            case NaturalLogicRelation.ForwardEntailment:
            {
                switch (mono)
                {
                case Monotonicity.Monotone:
                {
                    return(NaturalLogicRelation.ForwardEntailment);
                }

                case Monotonicity.Antitone:
                {
                    return(NaturalLogicRelation.ReverseEntailment);
                }

                case Monotonicity.Nonmonotone:
                case Monotonicity.Invalid:
                {
                    return(NaturalLogicRelation.Independence);
                }
                }
                goto case NaturalLogicRelation.ReverseEntailment;
            }

            case NaturalLogicRelation.ReverseEntailment:
            {
                switch (mono)
                {
                case Monotonicity.Monotone:
                {
                    return(NaturalLogicRelation.ReverseEntailment);
                }

                case Monotonicity.Antitone:
                {
                    return(NaturalLogicRelation.ForwardEntailment);
                }

                case Monotonicity.Nonmonotone:
                case Monotonicity.Invalid:
                {
                    return(NaturalLogicRelation.Independence);
                }
                }
                goto case NaturalLogicRelation.Negation;
            }

            case NaturalLogicRelation.Negation:
            {
                switch (type)
                {
                case MonotonicityType.None:
                {
                    return(NaturalLogicRelation.Independence);
                }

                case MonotonicityType.Additive:
                {
                    switch (mono)
                    {
                    case Monotonicity.Monotone:
                    {
                        return(NaturalLogicRelation.Cover);
                    }

                    case Monotonicity.Antitone:
                    {
                        return(NaturalLogicRelation.Alternation);
                    }

                    case Monotonicity.Nonmonotone:
                    case Monotonicity.Invalid:
                    {
                        return(NaturalLogicRelation.Independence);
                    }
                    }
                    goto case MonotonicityType.Multiplicative;
                }

                case MonotonicityType.Multiplicative:
                {
                    switch (mono)
                    {
                    case Monotonicity.Monotone:
                    {
                        return(NaturalLogicRelation.Alternation);
                    }

                    case Monotonicity.Antitone:
                    {
                        return(NaturalLogicRelation.Cover);
                    }

                    case Monotonicity.Nonmonotone:
                    case Monotonicity.Invalid:
                    {
                        return(NaturalLogicRelation.Independence);
                    }
                    }
                    break;
                }

                case MonotonicityType.Both:
                {
                    return(NaturalLogicRelation.Negation);
                }
                }
                break;
            }

            case NaturalLogicRelation.Alternation:
            {
                switch (mono)
                {
                case Monotonicity.Monotone:
                {
                    switch (type)
                    {
                    case MonotonicityType.None:
                    case MonotonicityType.Additive:
                    {
                        return(NaturalLogicRelation.Independence);
                    }

                    case MonotonicityType.Multiplicative:
                    case MonotonicityType.Both:
                    {
                        return(NaturalLogicRelation.Alternation);
                    }
                    }
                    goto case Monotonicity.Antitone;
                }

                case Monotonicity.Antitone:
                {
                    switch (type)
                    {
                    case MonotonicityType.None:
                    case MonotonicityType.Additive:
                    {
                        return(NaturalLogicRelation.Independence);
                    }

                    case MonotonicityType.Multiplicative:
                    case MonotonicityType.Both:
                    {
                        return(NaturalLogicRelation.Cover);
                    }
                    }
                    goto case Monotonicity.Nonmonotone;
                }

                case Monotonicity.Nonmonotone:
                case Monotonicity.Invalid:
                {
                    return(NaturalLogicRelation.Independence);
                }
                }
                goto case NaturalLogicRelation.Cover;
            }

            case NaturalLogicRelation.Cover:
            {
                switch (mono)
                {
                case Monotonicity.Monotone:
                {
                    switch (type)
                    {
                    case MonotonicityType.None:
                    case MonotonicityType.Multiplicative:
                    {
                        return(NaturalLogicRelation.Independence);
                    }

                    case MonotonicityType.Additive:
                    case MonotonicityType.Both:
                    {
                        return(NaturalLogicRelation.Cover);
                    }
                    }
                    goto case Monotonicity.Antitone;
                }

                case Monotonicity.Antitone:
                {
                    switch (type)
                    {
                    case MonotonicityType.None:
                    case MonotonicityType.Multiplicative:
                    {
                        return(NaturalLogicRelation.Independence);
                    }

                    case MonotonicityType.Additive:
                    case MonotonicityType.Both:
                    {
                        return(NaturalLogicRelation.Alternation);
                    }
                    }
                    goto case Monotonicity.Nonmonotone;
                }

                case Monotonicity.Nonmonotone:
                case Monotonicity.Invalid:
                {
                    return(NaturalLogicRelation.Independence);
                }
                }
                goto case NaturalLogicRelation.Independence;
            }

            case NaturalLogicRelation.Independence:
            {
                return(NaturalLogicRelation.Independence);
            }
            }
            throw new InvalidOperationException("[should not happen!] Projection table is incomplete for " + mono + " : " + type + " on relation " + input);
        }
コード例 #7
0
 /// <seealso cref="NegatesTruth(NaturalLogicRelation)"/>
 public virtual bool NegatesFalsehood(NaturalLogicRelation lexicalRelation)
 {
     return(ProjectLexicalRelation(lexicalRelation).negatesFalsehood);
 }
コード例 #8
0
 /// <seealso cref="MaintainsTruth(NaturalLogicRelation)"/>
 public virtual bool MaintainsFalsehood(NaturalLogicRelation lexicalRelation)
 {
     return(ProjectLexicalRelation(lexicalRelation).maintainsFalsehood);
 }
コード例 #9
0
 /// <summary>
 /// If true, applying this lexical relation to this word creates a sentence which is negated by the original sentence
 /// Note that both this, and
 /// <see cref="MaintainsTruth(NaturalLogicRelation)"/>
 /// } can be false. If this is the case, then
 /// natural logic can neither verify nor disprove this mutation.
 /// </summary>
 public virtual bool NegatesTruth(NaturalLogicRelation lexicalRelation)
 {
     return(ProjectLexicalRelation(lexicalRelation).negatesTruth);
 }
コード例 #10
0
 /// <summary>
 /// If true, applying this lexical relation to this word creates a sentence which is entailed by the original sentence,
 /// Note that both this, and
 /// <see cref="NegatesTruth(NaturalLogicRelation)"/>
 /// can be false. If this is the case, then
 /// natural logic can neither verify nor disprove this mutation.
 /// </summary>
 public virtual bool MaintainsTruth(NaturalLogicRelation lexicalRelation)
 {
     return(ProjectLexicalRelation(lexicalRelation).maintainsTruth);
 }
コード例 #11
0
 /// <summary>Project the given natural logic lexical relation on this word.</summary>
 /// <remarks>
 /// Project the given natural logic lexical relation on this word. So, for example, if we want to go up the
 /// Hypernymy hierarchy (
 /// <see cref="NaturalLogicRelation.ForwardEntailment"/>
 /// ) on this word,
 /// then this function will tell you what relation holds between the new mutated fact and this fact.
 /// </remarks>
 /// <param name="lexicalRelation">The lexical relation we are applying to this word.</param>
 /// <returns>The relation between the mutated sentence and the original sentence.</returns>
 public virtual NaturalLogicRelation ProjectLexicalRelation(NaturalLogicRelation lexicalRelation)
 {
     return(NaturalLogicRelation.ByFixedIndex(projectionFunction[lexicalRelation.fixedIndex]));
 }
 public virtual void SomeDeletionRelations()
 {
     //    assertEquals(NaturalLogicRelation.INDEPENDENCE, NaturalLogicRelation.forDependencyDeletion("nsubj"));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ReverseEntailment, NaturalLogicRelation.ForDependencyDeletion("quantmod"));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ForwardEntailment, NaturalLogicRelation.ForDependencyDeletion("amod"));
 }
 public virtual void ConjOrPeculiarities()
 {
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ForwardEntailment, NaturalLogicRelation.ForDependencyInsertion("conj:or"));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ForwardEntailment, NaturalLogicRelation.ForDependencyInsertion("conj:or", true));
     NUnit.Framework.Assert.AreEqual(NaturalLogicRelation.ReverseEntailment, NaturalLogicRelation.ForDependencyInsertion("conj:or", false));
 }