예제 #1
0
        public string GetAnalysisStringFromWordList(List <string> words)
        {
            List <string> memory = new List <string>();

            for (int index = 0; index < this.LearningLength; ++index)
            {
                memory.Add(words[words.Count - this.LearningLength + index]);
            }
            return(Corpus.ConvertMemoryToString(memory));
        }
예제 #2
0
        public string GenerateSentence(Action <string> CompleteAction = null)
        {
            List <string> words = new List <string>();

            for (int index = 0; index < this.LearningLength; ++index)
            {
                words.Add((string)null);
            }
            int num1 = 0;

            while (num1 < 1000)
            {
                List <PotentialEntry> potentialEntries = this.GetPotentialEntries(this.GetAnalysisStringFromWordList(words));
                double num2 = Utils.random.NextDouble();
                double num3 = 0.0;
                for (int index = 0; index < potentialEntries.Count; ++index)
                {
                    num3 += potentialEntries[index].weighting;
                    if (num2 < num3)
                    {
                        words.Add(potentialEntries[index].word);
                        break;
                    }
                }
                ++num1;
                if (Corpus.WordEndsWithSentenceEnder(words[words.Count - 1]))
                {
                    string str1 = "";
                    for (int index = 0; index < words.Count; ++index)
                    {
                        str1 = str1 + words[index] + " ";
                    }
                    string str2 = str1.Trim();
                    if (CompleteAction != null)
                    {
                        CompleteAction(str2);
                    }
                    return(str2);
                }
            }
            if (CompleteAction != null)
            {
                CompleteAction((string)null);
            }
            return((string)null);
        }
예제 #3
0
 public static Corpus GenerateCorpusFromFolder(string name, int maxFilesToRead = -1, Action <float, string> percentCompleteUpdated = null, Action <Corpus> Complete = null)
 {
     try
     {
         Corpus        corpus        = new Corpus();
         DirectoryInfo directoryInfo = new DirectoryInfo(name + "/");
         string[]      extensions    = new string[2] {
             ".txt", ".cs"
         };
         FileInfo[] array = ((IEnumerable <FileInfo>)directoryInfo.GetFiles()).Where <FileInfo>((Func <FileInfo, bool>)(f => ((IEnumerable <string>)extensions).Contains <string>(f.Extension.ToLower()))).ToArray <FileInfo>();
         for (int index = 0; index < array.Length && (maxFilesToRead == -1 || index < maxFilesToRead); ++index)
         {
             float num = (float)index / (float)array.Length;
             num *= 100f;
             if (maxFilesToRead != -1)
             {
                 num = (float)index / (float)maxFilesToRead;
             }
             string input = File.ReadAllText(name + "/" + array[index].Name);
             if (percentCompleteUpdated != null)
             {
                 percentCompleteUpdated(num, num.ToString("00.00") + "% | Reading " + array[index].Name + "...");
             }
             corpus.LearnText(input);
         }
         if (Complete != null)
         {
             Complete(corpus);
         }
         return(corpus);
     }
     catch (Exception ex)
     {
         if (Complete != null)
         {
             Complete((Corpus)null);
         }
         return((Corpus)null);
     }
 }
예제 #4
0
        public void LearnText(string input)
        {
            List <string> memory = new List <string>();

            for (int index = 0; index < this.LearningLength; ++index)
            {
                memory.Add((string)null);
            }
            foreach (string str1 in input.Split(Utils.WhitespaceDelim, StringSplitOptions.RemoveEmptyEntries))
            {
                string key = Corpus.ConvertMemoryToString(memory);
                Dictionary <string, int> dictionary1;
                if (this.data.ContainsKey(key))
                {
                    dictionary1 = this.data[key];
                }
                else
                {
                    dictionary1 = new Dictionary <string, int>();
                    try
                    {
                        this.data.Add(key, dictionary1);
                    }
                    catch (OutOfMemoryException ex)
                    {
                        break;
                    }
                }
                string str2 = str1;
                if (dictionary1.ContainsKey(str2))
                {
                    Dictionary <string, int> dictionary2;
                    string index;
                    (dictionary2 = dictionary1)[index = str2] = dictionary2[index] + 1;
                }
                else
                {
                    dictionary1.Add(str2, 1);
                }
                try
                {
                    this.data[key] = dictionary1;
                }
                catch (OutOfMemoryException ex)
                {
                    break;
                }
                if (Corpus.WordEndsWithSentenceEnder(str2))
                {
                    for (int index = 0; index < this.LearningLength; ++index)
                    {
                        memory[index] = (string)null;
                    }
                }
                else
                {
                    memory.Add(str2);
                    memory.RemoveAt(0);
                }
            }
        }