Exemplo n.º 1
0
        public void Init()
        {
            var filename = "input.txt";
            var wc = new WebClient();
            var url = ConfigurationManager.AppSettings["InputUrl"];
            wc.DownloadFile(url, filename);
            var lines = File.ReadAllLines(filename);

            var currentUser = "";
            var phrasesDict = new Dictionary<string, List<string>>();
            foreach (var line in lines)
            {
                if (line == "[photo_100]") continue;
                var m = r.Match(line);
                if (m.Success)
                {
                    currentUser = m.Groups[1].Value;
                }
                else
                {
                    List<string> phrases;
                    if (phrasesDict.TryGetValue(currentUser, out phrases))
                    {
                        phrases.Add(line);
                    }
                    else
                    {
                        phrasesDict[currentUser] = new List<string> { line };
                    }
                }
            }

            foreach (var corpus in phrasesDict)
            {
                if (corpus.Value.Count() > 1000)
                {
                    var chain = new MarkovChain();
                    chains[corpus.Key] = chain;
                    foreach (var phrase in corpus.Value)
                    {
                        var cleanPhrase = phrase
                            .Replace(")", " ")
                            .Replace("(", " ")
                            .Replace(".", " ");

                        var words = cleanPhrase.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                        if (words.Length >= 3)
                        {
                            for (var i = 2; i < words.Length; i++)
                            {
                                chain.Add(words[i - 2], words[i - 1], words[i]);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Init()
        {
            var filename = "input.txt";
            var wc       = new WebClient();
            var url      = ConfigurationManager.AppSettings["InputUrl"];

            wc.DownloadFile(url, filename);
            var lines = File.ReadAllLines(filename);

            var currentUser = "";
            var phrasesDict = new Dictionary <string, List <string> >();

            foreach (var line in lines)
            {
                if (line == "[photo_100]")
                {
                    continue;
                }
                var m = r.Match(line);
                if (m.Success)
                {
                    currentUser = m.Groups[1].Value;
                }
                else
                {
                    List <string> phrases;
                    if (phrasesDict.TryGetValue(currentUser, out phrases))
                    {
                        phrases.Add(line);
                    }
                    else
                    {
                        phrasesDict[currentUser] = new List <string> {
                            line
                        };
                    }
                }
            }

            foreach (var corpus in phrasesDict)
            {
                if (corpus.Value.Count() > 1000)
                {
                    var chain = new MarkovChain();
                    chains[corpus.Key] = chain;
                    foreach (var phrase in corpus.Value)
                    {
                        var cleanPhrase = phrase
                                          .Replace(")", " ")
                                          .Replace("(", " ")
                                          .Replace(".", " ");

                        var words = cleanPhrase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (words.Length >= 3)
                        {
                            for (var i = 2; i < words.Length; i++)
                            {
                                chain.Add(words[i - 2], words[i - 1], words[i]);
                            }
                        }
                    }
                }
            }
        }