Ejemplo n.º 1
0
        /// <summary>Parse a JSON formatted tree into a SemanticGraph.</summary>
        /// <param name="jsonString">
        /// The JSON string tree to parse, e.g:
        /// "[{\"\"dependent\"\": 7, \"\"dep\"\": \"\"root\"\", \"\"governorgloss\"\": \"\"root\"\", \"\"governor\"\": 0, \"\"dependentgloss\"\": \"\"sport\"\"}, {\"\"dependent\"\": 1, \"\"dep\"\": \"\"nsubj\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"chess\"\"}, {\"\"dependent\"\": 2, \"\"dep\"\": \"\"cop\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"is\"\"}, {\"\"dependent\"\": 3, \"\"dep\"\": \"\"neg\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"not\"\"}, {\"\"dependent\"\": 4, \"\"dep\"\": \"\"det\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"a\"\"}, {\"\"dependent\"\": 5, \"\"dep\"\": \"\"advmod\"\", \"\"governorgloss\"\": \"\"physical\"\", \"\"governor\"\": 6, \"\"dependentgloss\"\": \"\"predominantly\"\"}, {\"\"dependent\"\": 6, \"\"dep\"\": \"\"amod\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"physical\"\"}, {\"\"dependent\"\": 9, \"\"dep\"\": \"\"advmod\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"yet\"\"}, {\"\"dependent\"\": 10, \"\"dep\"\": \"\"nsubj\"\", \"\"governorgloss\"\": \"\"shooting\"\", \"\"governor\"\": 12, \"\"dependentgloss\"\": \"\"neither\"\"}, {\"\"dependent\"\": 11, \"\"dep\"\": \"\"cop\"\", \"\"governorgloss\"\": \"\"shooting\"\", \"\"governor\"\": 12, \"\"dependentgloss\"\": \"\"are\"\"}, {\"\"dependent\"\": 12, \"\"dep\"\": \"\"parataxis\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"shooting\"\"}, {\"\"dependent\"\": 13, \"\"dep\"\": \"\"cc\"\", \"\"governorgloss\"\": \"\"shooting\"\", \"\"governor\"\": 12, \"\"dependentgloss\"\": \"\"and\"\"}, {\"\"dependent\"\": 14, \"\"dep\"\": \"\"parataxis\"\", \"\"governorgloss\"\": \"\"sport\"\", \"\"governor\"\": 7, \"\"dependentgloss\"\": \"\"curling\"\"}, {\"\"dependent\"\": 14, \"\"dep\"\": \"\"conj:and\"\", \"\"governorgloss\"\": \"\"shooting\"\", \"\"governor\"\": 12, \"\"dependentgloss\"\": \"\"curling\"\"}, {\"\"dependent\"\": 16, \"\"dep\"\": \"\"nsubjpass\"\", \"\"governorgloss\"\": \"\"nicknamed\"\", \"\"governor\"\": 23, \"\"dependentgloss\"\": \"\"which\"\"}, {\"\"dependent\"\": 18, \"\"dep\"\": \"\"case\"\", \"\"governorgloss\"\": \"\"fact\"\", \"\"governor\"\": 19, \"\"dependentgloss\"\": \"\"in\"\"}, {\"\"dependent\"\": 19, \"\"dep\"\": \"\"nmod:in\"\", \"\"governorgloss\"\": \"\"nicknamed\"\", \"\"governor\"\": 23, \"\"dependentgloss\"\": \"\"fact\"\"}, {\"\"dependent\"\": 21, \"\"dep\"\": \"\"aux\"\", \"\"governorgloss\"\": \"\"nicknamed\"\", \"\"governor\"\": 23, \"\"dependentgloss\"\": \"\"has\"\"}, {\"\"dependent\"\": 22, \"\"dep\"\": \"\"auxpass\"\", \"\"governorgloss\"\": \"\"nicknamed\"\", \"\"governor\"\": 23, \"\"dependentgloss\"\": \"\"been\"\"}, {\"\"dependent\"\": 23, \"\"dep\"\": \"\"dep\"\", \"\"governorgloss\"\": \"\"shooting\"\", \"\"governor\"\": 12, \"\"dependentgloss\"\": \"\"nicknamed\"\"}, {\"\"dependent\"\": 25, \"\"dep\"\": \"\"dobj\"\", \"\"governorgloss\"\": \"\"nicknamed\"\", \"\"governor\"\": 23, \"\"dependentgloss\"\": \"\"chess\"\"}, {\"\"dependent\"\": 26, \"\"dep\"\": \"\"case\"\", \"\"governorgloss\"\": \"\"ice\"\", \"\"governor\"\": 27, \"\"dependentgloss\"\": \"\"on\"\"}, {\"\"dependent\"\": 27, \"\"dep\"\": \"\"nmod:on\"\", \"\"governorgloss\"\": \"\"chess\"\", \"\"governor\"\": 25, \"\"dependentgloss\"\": \"\"ice\"\"}, {\"\"dependent\"\": 29, \"\"dep\"\": \"\"amod\"\", \"\"governorgloss\"\": \"\"chess\"\", \"\"governor\"\": 25, \"\"dependentgloss\"\": \"\"5\"\"}]");
        /// </param>
        /// <param name="tokens">The tokens of the sentence, to form the backing labels of the tree.</param>
        /// <returns>A semantic graph of the sentence, according to the given tree.</returns>
        public static SemanticGraph ParseJsonTree(string jsonString, IList <CoreLabel> tokens)
        {
            // Escape quoted string parts
            IJsonReader   json  = Javax.Json.Json.CreateReader(new StringReader(jsonString));
            SemanticGraph tree  = new SemanticGraph();
            IJsonArray    array = json.ReadArray();

            if (array == null || array.IsEmpty())
            {
                return(tree);
            }
            IndexedWord[] vertices = new IndexedWord[tokens.Count + 2];
            // Add edges
            for (int i = 0; i < array.Count; i++)
            {
                IJsonObject entry = array.GetJsonObject(i);
                // Parse row
                int dependentIndex = entry.GetInt("dependent");
                if (vertices[dependentIndex] == null)
                {
                    if (dependentIndex > tokens.Count)
                    {
                        // Bizarre mismatch in sizes; the malt parser seems to do this often
                        return(new SemanticGraph());
                    }
                    vertices[dependentIndex] = new IndexedWord(tokens[dependentIndex - 1]);
                }
                IndexedWord dependent     = vertices[dependentIndex];
                int         governorIndex = entry.GetInt("governor");
                if (governorIndex > tokens.Count)
                {
                    // Bizarre mismatch in sizes; the malt parser seems to do this often
                    return(new SemanticGraph());
                }
                if (vertices[governorIndex] == null && governorIndex > 0)
                {
                    vertices[governorIndex] = new IndexedWord(tokens[governorIndex - 1]);
                }
                IndexedWord governor = vertices[governorIndex];
                string      relation = entry.GetString("dep");
                // Process row
                if (governorIndex == 0)
                {
                    tree.AddRoot(dependent);
                }
                else
                {
                    tree.AddVertex(dependent);
                    if (!tree.ContainsVertex(governor))
                    {
                        tree.AddVertex(governor);
                    }
                    if (!"ref".Equals(relation))
                    {
                        tree.AddEdge(governor, dependent, GrammaticalRelation.ValueOf(Language.English, relation), double.NegativeInfinity, false);
                    }
                }
            }
            return(tree);
        }
Ejemplo n.º 2
0
        /// <summary>Parse a CoNLL formatted tree into a SemanticGraph.</summary>
        /// <param name="conll">The CoNLL tree to parse.</param>
        /// <param name="tokens">The tokens of the sentence, to form the backing labels of the tree.</param>
        /// <returns>A semantic graph of the sentence, according to the given tree.</returns>
        public static SemanticGraph ParseTree(string conll, IList <CoreLabel> tokens)
        {
            SemanticGraph tree = new SemanticGraph();

            if (conll == null || conll.IsEmpty())
            {
                return(tree);
            }
            string[]      treeLines = newline.Split(conll);
            IndexedWord[] vertices  = new IndexedWord[tokens.Count + 2];
            // Add edges
            foreach (string line in treeLines)
            {
                // Parse row
                string[] fields         = tab.Split(line);
                int      dependentIndex = System.Convert.ToInt32(fields[0]);
                if (vertices[dependentIndex] == null)
                {
                    if (dependentIndex > tokens.Count)
                    {
                        // Bizarre mismatch in sizes; the malt parser seems to do this often
                        return(new SemanticGraph());
                    }
                    vertices[dependentIndex] = new IndexedWord(tokens[dependentIndex - 1]);
                }
                IndexedWord dependent     = vertices[dependentIndex];
                int         governorIndex = System.Convert.ToInt32(fields[1]);
                if (governorIndex > tokens.Count)
                {
                    // Bizarre mismatch in sizes; the malt parser seems to do this often
                    return(new SemanticGraph());
                }
                if (vertices[governorIndex] == null && governorIndex > 0)
                {
                    vertices[governorIndex] = new IndexedWord(tokens[governorIndex - 1]);
                }
                IndexedWord governor = vertices[governorIndex];
                string      relation = fields[2];
                // Process row
                if (governorIndex == 0)
                {
                    tree.AddRoot(dependent);
                }
                else
                {
                    tree.AddVertex(dependent);
                    if (!tree.ContainsVertex(governor))
                    {
                        tree.AddVertex(governor);
                    }
                    if (!"ref".Equals(relation))
                    {
                        tree.AddEdge(governor, dependent, GrammaticalRelation.ValueOf(Language.English, relation), double.NegativeInfinity, false);
                    }
                }
            }
            return(tree);
        }
Ejemplo n.º 3
0
        /// <summary>Parse a CoNLL formatted tree into a SemanticGraph object (along with a list of tokens).</summary>
        /// <param name="conll">The CoNLL formatted tree.</param>
        /// <returns>
        /// A pair of a SemanticGraph and a token list, corresponding to the parse of the sentence
        /// and to tokens in the sentence.
        /// </returns>
        protected internal virtual Pair <SemanticGraph, IList <CoreLabel> > MkTree(string conll)
        {
            IList <CoreLabel> sentence = new List <CoreLabel>();
            SemanticGraph     tree     = new SemanticGraph();

            foreach (string line in conll.Split("\n"))
            {
                if (line.Trim().Equals(string.Empty))
                {
                    continue;
                }
                string[]  fields = line.Trim().Split("\\s+");
                int       index  = System.Convert.ToInt32(fields[0]);
                string    word   = fields[1];
                CoreLabel label  = IETestUtils.MkWord(word, index);
                sentence.Add(label);
                if (fields[2].Equals("0"))
                {
                    tree.AddRoot(new IndexedWord(label));
                }
                else
                {
                    tree.AddVertex(new IndexedWord(label));
                }
                if (fields.Length > 4)
                {
                    label.SetTag(fields[4]);
                }
                if (fields.Length > 5)
                {
                    label.SetNER(fields[5]);
                }
                if (fields.Length > 6)
                {
                    label.SetLemma(fields[6]);
                }
            }
            int i = 0;

            foreach (string line_1 in conll.Split("\n"))
            {
                if (line_1.Trim().Equals(string.Empty))
                {
                    continue;
                }
                string[] fields = line_1.Trim().Split("\\s+");
                int      parent = System.Convert.ToInt32(fields[2]);
                string   reln   = fields[3];
                if (parent > 0)
                {
                    tree.AddEdge(new IndexedWord(sentence[parent - 1]), new IndexedWord(sentence[i]), new GrammaticalRelation(Language.UniversalEnglish, reln, null, null), 1.0, false);
                }
                i += 1;
            }
            return(Pair.MakePair(tree, sentence));
        }
Ejemplo n.º 4
0
        public override void Evaluate(SemanticGraph sg, SemgrexMatcher sm)
        {
            IndexedWord rootNode = this.GetNamedNode(rootName, sm);
            ICollection <IndexedWord> subgraphNodeSet = sg.GetSubgraphVertices(rootNode);

            if (!sg.IsDag(rootNode))
            {
                /* Check if there is a cycle going back to the root. */
                foreach (IndexedWord child in sg.GetChildren(rootNode))
                {
                    ICollection <IndexedWord> reachableSet = sg.GetSubgraphVertices(child);
                    if (reachableSet.Contains(rootNode))
                    {
                        throw new ArgumentException("Subtree cannot contain cycle leading back to root node!");
                    }
                }
            }
            IList <IndexedWord> sortedSubgraphNodes = Generics.NewArrayList(subgraphNodeSet);

            sortedSubgraphNodes.Sort();
            IndexedWord newNode = new IndexedWord(rootNode.DocID(), rootNode.SentIndex(), rootNode.Index());

            /* Copy all attributes from rootNode. */
            foreach (Type key in newNode.BackingLabel().KeySet())
            {
                newNode.Set(key, rootNode.Get(key));
            }
            newNode.SetValue(StringUtils.Join(sortedSubgraphNodes.Stream().Map(null), " "));
            newNode.SetWord(StringUtils.Join(sortedSubgraphNodes.Stream().Map(null), " "));
            newNode.SetLemma(StringUtils.Join(sortedSubgraphNodes.Stream().Map(null), " "));
            if (sg.GetRoots().Contains(rootNode))
            {
                sg.GetRoots().Remove(rootNode);
                sg.AddRoot(rootNode);
            }
            foreach (SemanticGraphEdge edge in sg.IncomingEdgeIterable(rootNode))
            {
                sg.AddEdge(edge.GetGovernor(), newNode, edge.GetRelation(), edge.GetWeight(), edge.IsExtra());
            }
            foreach (IndexedWord node in sortedSubgraphNodes)
            {
                sg.RemoveVertex(node);
            }
        }
Ejemplo n.º 5
0
 /// <summary>Returns all of the entailed shortened clauses (as per natural logic) from the given clause.</summary>
 /// <remarks>
 /// Returns all of the entailed shortened clauses (as per natural logic) from the given clause.
 /// This runs the forward entailment component of the OpenIE system only.
 /// It is usually chained together with the clause splitting component:
 /// <see cref="ClausesInSentence(Edu.Stanford.Nlp.Util.ICoreMap)"/>
 /// .
 /// </remarks>
 /// <param name="clause">The premise clause, as a sentence fragment in itself.</param>
 /// <returns>A list of entailed clauses.</returns>
 public virtual IList <SentenceFragment> EntailmentsFromClause(SentenceFragment clause)
 {
     if (clause.parseTree.IsEmpty())
     {
         return(Java.Util.Collections.EmptyList());
     }
     else
     {
         // Get the forward entailments
         IList <SentenceFragment> list = new List <SentenceFragment>();
         if (entailmentsPerSentence > 0)
         {
             Sharpen.Collections.AddAll(list, forwardEntailer.Apply(clause.parseTree, true).Search().Stream().Map(null).Collect(Collectors.ToList()));
         }
         list.Add(clause);
         // A special case for adjective entailments
         IList <SentenceFragment> adjFragments = new List <SentenceFragment>();
         SemgrexMatcher           matcher      = adjectivePattern.Matcher(clause.parseTree);
         while (matcher.Find())
         {
             // (get nodes)
             IndexedWord subj = matcher.GetNode("subj");
             IndexedWord be   = matcher.GetNode("be");
             IndexedWord adj  = matcher.GetNode("adj");
             IndexedWord obj  = matcher.GetNode("obj");
             IndexedWord pobj = matcher.GetNode("pobj");
             string      prep = matcher.GetRelnString("prep");
             // (if the adjective, or any earlier adjective, is privative, then all bets are off)
             foreach (SemanticGraphEdge edge in clause.parseTree.OutgoingEdgeIterable(obj))
             {
                 if ("amod".Equals(edge.GetRelation().ToString()) && edge.GetDependent().Index() <= adj.Index() && Edu.Stanford.Nlp.Naturalli.Util.PrivativeAdjectives.Contains(edge.GetDependent().Word().ToLower()))
                 {
                     goto OUTER_continue;
                 }
             }
             // (create the core tree)
             SemanticGraph tree = new SemanticGraph();
             tree.AddRoot(adj);
             tree.AddVertex(subj);
             tree.AddVertex(be);
             tree.AddEdge(adj, be, GrammaticalRelation.ValueOf(Language.English, "cop"), double.NegativeInfinity, false);
             tree.AddEdge(adj, subj, GrammaticalRelation.ValueOf(Language.English, "nsubj"), double.NegativeInfinity, false);
             // (add pp attachment, if it existed)
             if (pobj != null)
             {
                 System.Diagnostics.Debug.Assert(prep != null);
                 tree.AddEdge(adj, pobj, GrammaticalRelation.ValueOf(Language.English, prep), double.NegativeInfinity, false);
             }
             // (check for monotonicity)
             if (adj.Get(typeof(NaturalLogicAnnotations.PolarityAnnotation)).IsUpwards() && be.Get(typeof(NaturalLogicAnnotations.PolarityAnnotation)).IsUpwards())
             {
                 // (add tree)
                 adjFragments.Add(new SentenceFragment(tree, clause.assumedTruth, false));
             }
             OUTER_continue :;
         }
         OUTER_break :;
         Sharpen.Collections.AddAll(list, adjFragments);
         return(list);
     }
 }
Ejemplo n.º 6
0
            public virtual SemanticGraph ConvertIntermediateGraph(IList <CoreLabel> sentence)
            {
                SemanticGraph graph = new SemanticGraph();
                // First construct the actual nodes; keep them indexed by their index and copy count.
                // Sentences such as "I went over the river and through the woods" have
                // two copies for "went" in the collapsed dependencies.
                TwoDimensionalMap <int, int, IndexedWord> nodeMap = TwoDimensionalMap.HashMap();

                foreach (AnnotationSerializer.IntermediateNode @in in nodes)
                {
                    CoreLabel token = sentence[@in.index - 1];
                    // index starts at 1!
                    IndexedWord word;
                    if (@in.copyAnnotation > 0)
                    {
                        // TODO: if we make a copy wrapper CoreLabel, use it here instead
                        word = new IndexedWord(new CoreLabel(token));
                        word.SetCopyCount(@in.copyAnnotation);
                    }
                    else
                    {
                        word = new IndexedWord(token);
                    }
                    // for backwards compatibility - new annotations should have
                    // these fields set, but annotations older than August 2014 might not
                    if (word.DocID() == null && @in.docId != null)
                    {
                        word.SetDocID(@in.docId);
                    }
                    if (word.SentIndex() < 0 && @in.sentIndex >= 0)
                    {
                        word.SetSentIndex(@in.sentIndex);
                    }
                    if (word.Index() < 0 && @in.index >= 0)
                    {
                        word.SetIndex(@in.index);
                    }
                    nodeMap.Put(word.Index(), word.CopyCount(), word);
                    graph.AddVertex(word);
                    if (@in.isRoot)
                    {
                        graph.AddRoot(word);
                    }
                }
                // add all edges to the actual graph
                foreach (AnnotationSerializer.IntermediateEdge ie in edges)
                {
                    IndexedWord source = nodeMap.Get(ie.source, ie.sourceCopy);
                    if (source == null)
                    {
                        throw new RuntimeIOException("Failed to find node " + ie.source + "-" + ie.sourceCopy);
                    }
                    IndexedWord target = nodeMap.Get(ie.target, ie.targetCopy);
                    if (target == null)
                    {
                        throw new RuntimeIOException("Failed to find node " + ie.target + "-" + ie.targetCopy);
                    }
                    // assert(target != null);
                    lock (Lock)
                    {
                        // this is not thread-safe: there are static fields in GrammaticalRelation
                        GrammaticalRelation rel = GrammaticalRelation.ValueOf(ie.dep);
                        graph.AddEdge(source, target, rel, 1.0, ie.isExtra);
                    }
                }
                // compute root nodes if they weren't stored in the graph
                if (!graph.IsEmpty() && graph.GetRoots().Count == 0)
                {
                    graph.ResetRoots();
                }
                return(graph);
            }
Ejemplo n.º 7
0
        protected internal virtual ICollection <string> Clauses(string conll)
        {
            IList <CoreLabel> sentence = new List <CoreLabel>();
            SemanticGraph     tree     = new SemanticGraph();

            foreach (string line in conll.Split("\n"))
            {
                if (line.Trim().Equals(string.Empty))
                {
                    continue;
                }
                string[]  fields = line.Trim().Split("\\s+");
                int       index  = System.Convert.ToInt32(fields[0]);
                string    word   = fields[1];
                CoreLabel label  = MkWord(word, index);
                sentence.Add(label);
                if (fields[2].Equals("0"))
                {
                    tree.AddRoot(new IndexedWord(label));
                }
                else
                {
                    tree.AddVertex(new IndexedWord(label));
                }
                if (fields.Length > 4)
                {
                    label.SetTag(fields[4]);
                }
                if (fields.Length > 5)
                {
                    label.SetNER(fields[5]);
                }
                if (fields.Length > 6)
                {
                    label.SetLemma(fields[6]);
                }
            }
            int i = 0;

            foreach (string line_1 in conll.Split("\n"))
            {
                if (line_1.Trim().Equals(string.Empty))
                {
                    continue;
                }
                string[] fields = line_1.Trim().Split("\\s+");
                int      parent = System.Convert.ToInt32(fields[2]);
                string   reln   = fields[3];
                if (parent > 0)
                {
                    tree.AddEdge(new IndexedWord(sentence[parent - 1]), new IndexedWord(sentence[i]), new GrammaticalRelation(Language.English, reln, null, null), 1.0, false);
                }
                i += 1;
            }
            // Run extractor
            ClauseSplitterSearchProblem problem = new ClauseSplitterSearchProblem(tree, true);
            ICollection <string>        clauses = new HashSet <string>();

            problem.Search(null, new LinearClassifier <ClauseSplitter.ClauseClassifierLabel, string>(new ClassicCounter <Pair <string, ClauseSplitter.ClauseClassifierLabel> >()), ClauseSplitterSearchProblem.HardSplits, null, 100000);
            return(clauses);
        }