Esempio n. 1
0
 public AddDep(string govNodeName, GrammaticalRelation relation, IndexedWord newNodePrototype)
 {
     this.newNodePrototype = newNodePrototype;
     this.relation         = relation;
     this.govNodeName      = govNodeName;
     this.weight           = 0;
 }
 public DepPattern(Token token, GrammaticalRelation relation)
     : base(PatternFactory.PatternType.Dep)
 {
     this.relations = new List <Pair <Token, GrammaticalRelation> >();
     relations.Add(new Pair <Token, GrammaticalRelation>(token, relation));
     hashCode = this.ToString().GetHashCode();
 }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>Creates an EnglishGrammaticalRelation AddDep edit.</summary>
        /// <param name="newNode">String representation of new dependent IndexedFeatureNode map.</param>
        public static Edu.Stanford.Nlp.Semgraph.Semgrex.Ssurgeon.AddDep CreateEngAddDep(string govNodeName, string engRelation, string newNode)
        {
            GrammaticalRelation relation = EnglishGrammaticalRelations.ValueOf(engRelation);
            //  IndexedWord newNodeObj = new IndexedWord(CoreLabel.fromAbstractMapLabel(IndexedFeatureLabel.valueOf(newNode, MapFactory.HASH_MAP_FACTORY)));
            IndexedWord newNodeObj = FromCheapString(newNode);

            return(new Edu.Stanford.Nlp.Semgraph.Semgrex.Ssurgeon.AddDep(govNodeName, relation, newNodeObj));
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
 public RemoveEdge(GrammaticalRelation relation, string govName, string depName)
 {
     // Name of the matched relation type
     // Name of governor of this reln, in match
     // Name of the dependent in this reln, in match
     this.relation = relation;
     this.govName  = govName;
     this.depName  = depName;
 }
Esempio n. 7
0
 /// <param name="source">The source IndexedWord for this edge</param>
 /// <param name="target">The target IndexedWord for this edge</param>
 /// <param name="relation">The relation between the two words represented by this edge</param>
 /// <param name="weight">A score or weight to attach to the edge (not often used)</param>
 /// <param name="isExtra">Whether or not the dependency this edge represents was "extra"</param>
 public SemanticGraphEdge(IndexedWord source, IndexedWord target, GrammaticalRelation relation, double weight, bool isExtra)
 {
     // a hack for displaying SemanticGraph in JGraph.  Should be redone better.
     this.source   = source;
     this.target   = target;
     this.relation = relation;
     this.weight   = weight;
     this.isExtra  = isExtra;
 }
 /// <exception cref="System.Exception"/>
 private static void DescendantsHelper(SemanticGraph g, IndexedWord curr, ICollection <IndexedWord> descendantSet, IList <string> allCutOffRels, IList <IndexedWord> doNotAddThese, IList <IndexedWord> seenNodes, bool ignoreCommonTags, IPredicate <CoreLabel
                                                                                                                                                                                                                                                      > acceptWord, CollectionValuedMap <int, string> feat)
 {
     if (seenNodes.Contains(curr))
     {
         return;
     }
     seenNodes.Add(curr);
     if (descendantSet.Contains(curr) || (doNotAddThese != null && doNotAddThese.Contains(curr)) || !acceptWord.Test(curr.BackingLabel()))
     {
         return;
     }
     if (!ignoreCommonTags || !ignoreTags.Contains(curr.Tag().Trim()))
     {
         descendantSet.Add(curr);
     }
     foreach (IndexedWord child in g.GetChildren(curr))
     {
         bool dontuse = false;
         if (doNotAddThese != null && doNotAddThese.Contains(child))
         {
             dontuse = true;
         }
         GrammaticalRelation rel = null;
         if (dontuse == false)
         {
             rel     = g.Reln(curr, child);
             dontuse = CheckIfSatisfiesRelConstrains(g, curr, child, allCutOffRels, rel);
         }
         if (dontuse == false)
         {
             foreach (string cutOffTagRegex in cutoffTags)
             {
                 if (child.Tag().Matches(cutOffTagRegex))
                 {
                     if (Debug >= 5)
                     {
                         System.Console.Out.WriteLine("ignored tag " + child + " because it satisfied " + cutOffTagRegex);
                     }
                     dontuse = true;
                     break;
                 }
             }
         }
         if (dontuse == false)
         {
             if (!feat.Contains(curr.Index()))
             {
                 feat[curr.Index()] = new List <string>();
             }
             GetPatternsFromDataMultiClass.GetFeatures(g, curr, false, feat[curr.Index()], rel);
             //feat.add(curr.index(), "REL-" + rel.getShortName());
             DescendantsHelper(g, child, descendantSet, allCutOffRels, doNotAddThese, seenNodes, ignoreCommonTags, acceptWord, feat);
         }
     }
 }
Esempio n. 9
0
 public AddEdge(string govName, string depName, GrammaticalRelation relation)
 {
     // Name of governor of this reln, in match
     // Name of the dependent in this reln, in match
     // Type of relation to add between these edges
     this.govName  = govName;
     this.depName  = depName;
     this.relation = relation;
     this.weight   = 0;
 }
 private static bool IfIgnoreRel(GrammaticalRelation rel)
 {
     if (ignoreRelsSet.Contains(rel))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 11
0
        public override void Evaluate(SemanticGraph sg, SemgrexMatcher sm)
        {
            string            relation = sm.GetRelnString(edgeName);
            IndexedWord       govNode  = GetNamedNode(govName, sm);
            IndexedWord       depNode  = GetNamedNode(depName, sm);
            SemanticGraphEdge edge     = sg.GetEdge(govNode, depNode, GrammaticalRelation.ValueOf(relation));

            if (edge != null)
            {
                sg.RemoveEdge(edge);
            }
        }
 public static void SetUp(Properties props)
 {
     ArgumentParser.FillOptions(typeof(DepPatternFactory), props);
     ArgumentParser.FillOptions(typeof(PatternFactory), props);
     foreach (string s in ignoreRels.Split("[,;]"))
     {
         ignoreRelsSet.Add(GrammaticalRelation.ValueOf(s));
     }
     foreach (string s_1 in allowedTagsForTrigger.Split("[,;]"))
     {
         allowedTagPatternForTrigger.Add(Pattern.Compile(s_1));
     }
 }
        internal static ICollection <DepPattern> GetContext(IndexedWord w, SemanticGraph graph, ICollection <CandidatePhrase> stopWords, DataInstance sent)
        {
            ICollection <DepPattern> patterns = new HashSet <DepPattern>();
            IndexedWord node  = w;
            int         depth = 1;

            while (depth <= upDepth)
            {
                IndexedWord parent = graph.GetParent(node);
                if (parent == null)
                {
                    break;
                }
                GrammaticalRelation rel = graph.Reln(parent, node);
                foreach (Pattern tagPattern in allowedTagPatternForTrigger)
                {
                    if (tagPattern.Matcher(parent.Tag()).Matches())
                    {
                        if (!IfIgnoreRel(rel) && !stopWords.Contains(CandidatePhrase.CreateOrGet(parent.Word())) && parent.Word().Length > 1)
                        {
                            Pair <IndexedWord, GrammaticalRelation> pattern = new Pair <IndexedWord, GrammaticalRelation>(parent, rel);
                            DepPattern patterndep = PatternToDepPattern(pattern, sent);
                            if (depth <= upDepth)
                            {
                                patterns.Add(patterndep);
                            }
                        }
                    }
                }
                //                    if (depth <= maxDepth) {
                //                      Counter<String> phrasesForPattern = phrasesForPatternForSent.get(patternStr);
                //                      if (phrasesForPattern == null)
                //                        phrasesForPattern = new ClassicCounter<String>();
                //                      phrasesForPattern.incrementCount(phrase);
                //                      phrasesForPatternForSent.put(patternStr, phrasesForPattern);
                //                    }
                //                    if (DEBUG >= 1)
                //                      System.out.println("for phrase " + phrase + " pattern is " + patternStr);
                node = parent;
                depth++;
            }
            return(patterns);
        }
Esempio n. 14
0
        private void ExtractNPorPRPFromDependency(ICoreMap s, IList <Mention> mentions, ICollection <IntPair> mentionSpanSet, ICollection <IntPair> namedEntitySpanSet)
        {
            IList <CoreLabel>   sent       = s.Get(typeof(CoreAnnotations.TokensAnnotation));
            SemanticGraph       basic      = s.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
            IList <IndexedWord> nounsOrPrp = basic.GetAllNodesByPartOfSpeechPattern("N.*|PRP.*|DT");
            // DT is for "this, these, etc"
            Tree tree = s.Get(typeof(TreeCoreAnnotations.TreeAnnotation));

            foreach (IndexedWord w in nounsOrPrp)
            {
                SemanticGraphEdge   edge = basic.GetEdge(basic.GetParent(w), w);
                GrammaticalRelation rel  = null;
                string shortname         = "root";
                // if edge is null, it's root
                if (edge != null)
                {
                    rel       = edge.GetRelation();
                    shortname = rel.GetShortName();
                }
                // TODO: what to remove? remove more?
                if (shortname.Matches("det|compound"))
                {
                    //        // for debug  ---------------
                    //        Tree t = tree.getLeaves().get(w.index()-1);
                    //        for(Tree p : tree.pathNodeToNode(t, tree)) {
                    //          if(p.label().value().equals("NP")) {
                    //            HeadFinder headFinder = new SemanticHeadFinder();
                    //            Tree head = headFinder.determineHead(p);
                    //            if(head == t.parent(tree)) {
                    //              log.info();
                    //            }
                    //            break;
                    //          }
                    //        } // for debug -------------
                    continue;
                }
                else
                {
                    ExtractMentionForHeadword(w, basic, s, mentions, mentionSpanSet, namedEntitySpanSet);
                }
            }
        }
Esempio n. 15
0
            private static Pair <IndexedWord, GrammaticalRelation> GetGovAndReln(int govIdx, int copyCount, IndexedWord word, string relationName, IList <IndexedWord> sortedTokens)
            {
                IndexedWord         gov;
                GrammaticalRelation reln;

                if (relationName.Equals("root"))
                {
                    reln = GrammaticalRelation.Root;
                }
                else
                {
                    reln = GrammaticalRelation.ValueOf(Language.UniversalEnglish, relationName);
                }
                if (govIdx == 0)
                {
                    gov = new IndexedWord(word.DocID(), word.SentIndex(), 0);
                    gov.SetValue("ROOT");
                }
                else
                {
                    gov = CoNLLUDocumentReader.SentenceProcessor.GetToken(sortedTokens, govIdx, copyCount);
                }
                return(Generics.NewPair(gov, reln));
            }
Esempio n. 16
0
        public static Edu.Stanford.Nlp.Semgraph.Semgrex.Ssurgeon.AddEdge CreateEngAddEdge(string govName, string depName, string engRelnName, double weight)
        {
            GrammaticalRelation reln = EnglishGrammaticalRelations.ValueOf(engRelnName);

            return(new Edu.Stanford.Nlp.Semgraph.Semgrex.Ssurgeon.AddEdge(govName, depName, reln, weight));
        }
Esempio n. 17
0
 public AddEdge(string govName, string depName, GrammaticalRelation relation, double weight)
     : this(govName, depName, relation)
 {
     this.weight = weight;
 }
Esempio n. 18
0
 /// <summary>
 /// Compares grammatical relations.
 /// </summary>
 /// <param name="gr1"></param>
 /// <param name="gr2"></param>
 /// <returns></returns>
 public static bool IsGrammaticalRelation(this GrammaticalRelation gr1, GrammaticalRelation gr2)
 {
     return gr1.getLongName() == gr2.getLongName();
 }
Esempio n. 19
0
        public virtual string PrintSemanticGraph(SemanticGraph sg, bool unescapeParenthesis)
        {
            bool          isTree = SemanticGraphUtils.IsTree(sg);
            StringBuilder sb     = new StringBuilder();

            /* Print comments. */
            foreach (string comment in sg.GetComments())
            {
                sb.Append(comment).Append("\n");
            }
            foreach (IndexedWord token in sg.VertexListSorted())
            {
                /* Check for multiword tokens. */
                if (token.ContainsKey(typeof(CoreAnnotations.CoNLLUTokenSpanAnnotation)))
                {
                    IntPair tokenSpan = token.Get(typeof(CoreAnnotations.CoNLLUTokenSpanAnnotation));
                    if (tokenSpan.GetSource() == token.Index())
                    {
                        string range = string.Format("%d-%d", tokenSpan.GetSource(), tokenSpan.GetTarget());
                        sb.Append(string.Format("%s\t%s\t_\t_\t_\t_\t_\t_\t_\t_%n", range, token.OriginalText()));
                    }
                }
                /* Try to find main governor and additional dependencies. */
                string govIdx = null;
                GrammaticalRelation         reln = null;
                Dictionary <string, string> enhancedDependencies = new Dictionary <string, string>();
                foreach (IndexedWord parent in sg.GetParents(token))
                {
                    SemanticGraphEdge edge = sg.GetEdge(parent, token);
                    if (govIdx == null && !edge.IsExtra())
                    {
                        govIdx = parent.ToCopyIndex();
                        reln   = edge.GetRelation();
                    }
                    enhancedDependencies[parent.ToCopyIndex()] = edge.GetRelation().ToString();
                }
                string additionalDepsString = isTree ? "_" : CoNLLUUtils.ToExtraDepsString(enhancedDependencies);
                string word           = token.Word();
                string featuresString = CoNLLUUtils.ToFeatureString(token.Get(typeof(CoreAnnotations.CoNLLUFeats)));
                string pos            = token.GetString <CoreAnnotations.PartOfSpeechAnnotation>("_");
                string upos           = token.GetString <CoreAnnotations.CoarseTagAnnotation>("_");
                string misc           = token.GetString <CoreAnnotations.CoNLLUMisc>("_");
                string lemma          = token.GetString <CoreAnnotations.LemmaAnnotation>("_");
                string relnName       = reln == null ? "_" : reln.ToString();
                /* Root. */
                if (govIdx == null && sg.GetRoots().Contains(token))
                {
                    govIdx               = "0";
                    relnName             = GrammaticalRelation.Root.ToString();
                    additionalDepsString = isTree ? "_" : "0:" + relnName;
                }
                else
                {
                    if (govIdx == null)
                    {
                        govIdx   = "_";
                        relnName = "_";
                    }
                }
                if (unescapeParenthesis)
                {
                    word  = word.ReplaceAll(LrbPattern, "(");
                    word  = word.ReplaceAll(RrbPattern, ")");
                    lemma = lemma.ReplaceAll(LrbPattern, "(");
                    lemma = lemma.ReplaceAll(RrbPattern, ")");
                }
                sb.Append(string.Format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s%n", token.ToCopyIndex(), word, lemma, upos, pos, featuresString, govIdx, relnName, additionalDepsString, misc));
            }
            sb.Append("\n");
            return(sb.ToString());
        }
Esempio n. 20
0
            public virtual SemanticGraph Apply(string line)
            {
                if (line == null)
                {
                    return(null);
                }
                IFunction <string, IndexedWord> func     = new CoNLLUDocumentReader.WordProcessor();
                ObjectBank <IndexedWord>        words    = ObjectBank.GetLineIterator(new StringReader(line), func);
                IList <IndexedWord>             wordList = new List <IndexedWord>(words);
                IList <IndexedWord>             sorted   = new List <IndexedWord>(wordList.Count);
                IList <string> comments = new LinkedList <string>();

                /* Increase the line number in case there are comments before the actual sentence
                 * and add them to the list of comments. */
                wordList.Stream().Filter(null).ForEach(null);
                wordList.Stream().Filter(null).Sorted(byIndex.ThenComparing(byType)).ForEach(null);
                IList <IndexedWord> sortedTokens = new List <IndexedWord>(wordList.Count);

                sorted.Stream().Filter(null).Filter(null).ForEach(null);
                sorted.Stream().Filter(null).Filter(null).ForEach(null);
                /* Construct a semantic graph. */
                IList <TypedDependency> deps = new List <TypedDependency>(sorted.Count);
                IntPair tokenSpan            = null;
                string  originalToken        = null;

                foreach (IndexedWord word in sorted)
                {
                    lineNumberCounter++;
                    if (word.ContainsKey(typeof(CoreAnnotations.CoNLLUTokenSpanAnnotation)))
                    {
                        tokenSpan     = word.Get(typeof(CoreAnnotations.CoNLLUTokenSpanAnnotation));
                        originalToken = word.Word();
                    }
                    else
                    {
                        /* Deal with multiword tokens. */
                        if (tokenSpan != null && tokenSpan.GetTarget() >= word.Index())
                        {
                            word.SetOriginalText(originalToken);
                            word.Set(typeof(CoreAnnotations.CoNLLUTokenSpanAnnotation), tokenSpan);
                        }
                        else
                        {
                            tokenSpan     = null;
                            originalToken = null;
                        }
                        Dictionary <string, string> extraDeps = word.Get(typeof(CoreAnnotations.CoNLLUSecondaryDepsAnnotation));
                        if (extraDeps.IsEmpty())
                        {
                            int govIdx = word.Get(typeof(CoreAnnotations.CoNLLDepParentIndexAnnotation));
                            Pair <IndexedWord, GrammaticalRelation> govReln = GetGovAndReln(govIdx, 0, word, word.Get(typeof(CoreAnnotations.CoNLLDepTypeAnnotation)), sortedTokens);
                            IndexedWord         gov  = govReln.First();
                            GrammaticalRelation reln = govReln.Second();
                            TypedDependency     dep  = new TypedDependency(reln, gov, word);
                            word.Set(typeof(CoreAnnotations.LineNumberAnnotation), lineNumberCounter);
                            deps.Add(dep);
                        }
                        else
                        {
                            foreach (string extraGovIdxStr in extraDeps.Keys)
                            {
                                if (extraGovIdxStr.Contains("."))
                                {
                                    string[] indexParts  = extraGovIdxStr.Split("\\.");
                                    int      extraGovIdx = System.Convert.ToInt32(indexParts[0]);
                                    int      copyCount   = System.Convert.ToInt32(indexParts[1]);
                                    Pair <IndexedWord, GrammaticalRelation> govReln = GetGovAndReln(extraGovIdx, copyCount, word, extraDeps[extraGovIdxStr], sortedTokens);
                                    IndexedWord         gov  = govReln.First();
                                    GrammaticalRelation reln = govReln.Second();
                                    TypedDependency     dep  = new TypedDependency(reln, gov, word);
                                    dep.SetExtra();
                                    deps.Add(dep);
                                }
                                else
                                {
                                    int extraGovIdx = System.Convert.ToInt32(extraGovIdxStr);
                                    int mainGovIdx  = word.Get(typeof(CoreAnnotations.CoNLLDepParentIndexAnnotation)) != null?word.Get(typeof(CoreAnnotations.CoNLLDepParentIndexAnnotation)) : -1;

                                    Pair <IndexedWord, GrammaticalRelation> govReln = GetGovAndReln(extraGovIdx, 0, word, extraDeps[extraGovIdxStr], sortedTokens);
                                    IndexedWord         gov  = govReln.First();
                                    GrammaticalRelation reln = govReln.Second();
                                    TypedDependency     dep  = new TypedDependency(reln, gov, word);
                                    if (extraGovIdx != mainGovIdx)
                                    {
                                        dep.SetExtra();
                                    }
                                    deps.Add(dep);
                                }
                            }
                        }
                    }
                }
                lineNumberCounter++;
                SemanticGraph sg = new SemanticGraph(deps);

                comments.ForEach(null);
                return(sg);
            }
Esempio n. 21
0
 public virtual void SetRelation(GrammaticalRelation relation)
 {
     this.relation = relation;
 }
Esempio n. 22
0
        static ChineseGrammaticalRelations()
        {
            /*
             * The "predicate" grammatical relation.
             */
            // Fri Feb 20 15:42:54 2009 (pichuan)
            // I'm surprise this relation has patterns.
            // However it doesn't seem to match anything in CTB6.

            /*
             * public static final GrammaticalRelation PREDICATE =
             * new GrammaticalRelation(Language.Chinese, "pred", "predicate",
             * PredicateGRAnnotation.class, DEPENDENT, "IP",
             * new String[]{
             * " IP=target !> IP"
             * });
             * public static class PredicateGRAnnotation
             * extends GrammaticalRelationAnnotation { }
             */
            // Split the first rule to the second rule to avoid the duplication:
            // ccomp(前来-12, 投资-13)
            // conj(前来-12, 投资-13)
            //
            //      (IP
            //        (VP
            //          (VP (VV 前来))
            //          (VP
            //            (VCD (VV 投资) (VV 办厂)))
            //          (CC 和)
            //          (VP (VV 洽谈)
            //            (NP (NN 生意))))))
            // TODO: this following line has to be fixed.
            //       I think for now it just doesn't match anything.
            //"VP|NP|ADJP|PP|ADVP|UCP < ( __=target $+ (PU < 、) )",
            // Consider changing the rule ABOVE to these rules.
            //"ADVP < ( /^AD/=target $+ ((PU < 、) $+ /^AD/))",
            // This is for the 'conj's separated by commas.
            // For now this creates too much duplicates with 'ccomp'.
            // Need to look at more examples.
            // Original version of this did not have the outer layer of
            // the FRAG|INC|IP|VP.  This caused a bug where the basic
            // dependencies could have cycles.
            // This following rule is too general and collides with 'ccomp'.
            // Delete it for now.
            // TODO: come up with a new rule. Does this exist in Chinese?
            //"IP < (IP=target $+ ( VP !< VC))",

            /*
             * The "indirect object" grammatical relation.
             */
            // deleted by pichuan: no real matches

            /*
             * public static final GrammaticalRelation INDIRECT_OBJECT =
             * new GrammaticalRelation(Language.Chinese,
             * "iobj", "indirect object",
             * IndirectObjectGRAnnotation.class,  OBJECT, "VP",
             * new String[]{
             * " CP !> VP < ( VV $+ ( NP|DP|QP|CLP=target . NP|DP ) )"
             * });
             * public static class IndirectObjectGRAnnotation
             * extends GrammaticalRelationAnnotation { }
             */
            //        "  VP|IP <  ( VV|VC|VRD|VCD !$+  NP|QP|LCP ) > (IP   < IP|VP|VRD|VCD=target)   "
            //          "VP < (S=target < (VP !<, TO|VBG) !$-- NP)",
            // pichuan: this is difficult to recognize in Chinese.
            // remove the rules since it (always) collides with ccomp
            // TODO: these rules seem to always collide with ccomp.
            // Is this really desirable behavior?
            //"VP !> (/^VP/ < /^VC$/ ) < (IP=target < (VP < P))",
            //"ADJP < (IP=target <, (VP <, P))",
            //"VP < (IP=target < (NP $+ NP|ADJP))",
            //"VP < (/^VC/ $+ (VP=target < VC < NP))"

            /*
             * The "adjectival complement" grammatical relation.
             * Example:
             */
            // deleted by pichuan: no real matches

            /*
             * public static final GrammaticalRelation ADJECTIVAL_COMPLEMENT =
             * new GrammaticalRelation(Language.Chinese,
             * "acomp", "adjectival complement",
             * AdjectivalComplementGRAnnotation.class, COMPLEMENT, "VP", tregexCompiler,
             * new String[]{
             * "VP < (ADJP=target !$-- NP)"
             * });
             * public static class AdjectivalComplementGRAnnotation
             * extends GrammaticalRelationAnnotation { }
             */
            /* This rule actually matches nothing.
             * There's another tmod rule. This is removed for now.
             * (pichuan) Sun Mar  8 18:22:40 2009
             */
            /*
             * public static final GrammaticalRelation TEMPORAL_MODIFIER =
             * new GrammaticalRelation(Language.Chinese,
             * "tmod", "temporal modifier",
             * TemporalModifierGRAnnotation.class, MODIFIER, "VP|IP|ADJP", tregexCompiler,
             * new String[]{
             * " VC|VE ! >> VP|ADJP < NP=target < NT",
             * "VC|VE !>>IP <( NP=target < NT $++ VP !< VC|VE )"
             * });
             * public static class TemporalModifierGRAnnotation
             * extends GrammaticalRelationAnnotation { }
             */
            // pichuan: previously "tclaus"
            // TODO: we should figure out various ways to improve this pattern to
            // improve both its precision and recall
            //"DP < DT < QP=target"

            /*
             * The "possession modifier" grammatical relation.
             */
            // Fri Feb 20 15:40:13 2009 (pichuan)
            // I think this "poss" relation is just WRONG.
            // DEC is a complementizer or a nominalizer,
            // this rule probably originally want to capture "DEG".
            // But it seems like it's covered by "assm" (associative marker).

            /*
             * public static final GrammaticalRelation POSSESSION_MODIFIER =
             * new GrammaticalRelation(Language.Chinese,
             * "poss", "possession modifier",
             * PossessionModifierGRAnnotation.class, MODIFIER, "NP", tregexCompiler,
             * new String[]{
             * "NP < ( PN=target $+ DEC $+  NP )"
             * });
             * public static class PossessionModifierGRAnnotation
             * extends GrammaticalRelationAnnotation { }
             */
            /*
             * The "possessive marker" grammatical relation.
             */
            // Similar to the comments to "poss",
            // I think this relation is wrong and will not appear.

            /*
             * public static final GrammaticalRelation POSSESSIVE_MODIFIER =
             * new GrammaticalRelation(Language.Chinese, "possm", "possessive marker",
             * PossessiveModifierGRAnnotation.class,
             * MODIFIER, "NP", tregexCompiler,
             * new String[]{
             * "NP < ( PN $+ DEC=target ) "
             * });
             * public static class PossessiveModifierGRAnnotation
             * extends GrammaticalRelationAnnotation { }
             */
            // pichuan: previously "clmpd"
            //ADJECTIVAL_COMPLEMENT,
            //INDIRECT_OBJECT,
            //POSSESSION_MODIFIER,
            //POSSESSIVE_MODIFIER,
            //PREDICATE,
            // Cache frequently used views of the values list
            for (int i = 0; i < rawValues.Length; i++)
            {
                GrammaticalRelation gr = rawValues[i];
                synchronizedValues.Add(gr);
            }
            ValuesLock().Lock();
            try
            {
                foreach (GrammaticalRelation gr in Edu.Stanford.Nlp.Trees.International.Pennchinese.ChineseGrammaticalRelations.Values())
                {
                    shortNameToGRel[gr.GetShortName()] = gr;
                }
            }
            finally
            {
                ValuesLock().Unlock();
            }
        }
Esempio n. 23
0
 public static GrammaticalRelation ValueOf(string s)
 {
     return(GrammaticalRelation.ValueOf(s, Values(), ValuesLock()));
 }
Esempio n. 24
0
 public NotenizerRelation(GrammaticalRelation grammaticalRelation)
 {
     _longName = grammaticalRelation.getLongName();
     _shortName = grammaticalRelation.getShortName();
     _specific = grammaticalRelation.getSpecific();
 }
Esempio n. 25
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);
            }
        internal static bool CheckIfSatisfiesRelConstrains(SemanticGraph g, IndexedWord curr, IndexedWord child, IList <string> allCutOffRels, GrammaticalRelation rel)
        {
            string relName         = rel.GetShortName();
            string relSpecificName = rel.ToString();
            string relFullName     = rel.GetLongName();

            if (allCutOffRels != null)
            {
                foreach (string check in allCutOffRels)
                {
                    if (relName.Matches(check) || (relSpecificName != null && relSpecificName.Matches(check)) || (relFullName != null && relFullName.Matches(check)))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 27
0
        /// <summary>Fix some bizarre peculiarities with certain trees.</summary>
        /// <remarks>
        /// Fix some bizarre peculiarities with certain trees.
        /// So far, these include:
        /// <ul>
        /// <li>Sometimes there's a node from a word to itself. This seems wrong.</li>
        /// </ul>
        /// </remarks>
        /// <param name="tree">The tree to clean (in place!).</param>
        /// <returns>A list of extra edges, which are valid but were removed.</returns>
        public static IList <SemanticGraphEdge> CleanTree(SemanticGraph tree)
        {
            //    assert !isCyclic(tree);
            // Clean nodes
            IList <IndexedWord> toDelete = new List <IndexedWord>();

            foreach (IndexedWord vertex in tree.VertexSet())
            {
                // Clean punctuation
                if (vertex.Tag() == null)
                {
                    continue;
                }
                char tag = vertex.BackingLabel().Tag()[0];
                if (tag == '.' || tag == ',' || tag == '(' || tag == ')' || tag == ':')
                {
                    if (!tree.OutgoingEdgeIterator(vertex).MoveNext())
                    {
                        // This should really never happen, but it does.
                        toDelete.Add(vertex);
                    }
                }
            }
            toDelete.ForEach(null);
            // Clean edges
            IEnumerator <SemanticGraphEdge> iter = tree.EdgeIterable().GetEnumerator();
            IList <Triple <IndexedWord, IndexedWord, SemanticGraphEdge> > toAdd = new List <Triple <IndexedWord, IndexedWord, SemanticGraphEdge> >();

            toDelete.Clear();
            while (iter.MoveNext())
            {
                SemanticGraphEdge edge = iter.Current;
                if (edge.GetDependent().Index() == edge.GetGovernor().Index())
                {
                    // Clean up copy-edges
                    if (edge.GetDependent().IsCopy(edge.GetGovernor()))
                    {
                        foreach (SemanticGraphEdge toCopy in tree.OutgoingEdgeIterable(edge.GetDependent()))
                        {
                            toAdd.Add(Triple.MakeTriple(edge.GetGovernor(), toCopy.GetDependent(), toCopy));
                        }
                        toDelete.Add(edge.GetDependent());
                    }
                    if (edge.GetGovernor().IsCopy(edge.GetDependent()))
                    {
                        foreach (SemanticGraphEdge toCopy in tree.OutgoingEdgeIterable(edge.GetGovernor()))
                        {
                            toAdd.Add(Triple.MakeTriple(edge.GetDependent(), toCopy.GetDependent(), toCopy));
                        }
                        toDelete.Add(edge.GetGovernor());
                    }
                    // Clean self-edges
                    iter.Remove();
                }
                else
                {
                    if (edge.GetRelation().ToString().Equals("punct"))
                    {
                        // Clean punctuation (again)
                        if (!tree.OutgoingEdgeIterator(edge.GetDependent()).MoveNext())
                        {
                            // This should really never happen, but it does.
                            iter.Remove();
                        }
                    }
                }
            }
            // (add edges we wanted to add)
            toDelete.ForEach(null);
            foreach (Triple <IndexedWord, IndexedWord, SemanticGraphEdge> edge_1 in toAdd)
            {
                tree.AddEdge(edge_1.first, edge_1.second, edge_1.third.GetRelation(), edge_1.third.GetWeight(), edge_1.third.IsExtra());
            }
            // Handle extra edges.
            // Two cases:
            // (1) the extra edge is a subj/obj edge and the main edge is a conj:.*
            //     in this case, keep the extra
            // (2) otherwise, delete the extra
            IList <SemanticGraphEdge> extraEdges = new List <SemanticGraphEdge>();

            foreach (SemanticGraphEdge edge_2 in tree.EdgeIterable())
            {
                if (edge_2.IsExtra())
                {
                    IList <SemanticGraphEdge> incomingEdges = tree.IncomingEdgeList(edge_2.GetDependent());
                    SemanticGraphEdge         toKeep        = null;
                    foreach (SemanticGraphEdge candidate in incomingEdges)
                    {
                        if (toKeep == null)
                        {
                            toKeep = candidate;
                        }
                        else
                        {
                            if (toKeep.GetRelation().ToString().StartsWith("conj") && candidate.GetRelation().ToString().Matches(".subj.*|.obj.*"))
                            {
                                toKeep = candidate;
                            }
                            else
                            {
                                if (!candidate.IsExtra() && !(candidate.GetRelation().ToString().StartsWith("conj") && toKeep.GetRelation().ToString().Matches(".subj.*|.obj.*")))
                                {
                                    toKeep = candidate;
                                }
                            }
                        }
                    }
                    foreach (SemanticGraphEdge candidate_1 in incomingEdges)
                    {
                        if (candidate_1 != toKeep)
                        {
                            extraEdges.Add(candidate_1);
                        }
                    }
                }
            }
            extraEdges.ForEach(null);
            // Add apposition edges (simple coref)
            foreach (SemanticGraphEdge extraEdge in new List <SemanticGraphEdge>(extraEdges))
            {
                // note[gabor] prevent concurrent modification exception
                foreach (SemanticGraphEdge candidateAppos in tree.IncomingEdgeIterable(extraEdge.GetDependent()))
                {
                    if (candidateAppos.GetRelation().ToString().Equals("appos"))
                    {
                        extraEdges.Add(new SemanticGraphEdge(extraEdge.GetGovernor(), candidateAppos.GetGovernor(), extraEdge.GetRelation(), extraEdge.GetWeight(), extraEdge.IsExtra()));
                    }
                }
                foreach (SemanticGraphEdge candidateAppos_1 in tree.OutgoingEdgeIterable(extraEdge.GetDependent()))
                {
                    if (candidateAppos_1.GetRelation().ToString().Equals("appos"))
                    {
                        extraEdges.Add(new SemanticGraphEdge(extraEdge.GetGovernor(), candidateAppos_1.GetDependent(), extraEdge.GetRelation(), extraEdge.GetWeight(), extraEdge.IsExtra()));
                    }
                }
            }
            // Brute force ensure tree
            // Remove incoming edges from roots
            IList <SemanticGraphEdge> rootIncomingEdges = new List <SemanticGraphEdge>();

            foreach (IndexedWord root in tree.GetRoots())
            {
                foreach (SemanticGraphEdge incomingEdge in tree.IncomingEdgeIterable(root))
                {
                    rootIncomingEdges.Add(incomingEdge);
                }
            }
            rootIncomingEdges.ForEach(null);
            // Loop until it becomes a tree.
            bool changed = true;

            while (changed)
            {
                // I just want trees to be trees; is that so much to ask!?
                changed = false;
                IList <IndexedWord>       danglingNodes = new List <IndexedWord>();
                IList <SemanticGraphEdge> invalidEdges  = new List <SemanticGraphEdge>();
                foreach (IndexedWord vertex_1 in tree.VertexSet())
                {
                    // Collect statistics
                    IEnumerator <SemanticGraphEdge> incomingIter = tree.IncomingEdgeIterator(vertex_1);
                    bool hasIncoming         = incomingIter.MoveNext();
                    bool hasMultipleIncoming = false;
                    if (hasIncoming)
                    {
                        incomingIter.Current;
                        hasMultipleIncoming = incomingIter.MoveNext();
                    }
                    // Register actions
                    if (!hasIncoming && !tree.GetRoots().Contains(vertex_1))
                    {
                        danglingNodes.Add(vertex_1);
                    }
                    else
                    {
                        if (hasMultipleIncoming)
                        {
                            foreach (SemanticGraphEdge edge in new IterableIterator <SemanticGraphEdge>(incomingIter))
                            {
                                invalidEdges.Add(edge_2);
                            }
                        }
                    }
                }
                // Perform actions
                foreach (IndexedWord vertex_2 in danglingNodes)
                {
                    tree.RemoveVertex(vertex_2);
                    changed = true;
                }
                foreach (SemanticGraphEdge edge_3 in invalidEdges)
                {
                    tree.RemoveEdge(edge_3);
                    changed = true;
                }
            }
            // Edge case: remove duplicate dobj to "that."
            //            This is a common parse error.
            foreach (IndexedWord vertex_3 in tree.VertexSet())
            {
                SemanticGraphEdge thatEdge = null;
                int dobjCount = 0;
                foreach (SemanticGraphEdge edge in tree.OutgoingEdgeIterable(vertex_3))
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase("that", edge_2.GetDependent().Word()))
                    {
                        thatEdge = edge_2;
                    }
                    if ("dobj".Equals(edge_2.GetRelation().ToString()))
                    {
                        dobjCount += 1;
                    }
                }
                if (dobjCount > 1 && thatEdge != null)
                {
                    // Case: there are two dobj edges, one of which goes to the word "that"
                    // Action: rewrite the dobj edge to "that" to be a "mark" edge.
                    tree.RemoveEdge(thatEdge);
                    tree.AddEdge(thatEdge.GetGovernor(), thatEdge.GetDependent(), GrammaticalRelation.ValueOf(thatEdge.GetRelation().GetLanguage(), "mark"), thatEdge.GetWeight(), thatEdge.IsExtra());
                }
            }
            // Return
            System.Diagnostics.Debug.Assert(IsTree(tree));
            return(extraEdges);
        }
Esempio n. 28
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);
     }
 }
Esempio n. 29
0
        /// <summary>Given a string entry, converts it into a SsurgeonEdit object.</summary>
        public static SsurgeonEdit ParseEditLine(string editLine)
        {
            // Extract the operation name first
            string[] tuples1 = editLine.Split("\\s+", 2);
            if (tuples1.Length < 2)
            {
                throw new ArgumentException("Error in SsurgeonEdit.parseEditLine: invalid number of arguments");
            }
            string command = tuples1[0];

            string[] argsArray            = ParseArgs(tuples1[1]);
            Ssurgeon.SsurgeonArgs argsBox = new Ssurgeon.SsurgeonArgs();
            for (int argIndex = 0; argIndex < argsArray.Length; ++argIndex)
            {
                switch (argsArray[argIndex])
                {
                case GovNodenameArg:
                {
                    argsBox.govNodeName = argsArray[argIndex + 1];
                    argIndex           += 2;
                    break;
                }

                case DepNodenameArg:
                {
                    argsBox.dep = argsArray[argIndex + 1];
                    argIndex   += 2;
                    break;
                }

                case EdgeNameArg:
                {
                    argsBox.edge = argsArray[argIndex + 1];
                    argIndex    += 2;
                    break;
                }

                case RelnArg:
                {
                    argsBox.reln = argsArray[argIndex + 1];
                    argIndex    += 2;
                    break;
                }

                case NodenameArg:
                {
                    argsBox.node = argsArray[argIndex + 1];
                    argIndex    += 2;
                    break;
                }

                case NodeProtoArg:
                {
                    argsBox.nodeString = argsArray[argIndex + 1];
                    argIndex          += 2;
                    break;
                }

                case WeightArg:
                {
                    argsBox.weight = double.ValueOf(argsArray[argIndex + 1]);
                    argIndex      += 2;
                    break;
                }

                case NameArg:
                {
                    argsBox.name = argsArray[argIndex + 1];
                    argIndex    += 2;
                    break;
                }

                default:
                {
                    throw new ArgumentException("Parsing Ssurgeon args: unknown flag " + argsArray[argIndex]);
                }
                }
            }
            // Parse the arguments based upon the type of command to execute.
            // TODO: this logic really should be moved into the individual classes.  The string-->class
            // mappings should also be stored in more appropriate data structure.
            SsurgeonEdit retEdit;

            if (Sharpen.Runtime.EqualsIgnoreCase(command, AddDep.Label))
            {
                retEdit = AddDep.CreateEngAddDep(argsBox.govNodeName, argsBox.reln, argsBox.nodeString);
            }
            else
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(command, AddNode.Label))
                {
                    retEdit = AddNode.CreateAddNode(argsBox.nodeString, argsBox.name);
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(command, AddEdge.Label))
                    {
                        retEdit = AddEdge.CreateEngAddEdge(argsBox.govNodeName, argsBox.dep, argsBox.reln);
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(command, DeleteGraphFromNode.Label))
                        {
                            retEdit = new DeleteGraphFromNode(argsBox.node);
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(command, RemoveEdge.Label))
                            {
                                retEdit = new RemoveEdge(GrammaticalRelation.ValueOf(argsBox.reln), argsBox.govNodeName, argsBox.dep);
                            }
                            else
                            {
                                if (Sharpen.Runtime.EqualsIgnoreCase(command, RemoveNamedEdge.Label))
                                {
                                    retEdit = new RemoveNamedEdge(argsBox.edge, argsBox.govNodeName, argsBox.dep);
                                }
                                else
                                {
                                    if (Sharpen.Runtime.EqualsIgnoreCase(command, SetRoots.Label))
                                    {
                                        string[]       names    = tuples1[1].Split("\\s+");
                                        IList <string> newRoots = Arrays.AsList(names);
                                        retEdit = new SetRoots(newRoots);
                                    }
                                    else
                                    {
                                        if (Sharpen.Runtime.EqualsIgnoreCase(command, KillNonRootedNodes.Label))
                                        {
                                            retEdit = new KillNonRootedNodes();
                                        }
                                        else
                                        {
                                            if (Sharpen.Runtime.EqualsIgnoreCase(command, KillAllIncomingEdges.Label))
                                            {
                                                retEdit = new KillAllIncomingEdges(argsBox.node);
                                            }
                                            else
                                            {
                                                throw new ArgumentException("Error in SsurgeonEdit.parseEditLine: command '" + command + "' is not supported");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(retEdit);
        }
Esempio n. 30
0
 public AddDep(string govNodeName, GrammaticalRelation relation, IndexedWord newNodePrototype, double weight)
     : this(govNodeName, relation, newNodePrototype)
 {
     this.weight = weight;
 }
 /// <summary>
 /// The "prep" grammatical relation. Used to collapse prepositions.
 /// They will be turned into prep_word, where "word" is a preposition
 /// </summary>
 /// <param name="prepositionString">The preposition to make a GrammaticalRelation out of</param>
 /// <returns>A grammatical relation for this preposition</returns>
 public static GrammaticalRelation GetPrep(string prepositionString)
 {
     GrammaticalRelation result = Preps[prepositionString];
     if (result == null)
     {
         lock (Preps)
         {
             result = Preps[prepositionString];
             if (result == null)
             {
                 result = new GrammaticalRelation(GrammaticalRelation.Language.English, "prep", "prep_collapsed",
                     PrepositionalModifier, prepositionString);
                 Preps.TryAdd(prepositionString, result);
                 //threadSafeAddRelation(result);
             }
         }
     }
     return result;
 }
 /// <summary>
 /// The "prepc" grammatical relation. Used to collapse preposition complements.
 /// They will be turned into prep_word, where "word" is a preposition
 /// </summary>
 /// <param name="prepositionString">The preposition to make a GrammaticalRelation out of</param>
 /// <returns>A grammatical relation for this preposition</returns>
 public static GrammaticalRelation GetPrepC(string prepositionString)
 {
     GrammaticalRelation result = PrepsC[prepositionString];
     if (result == null)
     {
         lock (PrepsC)
         {
             result = PrepsC[prepositionString];
             if (result == null)
             {
                 result = new GrammaticalRelation(GrammaticalRelation.Language.English, "prepc",
                     "prepc_collapsed", GrammaticalRelation.Dependent, prepositionString);
                 PrepsC.TryAdd(prepositionString, result);
                 //threadSafeAddRelation(result);
             }
         }
     }
     return result;
 }
        //      collapseMultiwordPreps(list);
        private static void CollapsePrepAndPoss(ICollection <TypedDependency> list)
        {
            ICollection <TypedDependency> newTypedDeps = new List <TypedDependency>();
            // Construct a map from words to the set of typed
            // dependencies in which the word appears as governor.
            IDictionary <IndexedWord, ICollection <TypedDependency> > map = Generics.NewHashMap();

            foreach (TypedDependency typedDep in list)
            {
                if (!map.Contains(typedDep.Gov()))
                {
                    map[typedDep.Gov()] = Generics.NewHashSet <TypedDependency>();
                }
                map[typedDep.Gov()].Add(typedDep);
            }
            //log.info("here's the map: " + map);
            foreach (TypedDependency td1 in list)
            {
                if (td1.Reln() != GrammaticalRelation.Kill)
                {
                    IndexedWord td1Dep    = td1.Dep();
                    string      td1DepPOS = td1Dep.Tag();
                    // find all other typedDeps having our dep as gov
                    ICollection <TypedDependency> possibles = map[td1Dep];
                    if (possibles != null)
                    {
                        // look for the "second half"
                        foreach (TypedDependency td2 in possibles)
                        {
                            // TreeGraphNode td2Dep = td2.dep();
                            // String td2DepPOS = td2Dep.parent().value();
                            if (td1.Reln() == GrammaticalRelation.Dependent && td2.Reln() == GrammaticalRelation.Dependent && td1DepPOS.Equals("P"))
                            {
                                GrammaticalRelation td3reln = UniversalChineseGrammaticalRelations.ValueOf(td1Dep.Value());
                                if (td3reln == null)
                                {
                                    td3reln = GrammaticalRelation.ValueOf(Language.UniversalChinese, td1Dep.Value());
                                }
                                TypedDependency td3 = new TypedDependency(td3reln, td1.Gov(), td2.Dep());
                                //log.info("adding: " + td3);
                                newTypedDeps.Add(td3);
                                td1.SetReln(GrammaticalRelation.Kill);
                                // remember these are "used up"
                                td2.SetReln(GrammaticalRelation.Kill);
                            }
                        }
                        // remember these are "used up"
                        // Now we need to see if there any TDs that will be "orphaned"
                        // by this collapse.  Example: if we have:
                        //   dep(drew, on)
                        //   dep(on, book)
                        //   dep(on, right)
                        // the first two will be collapsed to on(drew, book), but then
                        // the third one will be orphaned, since its governor no
                        // longer appears.  So, change its governor to 'drew'.
                        if (td1.Reln().Equals(GrammaticalRelation.Kill))
                        {
                            foreach (TypedDependency td2_1 in possibles)
                            {
                                if (!td2_1.Reln().Equals(GrammaticalRelation.Kill))
                                {
                                    //log.info("td1 & td2: " + td1 + " & " + td2);
                                    td2_1.SetGov(td1.Gov());
                                }
                            }
                        }
                    }
                }
            }
            // now copy remaining unkilled TDs from here to new
            foreach (TypedDependency td in list)
            {
                if (!td.Reln().Equals(GrammaticalRelation.Kill))
                {
                    newTypedDeps.Add(td);
                }
            }
            list.Clear();
            // forget all (esp. killed) TDs
            Sharpen.Collections.AddAll(list, newTypedDeps);
        }
 /// <summary>
 /// The "conj" grammatical relation. Used to collapse conjunct relations.
 /// They will be turned into conj_word, where "word" is a conjunction.
 /// </summary>
 /// <param name="conjunctionString">The conjunction to make a GrammaticalRelation out of</param>
 /// <returns>A grammatical relation for this conjunction</returns>
 public static GrammaticalRelation GetConj(string conjunctionString)
 {
     GrammaticalRelation result = Conjs[conjunctionString];
     if (result == null)
     {
         lock (Conjs)
         {
             result = Conjs[conjunctionString];
             if (result == null)
             {
                 result = new GrammaticalRelation(GrammaticalRelation.Language.English, "conj", "conj_collapsed",
                     Conjunct, conjunctionString);
                 Conjs.TryAdd(conjunctionString, result);
                 //threadSafeAddRelation(result);
             }
         }
     }
     return result;
 }