Beispiel #1
0
        public string Parse(string Sentence, bool answer)
        {
            bool question = false;
            string[] words = new string[0];
            int i = 0;
            int wordPos = 0;
            StringReader letterchecker;
            bool removed = false;

            if (Sentence.IndexOf("?") != -1)
            {
                question = true;
            }

            Sentence = RemovePunctuation(Sentence);
            words = Sentence.Split(' ');

            /*If there were multiple spaces in a row, there will be empty elements in words[]. This loop removes any such elements.
            for (i = 0; i < words.Length; i++)
            {
                if (removed)
                {
                    i--;
                }

                removed = false;

                if (words[i] == "")
                {
                    string[] sanitised = new string[words.Length-1];
                    Array.Copy(words.Take(i-1).ToArray(), sanitised, 0);
                    Array.Copy(words.Skip(i+1).ToArray(), sanitised, i);

                    words = sanitised;
                }
            }
             */

            for (i = 0; i < words.Length; i++)
            {
                letterchecker = new StringReader(words[i]);
                char[] firstletter = new char[1];
                letterchecker.Read(firstletter, 0, 1);
                bool upper = false;

                if (Char.IsUpper(firstletter[0]))
                {
                    upper = true;
                }

                words[i] = words[i].ToLower();

                wordPos = WordPos(words[i]);

                if ( wordPos != -1 )
                {
                    if (i < (words.Length-1) )
                    {
                        Vocabulary[wordPos].AddNewNext( words[i + 1].ToLower() );
                    }

                    if (i == 0)
                    {
                        Vocabulary[wordPos].start = true;
                    }

                    if (question)
                    {
                        Vocabulary[wordPos].question = true;
                    }

                    if (answer)
                    {
                        Vocabulary[wordPos].answer = true;
                    }

                    if (upper && i > 0)
                    {
                        Vocabulary[wordPos].upper = true;
                    }

                    if (i == (words.Length-1) )
                    {
                        Vocabulary[wordPos].end = true;
                    }
                }
                else
                {
                    Word newWord;

                    if (i < (words.Length - 1))
                    {
                        newWord = new Word(words[i], words[i + 1].ToLower());
                    }
                    else
                    {
                        newWord = new Word(words[i]);
                    }

                    if (i == 0)
                    {
                        newWord.start = true;
                    }

                    if (question)
                    {
                        newWord.question = true;
                    }

                    if (answer)
                    {
                        newWord.answer = true;
                    }

                    if (upper && i > 0)
                    {
                        newWord.upper = true;
                    }

                    if (i == (words.Length - 1))
                    {
                        newWord.end = true;
                    }

                    Vocabulary.Add(newWord);
                }
            }

            return (Answer(words, question));
        }
Beispiel #2
0
        public string Parse(string Sentence, bool answer)
        {
            bool question = false;
            List<string> words = new List<string>();
            int i = 0;
            int wordPos = 0;
            StringReader letterchecker;

            if (Sentence.IndexOf("?") != -1)
            {
                question = true;
            }

            Sentence = RemovePunctuation(Sentence);
            words = Sentence.Split(' ').ToList<string>();
            words.Remove("");//If the input had multiple spaces in a row, then words will contain empty elements. This removes them.

            for (i = 0; i < words.Count; i++)
            {
                letterchecker = new StringReader(words[i]);
                char[] firstletter = new char[1];
                letterchecker.Read(firstletter, 0, 1);
                bool upper = false;

                if (Char.IsUpper(firstletter[0]))
                {
                    upper = true;
                }

                words[i] = words[i].ToLower();

                wordPos = WordPos(words[i]);

                if ( wordPos != -1 )
                {
                    if (i < (words.Count-1) )
                    {
                        Vocabulary[wordPos].AddNewNext( words[i + 1].ToLower() );
                    }

                    if (i == 0)
                    {
                        Vocabulary[wordPos].start = true;
                    }

                    if (question)
                    {
                        Vocabulary[wordPos].question = true;
                    }

                    if (answer)
                    {
                        Vocabulary[wordPos].answer = true;
                    }

                    if (upper && i > 0)
                    {
                        Vocabulary[wordPos].upper = true;
                    }

                    if (i == (words.Count-1) )
                    {
                        Vocabulary[wordPos].end = true;
                    }
                }
                else
                {
                    Word newWord;

                    if (i < (words.Count - 1))
                    {
                        newWord = new Word(words[i], words[i + 1].ToLower());
                    }
                    else
                    {
                        newWord = new Word(words[i]);
                    }

                    if (i == 0)
                    {
                        newWord.start = true;
                    }

                    if (question)
                    {
                        newWord.question = true;
                    }

                    if (answer)
                    {
                        newWord.answer = true;
                    }

                    if (upper && i > 0)
                    {
                        newWord.upper = true;
                    }

                    if (i == (words.Count - 1))
                    {
                        newWord.end = true;
                    }

                    Vocabulary.Add(newWord);
                }
            }

            return (Answer(words, question));
        }