Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("[START]\tTraining started");
            NaiveBayesClassifier sentimentClassifier = new NaiveBayesClassifier();
            sentimentClassifier.Train(new TestDataParser("Resources/SentimentTrainingData.txt"));
            Console.WriteLine("[END]\tTraining");
            Console.WriteLine();
            Console.WriteLine("[START]\tFriendlist parsing");
            FriendListParser parser = new FriendListParser("Resources/friendships.reviews.txt");
            parser.Parse();
            File.WriteAllText("graph.csv", parser.Community.ToGraph());
            Console.WriteLine("[END]\t\tFriendlist parsing");
            Console.WriteLine();
            Console.WriteLine("[START]\t\tIdentifying communities");
            CommunityIdentifier communityIdentifier = new CommunityIdentifier(parser.Community);
            DateTime start = DateTime.Now;
            List<Community> subCommunities = communityIdentifier.Identify();
            Console.WriteLine("[END]\t\tIdentifying communities");
            Console.WriteLine();
            Console.WriteLine("[START]\t\tAnalysing communities");
            CommunityAnalyzer communityAnalyzer = new CommunityAnalyzer(sentimentClassifier, subCommunities);
            string filename = "result.csv";
            communityAnalyzer.Analyze(filename);
            Console.WriteLine("[END]\t\tAnalysing communities: Results written to {0}", filename);

            Console.WriteLine("Identied communities: " + subCommunities.Count);
            Console.WriteLine("\nSub communities:");
            foreach (var community in subCommunities)
            {
                Console.WriteLine(community.Size);
                community.Dispose();
            }
            communityAnalyzer.Dispose();
            communityIdentifier.Dispose();
            sentimentClassifier.Dispose();

            Console.WriteLine(DateTime.Now.Subtract(start).ToString("g"));
            Console.WriteLine("Finished everything. Press to exit");
            Console.ReadLine();

        }
        public List<Community> Identify(double lastDiff = double.MinValue)
        {
            List<Community> communities = new List<Community>();
            if (_rootCommunity.Users.Count < 2)
                return communities;

            Console.WriteLine("Root community size: {0}", _rootCommunity.Users.Count);

            _eigenvector = CalculateEigenvector();
            //Console.WriteLine("Eigenvector:\n\t" + string.Join("\n\t", _eigenvector));

            double largestDiff = lastDiff;
            int nodeIndexBeforeDiff = -1;
            for (int i = 1; i < _eigenvector.Count; i++)
            {
                double diff = Math.Abs(_eigenvector[i].Value - _eigenvector[i - 1].Value);
                if (diff > largestDiff)
                {
                    nodeIndexBeforeDiff = i;
                    largestDiff = diff;
                }
            }

            Console.WriteLine("Largest diff: " + largestDiff);

            if (nodeIndexBeforeDiff > -1) // We did discover subcommunities
            {
                Community community1 = new Community();
                for (int i = 0; i < nodeIndexBeforeDiff; i++)
                {
                    User user = _rootCommunity.Users[_eigenvector[i].Key];
                    community1.AddUser(user);
                }
                Community community2 = new Community();
                for (int i = nodeIndexBeforeDiff; i < _eigenvector.Count; i++)
                {
                    User user = _rootCommunity.Users[_eigenvector[i].Key];
                    community2.AddUser(user);
                }

                Console.WriteLine("Community1: " + community1.Size);
                Console.WriteLine("Community2: " + community2.Size);
                Console.WriteLine();

                if (community1.Size > 1 && community2.Size > 1)
                {
                    lastDiff = largestDiff*0.6;
                    CommunityIdentifier identifier1 = new CommunityIdentifier(community1);
                    List<Community> communities1 = identifier1.Identify(lastDiff);
                    if (communities1.Count > 0)
                        communities.AddRange(communities1);
                    else
                        communities.Add(community1);

                    CommunityIdentifier identifier2 = new CommunityIdentifier(community2);
                    List<Community> communities2 = identifier2.Identify(lastDiff);
                    if (communities2.Count > 0)
                        communities.AddRange(communities2);
                    else
                        communities.Add(community2);
                }
            }
            Console.WriteLine("Communities: {0}\n", communities.Count);
            return communities;
        }