Example #1
0
        public override bool Transform(Sentence sentence)
        {
            if (!sentence.phrases.Contains(this))
            {
                return(false);
            }

            bool success = false;
            KeyValuePair <string, string> nks = NeighborKinds(sentence);

            Phrase verbphr = FindConsituent <Verb>();
            string verb    = (verbphr == null ? "" : verbphr.Text);

            if (nks.Key == "ADVP")
            {
                sentence.AbsorbPrevious(this);
                success = true;
            }
            else if (nks.Value == "VBN")
            { // is considered
                sentence.AbsorbNext(this);
                success = true;
            }
            else if (nks.Value == "ADVP")
            {
                sentence.AbsorbNext(this);
                success = true;
            }
            else if (nks.Value == "NP")
            {
                sentence.AbsorbNext(this);
                success = true;
            }
            else if (nks.Value == "ADJP" && Verbs.IsToBe(verb))
            {
                sentence.AbsorbNext(this);
                success = true;
            }

            else if (nks.Value == "PP")
            {
                sentence.AbsorbNext(this);
                success = true;
            }

            else if (nks.Key == "TO")
            {
                List <Phrase> us = new List <Phrase>();
                us.Add(sentence.PhraseBefore(this));
                us.Add(this);
                sentence.Combine(us, new PrepositionalPhrase());
                success = true;
            }

            else if (nks.Key == "WHNP" || nks.Key == "WRB")
            {
                string trial = sentence.PhraseBefore(this).ToString() + ";" + ToString() + ";SBARQ";
                if (!triedBefore.ContainsKey(trial))
                {
                    List <Phrase> phrases = new List <Phrase>();
                    phrases.Add(sentence.PhraseBefore(this));
                    phrases.Add(this);
                    sentence.Combine(phrases, new SimpleQuestion());
                    triedBefore.Add(trial, true);
                    success = true;
                }
            }

            else if (nks.Key == "NP" || nks.Key == "EX")
            {
                string trial = sentence.PhraseBefore(this).ToString() + ";" + ToString() + ";S";
                if (!triedBefore.ContainsKey(trial))
                {
                    List <Phrase> phrases = new List <Phrase>();
                    phrases.Add(sentence.PhraseBefore(this));
                    phrases.Add(this);
                    sentence.Combine(phrases, new SimpleDeclarativePhrase());
                    triedBefore.Add(trial, true);
                    success = true;
                }
            }

            return(success);
        }
        public IParsedPhrase Handle(IEnumerable <KeyValuePair <string, string> > tokens)
        {
            Sentence sentence = new Sentence(tokens);

            return(sentence.Parse());
        }
        public override bool Transform(Sentence sentence)
        {
            if (!sentence.phrases.Contains(this))
            {
                return(false);
            }

            bool success = false;

            bool lastFound = true;

            while (lastFound)
            {
                KeyValuePair <string, string> nks = NeighborKinds(sentence);
                if (nks.Value == "." && constituents[constituents.Count - 1].Text.Length == 1)
                {
                    sentence.AbsorbNext(this);   // eat the period
                }
                else if (nks.Key == "" && nks.Value == "NP")
                {
                    sentence.MergeNext(this);
                }
                else if (nks.Key == "NP" && nks.Value == "NP")
                {
                    sentence.MergeNext(this);
                }
                else if (nks.Key == "NP" && (nks.Value == "" || nks.Value == "." || nks.Value == "!"))
                {
                    sentence.MergePrevious(this);
                }
                else
                {
                    lastFound = false;
                }

                if (lastFound == true)
                {
                    success = true;
                }
            }

            KeyValuePair <string, string> neighbors = NeighborKinds(sentence);

            if (neighbors.Key == "DT" || neighbors.Key == "PRP$")
            {
                sentence.AbsorbPrevious(this);
                neighbors = NeighborKinds(sentence);
                success   = true;
            }

            if (neighbors.Key == "ADJP")
            {
                List <Phrase> phrases = new List <Phrase>();
                phrases.Add(sentence.PhraseBefore(this));
                phrases.Add(this);
                sentence.Combine(phrases, new NounPhrase());
                neighbors = NeighborKinds(sentence);
                success   = true;
            }

            if (neighbors.Value == "PP")
            {
                Phrase after = sentence.PhraseAfter(this);
                if (after.Constituents.Count > 1 && after.Constituents[0].Text.ToLower() == "of")
                {
                    sentence.AbsorbNext(this);
                    return(true);
                }
            }

            return(success);
        }