Beispiel #1
0
        public void TestSerialization() {
            var headRulesOriginal =
                new HeadRules(new StreamReader(Tests.OpenFile("/opennlp/tools/parser/en_head_rules")));

            using (var data = new MemoryStream()) {
                headRulesOriginal.Serialize(new StreamWriter(data));

                data.Seek(0, SeekOrigin.Begin);

                var headRulesRecreated = new HeadRules(new StreamReader(data));

                Assert.AreEqual(headRulesOriginal, headRulesRecreated);
            }

        }
Beispiel #2
0
        /// <summary>
        /// Returns the head constituent for the specified constituents of the specified type.
        /// </summary>
        /// <param name="constituents">The constituents which make up a constituent of the specified type.</param>
        /// <param name="type">The type of a constituent which is made up of the specified constituents.</param>
        /// <returns>The constituent which is the head.</returns>
        public override Parse GetHead(Parse[] constituents, string type)
        {
            if (constituents[0].Type == AbstractBottomUpParser.TOK_NODE)
            {
                return(null);
            }


            if (type == "NP" || type == "NX")
            {
                string[] tags1 = { "NN", "NNP", "NNPS", "NNS", "NX", "JJR", "POS" };
                for (var ci = constituents.Length - 1; ci >= 0; ci--)
                {
                    for (var ti = tags1.Length - 1; ti >= 0; ti--)
                    {
                        if (constituents[ci].Type == tags1[ti])
                        {
                            return(constituents[ci].Head);
                        }
                    }
                }
                for (var ci = 0; ci < constituents.Length; ci++)
                {
                    if (constituents[ci].Type == "NP")
                    {
                        return(constituents[ci].Head);
                    }
                }
                string[] tags2 = { "$", "ADJP", "PRN" };
                for (var ci = constituents.Length - 1; ci >= 0; ci--)
                {
                    for (var ti = tags2.Length - 1; ti >= 0; ti--)
                    {
                        if (constituents[ci].Type == tags2[ti])
                        {
                            return(constituents[ci].Head);
                        }
                    }
                }
                string[] tags3 = { "JJ", "JJS", "RB", "QP" };
                for (var ci = constituents.Length - 1; ci >= 0; ci--)
                {
                    for (var ti = tags3.Length - 1; ti >= 0; ti--)
                    {
                        if (constituents[ci].Type == tags3[ti])
                        {
                            return(constituents[ci].Head);
                        }
                    }
                }
                return(constituents[constituents.Length - 1].Head);
            }

            if (HeadRules.ContainsKey(type))
            {
                var hr = HeadRules[type];

                var tags = hr.Tags;
                var cl   = constituents.Length;
                var tl   = tags.Length;
                if (hr.LeftToRight)
                {
                    for (var ti = 0; ti < tl; ti++)
                    {
                        for (var ci = 0; ci < cl; ci++)
                        {
                            if (constituents[ci].Type == tags[ti])
                            {
                                return(constituents[ci].Head);
                            }
                        }
                    }
                    return(constituents[0].Head);
                }
                for (var ti = 0; ti < tl; ti++)
                {
                    for (var ci = cl - 1; ci >= 0; ci--)
                    {
                        if (constituents[ci].Type == tags[ti])
                        {
                            return(constituents[ci].Head);
                        }
                    }
                }
                return(constituents[cl - 1].Head);
            }
            return(constituents[constituents.Length - 1].Head);
        }