Ejemplo n.º 1
0
        static async Task MainAsync()
        {
            Console.Write("Input Twitter user name: ");
            var userName = Console.ReadLine();

            if (string.IsNullOrEmpty(userName))
            {
                throw new InvalidOperationException();
            }

            List <ClassifiedItem> items = await GetItems(userName);

            Console.WriteLine("Input Categories(split comma)");
            Console.Write(": ");
            var categoriesText = Console.ReadLine();
            var categories     = categoriesText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (categories.Length == 0)
            {
                throw new InvalidOperationException();
            }

            Console.WriteLine("Start training...");
            var classifier = new NaiveBayesClassifier();

            Train(items, categories, classifier);

            Console.WriteLine("Classifing...");
            foreach (var item in items)
            {
                item.Category = classifier.Classify(item.Token.Nouns);
            }

            Console.WriteLine("Classified.");

            foreach (var g in items.GroupBy(i => i.Category))
            {
                Console.WriteLine($"{g.Key}: {g.Count()}");
            }

            SaveData(items, $"{userName}.json");
        }
Ejemplo n.º 2
0
        private static void Train(List <ClassifiedItem> items, string[] categories, NaiveBayesClassifier classifier)
        {
            var question = "Which category(-2:Quit, -1:Skip)? " +
                           string.Join(", ", categories.Select((c, i) => $"{i}:{c}"));
            var random       = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
            var trainingData = new List <Tuple <string, IEnumerable <string> > >();

            while (true)
            {
                var item = items[random.Next(items.Count)];
                Console.WriteLine($"[Tweet] {item.Tweet.Text}");
                Console.WriteLine($"[Token] {string.Join(", ", item.Token.Nouns)}");
                Console.WriteLine(question);
                Console.Write(": ");
                var idxText = Console.ReadLine();
                int idx     = 0;

                if (int.TryParse(idxText, out idx) && idx >= -2 && idx < categories.Length)
                {
                    if (idx == -2)
                    {
                        break;
                    }
                    if (idx == -1)
                    {
                        continue;
                    }

                    trainingData.Add(new Tuple <string, IEnumerable <string> >(categories[idx], item.Token.Nouns));
                }
                else
                {
                    Console.WriteLine("Input is invalid.");
                }
            }

            classifier.Train(trainingData);
        }