Пример #1
0
		public void BreakSentence (string input) {
			//Breaks down an inbound sentence.
			string [] stringArray;
			List <string> validStrings = new List <string> (); //First string is words, second string is punctuation.
			Word nextWord = null;
			Word priorWord = null;
			bool isFirst = true;
			string sentenceReport = "";
			Dictionary <Vocabulary.SpeechPart, Grammar> grammarTarget = new Dictionary<Vocabulary.SpeechPart, Grammar> ();
			Grammar nextGrammar = null;

			int wordCount = 1;
			DiscoveredChar lastCharacter = null;

			stringArray = input.Split (null); //splits to whitespace characters

			foreach (string testString in stringArray) { //Test each string to ensure it will process correctly, and kick out strings that won't.
				bool foundValid = false;

				foreach (char c in testString) {
					if (Char.IsLetter (c)) {
						foundValid = true;
					}
				}

				if (testString.Length == 0) { //Zero length strings get booted
					foundValid = false;
				}

				if (foundValid) {
					validStrings.Add (testString);
				}
			}

			foreach (string s in validStrings) {
				string nextString = "";
				string punctuation = "";

				nextString = SplitPunctuation (s, out punctuation);
				//Console.WriteLine ("Recovered " + punctuation + " after " + nextString);

				nextString = ReduceToBase (nextString);

				if (words.ContainsKey (nextString)) { //If the word is already in the dictionary, just increment the frequency.
					nextWord = words[nextString];
					nextWord.Frequency++;
				}
				else { //If the word isn't in the dictionary yet, add it in.
					nextWord = new Word (nextString, isFirst, vocabulary);
					words.Add (nextString, nextWord);
					if (!crossReference.ContainsKey (nextWord.Part)) {
						crossReference.Add (nextWord.Part, new List <Word> ());
					}
					crossReference [nextWord.Part].Add (nextWord);
				}

				if (isFirst) { //Add the new first word into the first words dictionary
					if (firstWords.ContainsKey (nextWord)) {
						firstWords[nextWord] = firstWords[nextWord]++;
					}
					else {
						firstWords.Add (nextWord, 1);
					}
					grammarTarget = grammars; //Begin at the top of the grammar map
				}
				else {
					//Console.WriteLine (nextWord.Name + " is not first (pos: " + nextWord.Part);
					if (priorWord != null) {
						priorWord.AddFollower (nextWord);
					}
					if ((nextWord.Part == Vocabulary.SpeechPart.None)) { //Searching for characters in the source text 
						if (VerifyProperNoun (nextWord.Name)) {
							nextWord.Part |= Vocabulary.SpeechPart.ProperNoun;
							lastCharacter = new DiscoveredChar (nextWord.Name);
							discoveredChars.Add (nextWord.Name, lastCharacter);
						}
						if (!crossReference.ContainsKey (nextWord.Part)) {
							crossReference.Add (nextWord.Part, new List <Word> ());
						}
						crossReference [nextWord.Part].Add (nextWord);
					}
				}
					
				sentenceReport = (sentenceReport + "[" + nextWord.Name + "-->" + nextWord.Part);
				if (grammarTarget.ContainsKey (nextWord.Part)) { //If there's an existing grammar map, just continue along it
					sentenceReport = (sentenceReport + ", existing]\n");
					nextGrammar = grammarTarget [nextWord.Part];
				}
				else { //Otherwise, start a new grammar map
					sentenceReport = (sentenceReport + "-->new branch]\n");
					nextGrammar = new Grammar (nextWord, wordCount);
					grammarTarget.Add (nextWord.Part, nextGrammar);
				}
				nextGrammar.AddPunctation (punctuation); //Record the punctuation following this word
				//Console.WriteLine ("Recorded " + punctuation + " after " + nextGrammar.Part);
				grammarTarget = nextGrammar.Followers;

				wordCount++; //Tracking this point on the grammar chain

				isFirst = false;
			}
			//Console.WriteLine (sentenceReport);
		}
Пример #2
0
        public void BreakSentence(string input)
        {
            //Breaks down an inbound sentence.
            string []     stringArray;
            List <string> validStrings   = new List <string> ();           //First string is words, second string is punctuation.
            Word          nextWord       = null;
            Word          priorWord      = null;
            bool          isFirst        = true;
            string        sentenceReport = "";
            Dictionary <Vocabulary.SpeechPart, Grammar> grammarTarget = new Dictionary <Vocabulary.SpeechPart, Grammar> ();
            Grammar nextGrammar = null;

            int            wordCount     = 1;
            DiscoveredChar lastCharacter = null;

            stringArray = input.Split(null);              //splits to whitespace characters

            foreach (string testString in stringArray)    //Test each string to ensure it will process correctly, and kick out strings that won't.
            {
                bool foundValid = false;

                foreach (char c in testString)
                {
                    if (Char.IsLetter(c))
                    {
                        foundValid = true;
                    }
                }

                if (testString.Length == 0)                   //Zero length strings get booted
                {
                    foundValid = false;
                }

                if (foundValid)
                {
                    validStrings.Add(testString);
                }
            }

            foreach (string s in validStrings)
            {
                string nextString  = "";
                string punctuation = "";

                nextString = SplitPunctuation(s, out punctuation);
                //Console.WriteLine ("Recovered " + punctuation + " after " + nextString);

                nextString = ReduceToBase(nextString);

                if (words.ContainsKey(nextString))                    //If the word is already in the dictionary, just increment the frequency.
                {
                    nextWord = words[nextString];
                    nextWord.Frequency++;
                }
                else                   //If the word isn't in the dictionary yet, add it in.
                {
                    nextWord = new Word(nextString, isFirst, vocabulary);
                    words.Add(nextString, nextWord);
                    if (!crossReference.ContainsKey(nextWord.Part))
                    {
                        crossReference.Add(nextWord.Part, new List <Word> ());
                    }
                    crossReference [nextWord.Part].Add(nextWord);
                }

                if (isFirst)                   //Add the new first word into the first words dictionary
                {
                    if (firstWords.ContainsKey(nextWord))
                    {
                        firstWords[nextWord] = firstWords[nextWord]++;
                    }
                    else
                    {
                        firstWords.Add(nextWord, 1);
                    }
                    grammarTarget = grammars;                     //Begin at the top of the grammar map
                }
                else
                {
                    //Console.WriteLine (nextWord.Name + " is not first (pos: " + nextWord.Part);
                    if (priorWord != null)
                    {
                        priorWord.AddFollower(nextWord);
                    }
                    if ((nextWord.Part == Vocabulary.SpeechPart.None))                       //Searching for characters in the source text
                    {
                        if (VerifyProperNoun(nextWord.Name))
                        {
                            nextWord.Part |= Vocabulary.SpeechPart.ProperNoun;
                            lastCharacter  = new DiscoveredChar(nextWord.Name);
                            discoveredChars.Add(nextWord.Name, lastCharacter);
                        }
                        if (!crossReference.ContainsKey(nextWord.Part))
                        {
                            crossReference.Add(nextWord.Part, new List <Word> ());
                        }
                        crossReference [nextWord.Part].Add(nextWord);
                    }
                }

                sentenceReport = (sentenceReport + "[" + nextWord.Name + "-->" + nextWord.Part);
                if (grammarTarget.ContainsKey(nextWord.Part))                    //If there's an existing grammar map, just continue along it
                {
                    sentenceReport = (sentenceReport + ", existing]\n");
                    nextGrammar    = grammarTarget [nextWord.Part];
                }
                else                   //Otherwise, start a new grammar map
                {
                    sentenceReport = (sentenceReport + "-->new branch]\n");
                    nextGrammar    = new Grammar(nextWord, wordCount);
                    grammarTarget.Add(nextWord.Part, nextGrammar);
                }
                nextGrammar.AddPunctation(punctuation);                  //Record the punctuation following this word
                //Console.WriteLine ("Recorded " + punctuation + " after " + nextGrammar.Part);
                grammarTarget = nextGrammar.Followers;

                wordCount++;                 //Tracking this point on the grammar chain

                isFirst = false;
            }
            //Console.WriteLine (sentenceReport);
        }