コード例 #1
0
        /// <summary>
        /// Convert from a String representation of a GrammaticalRelation to a
        /// GrammaticalRelation.
        /// </summary>
        /// <remarks>
        /// 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.
        /// </remarks>
        /// <param name="s">The String representation of a GrammaticalRelation</param>
        /// <returns>The grammatical relation represented by this String</returns>
        public static Edu.Stanford.Nlp.Trees.GrammaticalRelation ValueOf(Language language, string s)
        {
            Edu.Stanford.Nlp.Trees.GrammaticalRelation reln;
            lock (stringsToRelations)
            {
                reln = (stringsToRelations[language] != null ? ValueOf(s, stringsToRelations[language]) : null);
            }
            if (reln == null)
            {
                // TODO this breaks the hierarchical structure of the classes,
                //      but it makes English relations that much likelier to work.
                reln = UniversalEnglishGrammaticalRelations.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;
                char   separator          = language == Language.UniversalEnglish ? ':' : '_';
                int    underscorePosition = s.IndexOf(separator);
                if (underscorePosition > 0)
                {
                    name     = Sharpen.Runtime.Substring(s, 0, underscorePosition);
                    specific = Sharpen.Runtime.Substring(s, underscorePosition + 1);
                }
                else
                {
                    name     = s;
                    specific = null;
                }
                reln = new Edu.Stanford.Nlp.Trees.GrammaticalRelation(language, name, null, null, specific);
            }
            return(reln);
        }
コード例 #2
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);
            }
            }
        }