Пример #1
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);
        }