Exemplo n.º 1
0
        /// <summary>
        /// Determines whether the given PhraseNode overlaps with the given word.
        /// The two overlap if the last word of the phrase is the same as the given word,
        /// or if the second-to-last word of the phrase is the same as the given word and the last word of the phrase is ignorable.
        /// </summary>
        /// <param name="name">The phrase to check for overlap.</param>
        /// <param name="word">The word to check for overlap with.</param>
        /// <returns>True if the phrase and word overlap, False otherwise.</returns>
        private bool HasOverlap(PhraseNode name, string word)
        {
            if (name == null || name.Size() == 0 || string.IsNullOrEmpty(word))
            {
                return(false);
            }

            bool hasOverlap = false;

            if (string.Equals(name.LastWord().Text, word, StringComparison.InvariantCultureIgnoreCase))
            {
                //last word of name is same as given word
                hasOverlap = true;
            }
            else if (name.Size() > 1)
            {
                if (string.Equals(name[name.Size() - 2].Text, word, StringComparison.InvariantCultureIgnoreCase) &&
                    PosData.IsIgnorableHeadWord(name.LastWord().Text))
                {
                    //second-to-last word of name is same as given word, and the last word of name is ignorable
                    hasOverlap = true;
                }
            }

            return(hasOverlap);
        }
Exemplo n.º 2
0
        public void TestConstructor_EnumerableWordNodesEmpty() {
            var words = new WordNode[] {};
            PhraseNode pn = new PhraseNode(words, Location.Name, true);

            Assert.AreEqual(words.Length, pn.Size());
            for(int i = 0; i < pn.Size(); i++) {
                Assert.AreEqual(words[i], pn[i]);
            }
            Assert.AreEqual(Location.Name, pn.Location);
        }
Exemplo n.º 3
0
        public void TestConstructor_EnumerableStrings() {
            var words = new string[] {"Eat", "More", "chicken"};
            var pn = new PhraseNode(words);

            Assert.AreEqual(3, pn.Size());
            for(int i = 0; i < pn.Size(); i++) {
                Assert.AreEqual(words[i], pn[i].Text);
            }
            Assert.AreEqual(Location.None, pn.Location);
        }
Exemplo n.º 4
0
        public void TestConstructor_EnumerableStringsEmpty() {
            var words = new string[] {};
            var pn = new PhraseNode(words);

            Assert.AreEqual(words.Length, pn.Size());
            for(int i = 0; i < pn.Size(); i++) {
                Assert.AreEqual(words[i], pn[i].Text);
            }
            Assert.AreEqual(Location.None, pn.Location);
        }
        public void TestConstructor_EnumerableStringsEmpty()
        {
            var words = new string[] {};
            var pn    = new PhraseNode(words);

            Assert.AreEqual(words.Length, pn.Size());
            for (int i = 0; i < pn.Size(); i++)
            {
                Assert.AreEqual(words[i], pn[i].Text);
            }
            Assert.AreEqual(Location.None, pn.Location);
        }
        public void TestConstructor_EnumerableWordNodesEmpty()
        {
            var        words = new WordNode[] {};
            PhraseNode pn    = new PhraseNode(words, Location.Name, true);

            Assert.AreEqual(words.Length, pn.Size());
            for (int i = 0; i < pn.Size(); i++)
            {
                Assert.AreEqual(words[i], pn[i]);
            }
            Assert.AreEqual(Location.Name, pn.Location);
        }
        public void TestConstructor_EnumerableStrings()
        {
            var words = new string[] { "Eat", "More", "chicken" };
            var pn    = new PhraseNode(words);

            Assert.AreEqual(3, pn.Size());
            for (int i = 0; i < pn.Size(); i++)
            {
                Assert.AreEqual(words[i], pn[i].Text);
            }
            Assert.AreEqual(Location.None, pn.Location);
        }
Exemplo n.º 8
0
        public void TestConstructor_EnumerableWordNodes() {
            var wn1 = new WordNode("Eat", PartOfSpeechTag.Verb);
            var wn2 = new WordNode("More", PartOfSpeechTag.NounModifier);
            var wn3 = new WordNode("Chicken", PartOfSpeechTag.Noun);

            var words = new WordNode[] {wn1, wn2, wn3};
            PhraseNode pn = new PhraseNode(words, Location.Name, true);

            Assert.AreEqual(words.Length, pn.Size());
            for(int i = 0; i < pn.Size(); i++) {
                Assert.AreEqual(words[i], pn[i]);
            }
            Assert.AreEqual(Location.Name, pn.Location);
        }
 public static bool PhraseNodesAreEqual(PhraseNode pn1, PhraseNode pn2)
 {
     if (pn1.Size() != pn2.Size())
     {
         return(false);
     }
     for (int i = 0; i < pn1.Size(); i++)
     {
         if (!WordNodeTests.WordNodesAreEqual(pn1[i], pn2[i]))
         {
             return(false);
         }
     }
     return(true);
 }
        public void TestConstructor_EnumerableStringsNull()
        {
            var pn = new PhraseNode(null);

            Assert.AreEqual(0, pn.Size());
            Assert.AreEqual(Location.None, pn.Location);
        }
        public void TestConstructor_EnumerableWordNodes()
        {
            var wn1 = new WordNode("Eat", PartOfSpeechTag.Verb);
            var wn2 = new WordNode("More", PartOfSpeechTag.NounModifier);
            var wn3 = new WordNode("Chicken", PartOfSpeechTag.Noun);

            var        words = new WordNode[] { wn1, wn2, wn3 };
            PhraseNode pn    = new PhraseNode(words, Location.Name, true);

            Assert.AreEqual(words.Length, pn.Size());
            for (int i = 0; i < pn.Size(); i++)
            {
                Assert.AreEqual(words[i], pn[i]);
            }
            Assert.AreEqual(Location.Name, pn.Location);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Finds the index of the first preposition within the given PhraseNode, starting from the word indicated by startIndex.
 /// </summary>
 /// <param name="parsedName">The PhraseNode to search.</param>
 /// <param name="startIndex">The index of the word to start searching for prepositions from.</param>
 /// <returns>The index of the first preposition in the PhraseNode after startIndex, inclusively.</returns>
 private int FindFirstPreposition(PhraseNode parsedName, int startIndex)
 {
     for (int i = startIndex; i < parsedName.Size(); i++)
     {
         if (parsedName[i].Tag == PartOfSpeechTag.Preposition)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemplo n.º 13
0
        public void TestCreateThemeFromPhrases_FirstNull()
        {
            MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod");
            PhraseNode            pn1 = new PhraseNode(new string[] { "hello", "World" });

            mdn.CreateThemeFromPhrases(pn1, null);
            Assert.AreSame(pn1, mdn.Theme);
            Assert.AreEqual(2, pn1.Size());
            Assert.AreEqual("hello", pn1[0].Text);
            Assert.AreEqual("World", pn1[1].Text);
        }
Exemplo n.º 14
0
        public void TestCreateThemeFromPhrases_SecondNull()
        {
            MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod");
            PhraseNode            pn2 = new PhraseNode(new string[] { "cowboy", "watermelon" });

            mdn.CreateThemeFromPhrases(null, pn2);
            Assert.AreSame(pn2, mdn.Theme);
            Assert.AreEqual(2, pn2.Size());
            Assert.AreEqual("cowboy", pn2[0].Text);
            Assert.AreEqual("watermelon", pn2[1].Text);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Determines whether the given phrase indicates an event handler method.
 /// </summary>
 /// <param name="parsedName">The PhraseNode to test.</param>
 /// <returns>True if the phrase indicates an event handler method, False otherwise.</returns>
 protected bool IsEventHandler(PhraseNode parsedName)
 {
     if (parsedName == null || parsedName.Size() == 0)
     {
         return(false);
     }
     else
     {
         return(IsNonBaseVerb(parsedName.LastWord().Text) &&
                parsedName[0].Text.ToLower() != "get" &&
                parsedName[0].Text.ToLower() != "set");
     }
 }
Exemplo n.º 16
0
 /// <summary>
 /// Determines whether the specified word in the given phrase is an ignorable verb. If so, it tags it appropriately.
 /// </summary>
 /// <param name="parsedName">The PhraseNode containing the word to check.</param>
 /// <param name="wordIndex">The index of the desired word within the PhraseNode.</param>
 /// <returns>wordIndex+1 if the word was an ignorable verb; wordIndex if it was not.</returns>
 private int CheckForIgnorableVerb(PhraseNode parsedName, int wordIndex)
 {
     if (wordIndex < parsedName.Size() - 1 && //make sure last word in name is verb
         (PosData.IsIgnorableVerb(parsedName[wordIndex].Text) &&
          (PositionalFrequencies.GetVerbProbability(parsedName[wordIndex + 1].Text) > PositionalFrequencies.GetNounProbability(parsedName[wordIndex + 1].Text)) ||
          PosData.IsModalVerb(parsedName[wordIndex].Text))
         )
     {
         parsedName[wordIndex].Tag = PartOfSpeechTag.VerbIgnorable;
         wordIndex++;
     }
     return(wordIndex);
 }
Exemplo n.º 17
0
        public void TestCreateThemeFromPhrases()
        {
            MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod");
            PhraseNode            pn1 = new PhraseNode(new string[] { "hello", "World" });
            PhraseNode            pn2 = new PhraseNode(new string[] { "cowboy", "watermelon" });

            mdn.CreateThemeFromPhrases(pn1, pn2);
            Assert.AreSame(pn1, mdn.Theme);
            Assert.AreEqual(4, pn1.Size());
            Assert.AreEqual("hello", pn1[0].Text);
            Assert.AreEqual("World", pn1[1].Text);
            Assert.AreEqual("cowboy", pn1[2].Text);
            Assert.AreEqual("watermelon", pn1[3].Text);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Returns the index of <paramref name="word"/> within <paramref name="phraseNode"/>, or -1 if it's not found.
        /// </summary>
        private int FindWordInPhraseNode(PhraseNode phraseNode, string word)
        {
            if (phraseNode == null)
            {
                throw new ArgumentNullException("phraseNode");
            }
            if (word == null)
            {
                throw new ArgumentNullException("word");
            }

            int index = -1;

            for (int i = 0; i < phraseNode.Size(); i++)
            {
                if (string.Compare(phraseNode[i].Text, word, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    index = i;
                    break;
                }
            }
            return(index);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Returns a PhraseNode containing the noun phrase words from the given name, starting from startIndex.
        /// All noun phrase words prior to the first encountered preposition are included.
        /// </summary>
        /// <param name="parsedName">The PhraseNode to get the noun phrase from.</param>
        /// <param name="startIndex">The index of the word to start from.</param>
        private PhraseNode GetNounPhrase(PhraseNode parsedName, int startIndex)
        {
            PhraseNode phrase = parsedName.GetNewEmpty();

            for (int i = startIndex; i < parsedName.Size(); i++)
            {
                PartOfSpeechTag tag = parsedName[i].Tag;
                if (tag == PartOfSpeechTag.Noun ||
                    tag == PartOfSpeechTag.NounModifier ||
                    tag == PartOfSpeechTag.Determiner ||
                    tag == PartOfSpeechTag.Pronoun ||
                    tag == PartOfSpeechTag.NounIgnorable ||
                    tag == PartOfSpeechTag.Digit ||
                    tag == PartOfSpeechTag.Preamble)
                {
                    phrase.Add(parsedName[i]);
                }
                else if (tag == PartOfSpeechTag.Preposition)
                {
                    break;
                }
            }
            return(phrase);
        }
Exemplo n.º 20
0
 public void TestCreateThemeFromPhrases() {
     MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod");
     PhraseNode pn1 = new PhraseNode(new string[] { "hello", "World" });
     PhraseNode pn2 = new PhraseNode(new string[] { "cowboy", "watermelon" });
     mdn.CreateThemeFromPhrases(pn1, pn2);
     Assert.AreSame(pn1, mdn.Theme);
     Assert.AreEqual(4, pn1.Size());
     Assert.AreEqual("hello", pn1[0].Text);
     Assert.AreEqual("World", pn1[1].Text);
     Assert.AreEqual("cowboy", pn1[2].Text);
     Assert.AreEqual("watermelon", pn1[3].Text);
 }
Exemplo n.º 21
0
        /// <summary>
        /// Assigns part-of-speech tags to the word nodes in the given phrase, assuming it is a noun phrase.
        /// Only the words between startIndex and stopIndex, inclusive, are tagged.
        /// </summary>
        /// <param name="phrase">The noun phrase to tag.</param>
        /// <param name="startIndex">The index of the first word to tag.</param>
        /// <param name="stopIndex">The index of the last word to tag.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">startIndex or stopIndex are not valid indices, or stopIndex is less than startIndex</exception>
        public override void TagNounPhrase(PhraseNode phrase, int startIndex, int stopIndex)
        {
            if (phrase == null || phrase.Size() <= 0)
            {
                return;
            }
            if (startIndex < 0 || startIndex >= phrase.Size())
            {
                throw new ArgumentOutOfRangeException("startIndex", startIndex, string.Format("The given value is not a valid index of the PhraseNode. It must be between 0 and {0}.", phrase.Size() - 1));
            }
            if (stopIndex < startIndex || stopIndex >= phrase.Size())
            {
                throw new ArgumentOutOfRangeException("stopIndex", stopIndex, string.Format("The given value must be a valid index of the PhraseNode, and must be larger than the given startIndex value of {0}", startIndex));
            }

            //start from the end of the phrase
            int currentWord = stopIndex;

            //skip any digits at the end
            while (phrase[currentWord].Tag == PartOfSpeechTag.Digit)
            {
                currentWord--;
            }

            //tag the last word
            if (currentWord >= startIndex)
            {
                if (pos.IsDeterminer(phrase[currentWord].Text))
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.Determiner;
                }
                else if (pos.IsPronoun(phrase[currentWord].Text))
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.Pronoun;
                }
                else if (pos.IsIgnorableHeadWord(phrase[currentWord].Text))
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.NounIgnorable;
                }
                else
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.Noun;
                }
                currentWord--;
            }

            //tag the rest of the words
            while (currentWord >= startIndex)
            {
                if (pos.IsDeterminer(phrase[currentWord].Text))
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.Determiner;
                }
                else if (pos.IsPronoun(phrase[currentWord].Text))
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.Pronoun;
                }
                else if (phrase[currentWord].Tag != PartOfSpeechTag.Digit)
                {
                    phrase[currentWord].Tag = PartOfSpeechTag.NounModifier;
                }

                currentWord--;
            }
        }
Exemplo n.º 22
0
 /// <summary>
 /// Assigns part-of-speech tags to the word nodes in the given phrase, assuming it is a noun phrase.
 /// </summary>
 /// <param name="phrase">The noun phrase to tag.</param>
 public override void TagNounPhrase(PhraseNode phrase)
 {
     TagNounPhrase(phrase, 0, phrase.Size() - 1);
 }
Exemplo n.º 23
0
 public void TestCreateThemeFromPhrases_SecondNull() {
     MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod");
     PhraseNode pn2 = new PhraseNode(new string[] { "cowboy", "watermelon" });
     mdn.CreateThemeFromPhrases(null, pn2);
     Assert.AreSame(pn2, mdn.Theme);
     Assert.AreEqual(2, pn2.Size());
     Assert.AreEqual("cowboy", pn2[0].Text);
     Assert.AreEqual("watermelon", pn2[1].Text);
 }
Exemplo n.º 24
0
        /// <summary>
        /// Assigns part-of-speech tags to the words in the given MethodDeclarationNode's name, assuming that it follows a base verb pattern.
        /// This assumes that the node has already had its name split and preamble stripped.
        /// </summary>
        /// <param name="node">The MethodDeclarationNode to tag.</param>
        protected void ParseBaseVerbName(MethodDeclarationNode node)
        {
            //this assumes that the node has already had its name split and preamble stripped

            PhraseNode parsedName = node.ParsedName;

            //TODO: from Emily, what if it starts with an adverb??

            //if 1 word, assume verb
            if (parsedName.Size() == 1)
            {
                if (PosData.IsIgnorableVerb(parsedName[0].Text))
                {
                    parsedName[0].Tag = PartOfSpeechTag.VerbIgnorable;
                }
                else
                {
                    parsedName[0].Tag = PartOfSpeechTag.Verb;
                }
                return;
            }

            int currentWord = 0;

            currentWord = CheckForIgnorableVerb(parsedName, currentWord);

            //check for a verb modifier
            if (currentWord < parsedName.Size())
            {
                if (PosData.IsAdverb(parsedName[currentWord].Text) &&
                    parsedName[currentWord].Text.EndsWith("ly") &&
                    !PosData.IsDeterminer(parsedName[currentWord].Text) &&
                    !PosData.IsPronoun(parsedName[currentWord].Text) &&
                    !PosData.IsPreposition(parsedName[currentWord].Text))
                {
                    if (currentWord + 1 < parsedName.Size() && PosData.IsPotentialVerb(parsedName[currentWord + 1].Text))
                    {
                        parsedName[currentWord].Tag = PartOfSpeechTag.VerbModifier;
                    }
                }
            }

            if (PosData.IsIgnorableVerb(parsedName[currentWord].Text))
            {
                parsedName[currentWord].Tag = PartOfSpeechTag.VerbIgnorable;
            }
            else
            {
                parsedName[currentWord].Tag = PartOfSpeechTag.Verb;
            }

            currentWord++;

            //check for verb particle
            if (currentWord < parsedName.Size())
            {
                if (PosData.IsVerbParticle(parsedName[currentWord - 1].Text, parsedName[currentWord].Text))
                {
                    parsedName[currentWord].Tag = PartOfSpeechTag.VerbParticle;
                }
            }

            //rest of words should be objects or prepositions
            if (currentWord < parsedName.Size())
            {
                int prep = FindFirstPreposition(parsedName, currentWord);
                if (prep == -1)
                {
                    PosTagger.TagNounPhrase(parsedName, currentWord, parsedName.Size() - 1);
                }
                else
                {
                    //found a preposition, could be VX?PY?(f?)
                    bool noX = false;
                    bool noY = false;
                    if (currentWord == prep)
                    {
                        noX = true;
                    }
                    else
                    {
                        PosTagger.TagNounPhrase(parsedName, currentWord, prep - 1);
                    }
                    currentWord = prep + 1;
                    if (currentWord >= parsedName.Size())
                    {
                        noY = true;
                    }
                    else
                    {
                        PosTagger.TagNounPhrase(parsedName, currentWord, parsedName.Size() - 1);
                    }
                }
            }
        }
Exemplo n.º 25
0
 public static bool PhraseNodesAreEqual(PhraseNode pn1, PhraseNode pn2) {
     if(pn1.Size() != pn2.Size()) {
         return false;
     }
     for(int i = 0; i < pn1.Size(); i++) {
         if(!WordNodeTests.WordNodesAreEqual(pn1[i], pn2[i])) {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 26
0
 public void TestCreateThemeFromPhrases_FirstNull() {
     MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod");
     PhraseNode pn1 = new PhraseNode(new string[] { "hello", "World" });
     mdn.CreateThemeFromPhrases(pn1, null);
     Assert.AreSame(pn1, mdn.Theme);
     Assert.AreEqual(2, pn1.Size());
     Assert.AreEqual("hello", pn1[0].Text);
     Assert.AreEqual("World", pn1[1].Text);
 }
Exemplo n.º 27
0
 public void TestConstructor_EnumerableStringsNull() {
     var pn = new PhraseNode(null);
     Assert.AreEqual(0, pn.Size());
     Assert.AreEqual(Location.None, pn.Location);
 }