Beispiel #1
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));
        }
        /// <summary>
        /// Convert from a string representation of a GrammaticalRelation to a
        /// GrammaticalRelation.  Where possible, you should avoid using this
        /// method and simply work with true GrammaticalRelations rather than
        /// string representations.  Correct behavior of this method depends
        /// on the underlying data structure resources used being kept in sync
        /// with the ToString() and Equals() methods.  However, there is really
        /// no choice but to use this method when storing GrammaticalRelations
        /// to text files and then reading them back in, so this method is not deprecated.
        /// </summary>
        /// <param name="s">The string representation of a GrammaticalRelation</param>
        /// <returns>The grammatical relation represented by this String</returns>
        public static GrammaticalRelation ValueOf(Language language, string s)
        {
            GrammaticalRelation reln = (StringsToRelations[language] != null
                ? ValueOf(s, StringsToRelations[language].Values)
                : null);

            if (reln == null)
            {
                // TODO this breaks the hierarchical structure of the classes,
                //      but it makes English relations that much likelier to work.
                reln = EnglishGrammaticalRelations.ValueOf(s);
            }
            if (reln == null)
            {
                // the block below fails when 'specific' includes underscores.
                // this is possible on weird web text, which generates relations such as prep______

                /*
                 * string[] names = s.split("_");
                 * string specific = names.length > 1? names[1] : null;
                 * reln = new GrammaticalRelation(language, names[0], null, null, null, specific);
                 */
                string name;
                string specific;
                int    underscorePosition = s.IndexOf('_');
                if (underscorePosition > 0)
                {
                    name     = s.Substring(0, underscorePosition);
                    specific = s.Substring(underscorePosition + 1);
                }
                else
                {
                    name     = s;
                    specific = null;
                }
                reln = new GrammaticalRelation(language, name, null, null, specific);
            }
            return(reln);
        }
Beispiel #3
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));
        }
Beispiel #4
0
        /// <summary>
        /// When deserializing a GrammaticalRelation, it needs to be matched
        /// up with the existing singleton relation of the same type.
        /// </summary>
        /// <remarks>
        /// When deserializing a GrammaticalRelation, it needs to be matched
        /// up with the existing singleton relation of the same type.
        /// TODO: there are a bunch of things wrong with this.  For one
        /// thing, it's crazy slow, since it goes through all the existing
        /// relations in an array.  For another, it would be cleaner to have
        /// subclasses for the English and Chinese relations
        /// </remarks>
        /// <exception cref="Java.IO.ObjectStreamException"/>
        protected internal virtual object ReadResolve()
        {
            switch (language)
            {
            case Language.Any:
            {
                if (shortName.Equals(Governor.shortName))
                {
                    return(Governor);
                }
                else
                {
                    if (shortName.Equals(Dependent.shortName))
                    {
                        return(Dependent);
                    }
                    else
                    {
                        if (shortName.Equals(Root.shortName))
                        {
                            return(Root);
                        }
                        else
                        {
                            if (shortName.Equals(Kill.shortName))
                            {
                                return(Kill);
                            }
                            else
                            {
                                throw new Exception("Unknown general relation " + shortName);
                            }
                        }
                    }
                }
                goto case Language.English;
            }

            case Language.English:
            {
                Edu.Stanford.Nlp.Trees.GrammaticalRelation rel = EnglishGrammaticalRelations.ValueOf(ToString());
                if (rel == null)
                {
                    switch (shortName)
                    {
                    case "conj":
                    {
                        return(EnglishGrammaticalRelations.GetConj(specific));
                    }

                    case "prep":
                    {
                        return(EnglishGrammaticalRelations.GetPrep(specific));
                    }

                    case "prepc":
                    {
                        return(EnglishGrammaticalRelations.GetPrepC(specific));
                    }

                    default:
                    {
                        // TODO: we need to figure out what to do with relations
                        // which were serialized and then deprecated.  Perhaps there
                        // is a good way to make them singletons
                        return(this);
                    }
                    }
                }
                else
                {
                    //throw new RuntimeException("Unknown English relation " + this);
                    return(rel);
                }
                goto case Language.Chinese;
            }

            case Language.Chinese:
            {
                Edu.Stanford.Nlp.Trees.GrammaticalRelation rel = ChineseGrammaticalRelations.ValueOf(ToString());
                if (rel == null)
                {
                    // TODO: we need to figure out what to do with relations
                    // which were serialized and then deprecated.  Perhaps there
                    // is a good way to make them singletons
                    return(this);
                }
                //throw new RuntimeException("Unknown Chinese relation " + this);
                return(rel);
            }

            case Language.UniversalEnglish:
            {
                Edu.Stanford.Nlp.Trees.GrammaticalRelation rel_1 = UniversalEnglishGrammaticalRelations.ValueOf(ToString());
                if (rel_1 == null)
                {
                    switch (shortName)
                    {
                    case "conj":
                    {
                        return(UniversalEnglishGrammaticalRelations.GetConj(specific));
                    }

                    case "nmod":
                    {
                        return(UniversalEnglishGrammaticalRelations.GetNmod(specific));
                    }

                    case "acl":
                    {
                        return(UniversalEnglishGrammaticalRelations.GetAcl(specific));
                    }

                    case "advcl":
                    {
                        return(UniversalEnglishGrammaticalRelations.GetAdvcl(specific));
                    }

                    default:
                    {
                        // TODO: we need to figure out what to do with relations
                        // which were serialized and then deprecated.  Perhaps there
                        // is a good way to make them singletons
                        return(this);
                    }
                    }
                }
                else
                {
                    //throw new RuntimeException("Unknown English relation " + this);
                    return(rel_1);
                }
                goto default;
            }

            default:
            {
                throw new Exception("Unknown language " + language);
            }
            }
        }
        /// <summary>Read in typed dependencies.</summary>
        /// <remarks>
        /// Read in typed dependencies. Warning created typed dependencies are not
        /// backed by any sort of a tree structure.
        /// </remarks>
        /// <param name="filename"/>
        /// <exception cref="System.IO.IOException"/>
        protected internal static IList <ICollection <TypedDependency> > ReadDeps(string filename)
        {
            LineNumberReader breader = new LineNumberReader(new FileReader(filename));
            IList <ICollection <TypedDependency> > readDeps = new List <ICollection <TypedDependency> >();
            ICollection <TypedDependency>          deps     = new List <TypedDependency>();

            for (string line = breader.ReadLine(); line != null; line = breader.ReadLine())
            {
                if (line.Equals("null(-0,-0)") || line.Equals("null(-1,-1)"))
                {
                    readDeps.Add(deps);
                    deps = new List <TypedDependency>();
                    continue;
                }
                // relex parse error
                try
                {
                    if (line.Equals(string.Empty))
                    {
                        if (deps.Count != 0)
                        {
                            //System.out.println(deps);
                            readDeps.Add(deps);
                            deps = new List <TypedDependency>();
                        }
                        continue;
                    }
                    int    firstParen        = line.IndexOf("(");
                    int    commaSpace        = line.IndexOf(", ");
                    string depName           = Sharpen.Runtime.Substring(line, 0, firstParen);
                    string govName           = Sharpen.Runtime.Substring(line, firstParen + 1, commaSpace);
                    string childName         = Sharpen.Runtime.Substring(line, commaSpace + 2, line.Length - 1);
                    GrammaticalRelation grel = GrammaticalRelation.ValueOf(depName);
                    if (depName.StartsWith("prep_"))
                    {
                        string prep = Sharpen.Runtime.Substring(depName, 5);
                        grel = EnglishGrammaticalRelations.GetPrep(prep);
                    }
                    if (depName.StartsWith("prepc_"))
                    {
                        string prepc = Sharpen.Runtime.Substring(depName, 6);
                        grel = EnglishGrammaticalRelations.GetPrepC(prepc);
                    }
                    if (depName.StartsWith("conj_"))
                    {
                        string conj = Sharpen.Runtime.Substring(depName, 5);
                        grel = EnglishGrammaticalRelations.GetConj(conj);
                    }
                    if (grel == null)
                    {
                        throw new Exception("Unknown grammatical relation '" + depName + "'");
                    }
                    //Word govWord = new Word(govName.substring(0, govDash));
                    IndexedWord govWord = new IndexedWord();
                    govWord.SetValue(NormalizeNumbers(govName));
                    govWord.SetWord(govWord.Value());
                    //Word childWord = new Word(childName.substring(0, childDash));
                    IndexedWord childWord = new IndexedWord();
                    childWord.SetValue(NormalizeNumbers(childName));
                    childWord.SetWord(childWord.Value());
                    TypedDependency dep = new DependencyScoring.TypedDependencyStringEquality(grel, govWord, childWord);
                    deps.Add(dep);
                }
                catch (Exception e)
                {
                    breader.Close();
                    throw new Exception("Error on line " + breader.GetLineNumber() + ":\n\n" + e);
                }
            }
            if (deps.Count != 0)
            {
                readDeps.Add(deps);
            }
            //log.info("last: "+readDeps.get(readDeps.size()-1));
            breader.Close();
            return(readDeps);
        }