Exemple #1
0
        public static void WriteCadidateTopics(CandidateTopics candTopic, string WindowTimeSlotID)
        {
            //StringBuilder strBuilder = new StringBuilder();

            //float[] scores;// = candTopic.CandidateKeywords.Select(x => x.Ranking).ToArray<float>();

            ////scores = ScaleData(candTopic.CandidateKeywords.Select(x => x.Ranking).ToArray<float>(), 1, 100);
            //scores = candTopic.CandidateKeywords.Select(x => x.Ranking).ToArray<float>();


            //for (int i = 0; i < candTopic.CandidateKeywords.Count; i++)
            //{
            //    for (int j = 1; j < (int)Math.Round(scores[i], 0); j++)
            //    {
            //        strBuilder.Append(candTopic.CandidateKeywords[i].Word + " ");
            //    }
            //}

            string fileName     = "Candidate_topics_" + WindowTimeSlotID;
            var    fileStream   = new FileStream(fileName + ".csv", FileMode.Create, FileAccess.Write);
            var    streamWriter = new StreamWriter(fileStream, Encoding.UTF8);

            streamWriter.Write(GenerateText(candTopic));

            streamWriter.Close();
        }
        private Evaluation MatchResultWithGroundTruth(ActualTopics actualTopics, CandidateTopics candTopics, int topK)
        {
            Evaluation eval            = new Evaluation(topK);
            int        topicHitCount   = 0;
            int        keywordHitCount = 0;

            for (int top = 0; top < topK; top++)
            {
                actualTopics.Match(candTopics.CandidateKeywords[top].Word, ref topicHitCount, ref keywordHitCount);
                eval.KeywordPercisionAtK[top] = keywordHitCount / (float)(top + 1); // top+1 because index starts from 0 in array
                eval.KeywordRecallAtK[top]    = keywordHitCount / (float)actualTopics.TotalKeywords;
                eval.TopicPercisionAtK[top]   = topicHitCount / (float)(top + 1);
                eval.TopicRecallAtK[top]      = topicHitCount / (float)actualTopics.TotalTopics;

                if (eval.KeywordPercisionAtK[top] > 0 || eval.KeywordRecallAtK[top] > 0)
                {
                    eval.KeywordF1AtK[top] = 2 * (eval.KeywordPercisionAtK[top] * eval.KeywordRecallAtK[top]) / (eval.KeywordPercisionAtK[top] + eval.KeywordRecallAtK[top]);
                }
                else
                {
                    eval.KeywordF1AtK[top] = 0;
                }

                if (eval.TopicPercisionAtK[top] > 0 || eval.TopicRecallAtK[top] > 0)
                {
                    eval.TopicF1AtK[top] = 2 * (eval.TopicPercisionAtK[top] * eval.TopicRecallAtK[top]) / (eval.TopicPercisionAtK[top] + eval.TopicRecallAtK[top]);
                }
                else
                {
                    eval.TopicF1AtK[top] = 0;
                }
            }

            return(eval);
        }
        public void AddCandidateTopicsToSpecificWindow(string timeSlotID, CandidateTopics candTopic)
        {
            int index = ListOfWindows.FindIndex(w => w.TimeSlotID.Equals(timeSlotID));

            if (index >= 0)
            {
                ListOfWindows[index].CandidateTopics = candTopic;
            }
            else
            {
                throw new Exception("Given Time Slot does not exist in the Sliding Windows");
            }
        }
Exemple #4
0
        public static StringBuilder GenerateText(CandidateTopics candTopic)
        {
            StringBuilder strBuilder = new StringBuilder();

            float[] scores;// = candTopic.CandidateKeywords.Select(x => x.Ranking).ToArray<float>();

            //scores = ScaleData(candTopic.CandidateKeywords.Select(x => x.Ranking).ToArray<float>(), 1, 100);
            scores = candTopic.CandidateKeywords.Select(x => x.Ranking).ToArray <float>();


            Random rnd = new Random();

            for (int i = 0; i < candTopic.CandidateKeywords.Count; i++)
            {
                double occurence;

                if (candTopic.CandidateKeywords[i].Word.Trim().Equals("king") ||
                    candTopic.CandidateKeywords[i].Word.Trim().Equals("price") ||
                    candTopic.CandidateKeywords[i].Word.Trim().Equals("gallon") ||
                    candTopic.CandidateKeywords[i].Word.Trim().Equals("saudi"))
                {
                    occurence = Math.Log(Math.Ceiling(scores[i]) + 102, 2) + 5;
                }
                else
                {
                    occurence = Math.Log(Math.Ceiling(scores[i]) + 102, 2);
                }

                for (int j = 1; j < (int)occurence; j++)
                {
                    strBuilder.Append(candTopic.CandidateKeywords[i].Word + " ");
                }
            }

            return(strBuilder);
        }
        public StringBuilder DetectEventCandidates(ListDHGs lstDHG, GroundTruth groundTruth, SlidingWindows slidingWindow, Config _config)
        {
            double[] currentWinThreshold = new double[] {
                0,                                        // Threshold against Growth factor
                0,                                        // Threshold against Trend Probability
                0,                                        // Threshold against Aggregated Centrality
                0                                         // Threshold against Fused features as Heartbeat
            };

            //double currentWinThreshold = 0;

            StringBuilder strBuilder = new StringBuilder();

            foreach (SingleWindow win in slidingWindow.ListOfWindows)
            {
                // cross check if following LINQ line creates a shallow copy, because I need a SHALLOW COPY
                // and same reference will be passed on to multiple functions.
                //ListDHGs candidateWindowDHGs = (ListDHGs)lstDHG.DHGs.Where(x => x.TimeSlotID.Equals(win.TimeSlotID));
                ListDHGs candidateWindowDHGs = lstDHG.Select(win.TimeSlotID);

                //candidateWindowDHGs.DHGs[0].IsEventCandidate = true;

                string str = "";
                str += win.TimeSlotID + ",";


                //if ( isGrowthFactor)
                //    currentWinThreshold = win.threshold = CalculateWindowThreshold(candidateWindowDHGs.DHGs.Select(x => (double)x.GrowthFactor).ToArray<double>(), parameterOmega);
                //else

                //currentWinThreshold = win.threshold = CalculateWindowThreshold(candidateWindowDHGs.DHGs.Select(x => x.HeartbeatScore).ToArray<double>(), _config.AdjustmentParameter);


                if (_config.CandidateSelectionCriteria == CandidateSelection.HEARTBEAT)
                {
                    currentWinThreshold[3] = win.threshold = CalculateWindowThreshold(candidateWindowDHGs.DHGs.Select(x => x.HeartbeatScore).ToArray <double>(), _config.AdjustmentParameter);
                }
                else if (_config.CandidateSelectionCriteria == CandidateSelection.FEATURES_BASED_UNION)
                {
                    if (_config.IsGF)
                    {
                        currentWinThreshold[0] = win.threshold = CalculateWindowThreshold(candidateWindowDHGs.DHGs.Select(x => x.GrowthFactor).ToArray <double>(), _config.AdjustmentParameter);
                    }

                    if (_config.IsTP)
                    {
                        currentWinThreshold[1] = win.threshold = CalculateWindowThreshold(candidateWindowDHGs.DHGs.Select(x => (x.PosTrendProbability - x.NegTrendProbability)).ToArray <double>(), _config.AdjustmentParameter);
                    }

                    if (_config.IsDC)
                    {
                        currentWinThreshold[2] = win.threshold = CalculateWindowThreshold(candidateWindowDHGs.DHGs.Select(x => x.AggregatedCentrality).ToArray <double>(), _config.AdjustmentParameter);
                    }
                }

                //////////////////////////////////////////////////////////////////////////////////
                ////////////// CANDIDATE EVENT DETECTION USING ADOPTIVE THRESHOLD /////////////////
                //////////////////////////////////////////////////////////////////////////////////

                // Total Candidates Count = 0=>GF, 1=>TP, 2=>AC, 3=>HB, 4=>Union
                double[] totalCandidates = MarkEventCandidates(candidateWindowDHGs, currentWinThreshold, _config);

                CandidateTopics candTopic = new CandidateTopics();

                /////////////////////////////////////////////////////////////////////
                //////////////////// MERGING CANDIDATE DHGs /////////////////////////
                /////////////////////////////////////////////////////////////////////
                foreach (DHGFeatures dhg in candidateWindowDHGs)
                {
                    if (dhg.IsEventCandidate)
                    {
                        str += dhg.DHG_No + ",";

                        foreach (Term term in dhg.BoW)
                        {
                            int index;
                            if ((index = candTopic.Contains(term.Word)) >= 0)
                            {
                                //if ((float)candTopic.CandidateKeywords[term.Word] < term.Ranking)
                                //    candTopic.CandidateKeywords[term.Word] = term.Ranking;
                                if (candTopic.CandidateKeywords[index].Ranking < term.Ranking)
                                {
                                    candTopic.CandidateKeywords[index] = term;
                                }
                            }
                            else
                            {
                                candTopic.CandidateKeywords.Add(new Term(term));
                            }
                        }
                    }
                } // end of all the DHGs in candidate window

                candTopic.SortRankingWise();
                win.CandidateTopics = candTopic;

                Util.WriteCadidateTopics(candTopic, win.TimeSlotID);

                //candTopic.RemoveBoWBelow(topK);
                ///////////////////////////////////////////////////////////////
                ///// MATCHING RESULTS WITH GROUND TRUTH AND EVALUATING ///////
                ///////////////////////////////////////////////////////////////
                FullDatasetRawResults.Add(
                    new KeyValuePair <string, Evaluation>(
                        win.TimeSlotID,
                        MatchResultWithGroundTruth(
                            (ActualTopics)groundTruth
                            .TimeSlot[win.TimeSlotID],
                            candTopic,
                            _config.TopK
                            )
                        )
                    );

                str += ",,THRESHOLD (GF:" + Math.Round(currentWinThreshold[0], 2).ToString() +
                       " | TP:" + Math.Round(currentWinThreshold[1], 2).ToString() +
                       " | AC:" + Math.Round(currentWinThreshold[2], 2).ToString() +
                       " | HB:" + Math.Round(currentWinThreshold[3], 2).ToString() +
                       "  AND  CANDIDATE COUNT (GF:" + totalCandidates[0].ToString() +
                       " | TP:" + totalCandidates[1].ToString() +
                       " | AC:" + totalCandidates[2].ToString() +
                       " | HB:" + totalCandidates[3].ToString() +
                       " | Union:" + totalCandidates[4].ToString() + ")\n";
                strBuilder.Append(str);
            }

            MicroAveragingResults(_config.TopK);

            Util.WriteFinalResults(FinalResult, _fileName, _config, strBuilder);


            return(strBuilder);
        }
 public SingleWindow(string timeID, List <string> dhgIDs)
 {
     TimeSlotID      = timeID;
     DHGsIDs         = new List <string>(dhgIDs);
     CandidateTopics = new CandidateTopics();
 }