Exemplo n.º 1
0
        private void RunSimilarityModel(CitationPopularityModel citationModel,
                                        SimilarityRanker similarityModel)
        {
            Dictionary <string, double> scoredArticles = citationModel.PopularityScoredArticles;

            SortedDictionary <double, PopularGroup> result =
                new SortedDictionary <double, PopularGroup>(new DescDuplicateDoubleComp());

            while (scoredArticles.Count() > similarityModel.MaxNumSimilar)
            {
                _PrlOpt.CancellationToken.ThrowIfCancellationRequested();

                KeyValuePair <string, double> thisArt = getMaxVal(scoredArticles);

                var similarArt = similarityModel.GetTopSimilarArticlesTo(thisArt.Key);

                PopularGroup pop = new PopularGroup();
                pop.TitleArticle = thisArt.Key;
                foreach (KeyValuePair <double, string> p in similarArt)
                {
                    _PrlOpt.CancellationToken.ThrowIfCancellationRequested();

                    try { pop.SimilarArticles[p.Key] = p.Value; }
                    catch (Exception) { }

                    try { scoredArticles.Remove(p.Value); }
                    catch (Exception) { }
                }
                result[thisArt.Value] = pop;

                // remove already ranked articles
                try { scoredArticles.Remove(thisArt.Key); }
                catch (Exception) { }
            }

            // The few left overs
            foreach (string art in scoredArticles.Keys)
            {
                _PrlOpt.CancellationToken.ThrowIfCancellationRequested();

                PopularGroup pop = new PopularGroup();
                pop.TitleArticle            = art;
                result[scoredArticles[art]] = pop;
            }

            // Write the fully ranked cluster results to the database
            RankerDbAccess.WritePopularitySimilarityRankedClustersToDb(
                VerticalId, result, _cToken);
        }
Exemplo n.º 2
0
        public static SortedDictionary <DateTimeOffset, PopularGroup> ReadPopularitySimilarityRankedArticlesFromDb(
            Grouping verticalId,
            CancellationTokenSource cancellationToken)
        {
            ParallelOptions prlOpt = new ParallelOptions()
            {
                MaxDegreeOfParallelism = System.Environment.ProcessorCount,
                CancellationToken      = cancellationToken.Token
            };

            SortedDictionary <DateTimeOffset, PopularGroup> results =
                new SortedDictionary <DateTimeOffset, PopularGroup>(new DescDuplicateDateTimeOffsetComp());

            SortedDictionary <string, string> dataFromDb = new SortedDictionary <string, string>();

            // Lock RANKINGS table
            // Read (string, string> key,value pairs from the database into dataFromDb
            //unlock RANKINGS table

            char[] sep = { RankingDataProcessor.Separator };

            foreach (string art in dataFromDb.Keys)
            {
                prlOpt.CancellationToken.ThrowIfCancellationRequested();

                string[] parts = dataFromDb[art].Split(sep);

                if (parts[0] != "S")
                {
                    throw new Exception("[ERROR: RankerDbAccess::ReadPopularitySimilarityRankedArticlesFromDb()]"
                                        + " Error in the data read in.");
                }

                DateTimeOffset key = DateTimeOffset.Parse(parts[1]);
                PopularGroup   pop = new PopularGroup();
                pop.TitleArticle = art;

                for (int i = 2; i < parts.Count(); i = i + 2)
                {
                    double d = Double.Parse(parts[i + 1]);
                    pop.SimilarArticles[d] = parts[i];
                }

                results[key] = pop;
            }

            return(results);
        }