Ejemplo n.º 1
0
        private IEnumerable <KeywordPhraseReference> MergeWeakReferences(
            ICollection <KeywordPhraseReference> weakReferences, IEnumerable <PhraseIdWithRankModel> tops)
        {
            var sortedWeakReferences = new List <KeywordPhraseReference>(weakReferences);

            foreach (var top in tops)
            {
                if (sortedWeakReferences.Count < 10)
                {
                    sortedWeakReferences.Add(new KeywordPhraseReference
                    {
                        Convergence = top.Rank,
                        Phrase      = Phrases.Single(x => x.Id == top.PhraseId)
                    });
                    continue;
                }
                sortedWeakReferences.Sort(new WeakReferenceComparer());
                if (!(top.Rank > sortedWeakReferences[9].CompositeConvergance))
                {
                    continue;
                }

                if (sortedWeakReferences.Any(x => x.Phrase.Value.Equals(Phrases.Single(g => g.Id == top.PhraseId).Value)))
                {
                    continue;
                }

                var referenceToRemove = sortedWeakReferences.ElementAt(9);
                KeywordPhraseReferences.Remove(referenceToRemove);
                sortedWeakReferences.RemoveAt(9);

                var referenceToAdd = new KeywordPhraseReference
                {
                    Convergence = top.Rank,
                    Phrase      = Phrases.Single(x => x.Id == top.PhraseId)
                };
                KeywordPhraseReferences.Add(referenceToAdd);
                sortedWeakReferences.Add(referenceToAdd);
                sortedWeakReferences.Sort(new WeakReferenceComparer());
            }
            weakReferences = sortedWeakReferences;
            if (weakReferences.Count != 10)
            {
                throw new WeakReferenceCountException();
            }
            return(weakReferences);
        }
Ejemplo n.º 2
0
        public IEnumerable <Phrase> TheAlgorithm(string keyword)
        {
            var keywordEntity = Keywords.Include(x => x.WeakReferences.Select(r => r.Phrase)).SingleOrDefault(x => x.Value == keyword) ?? new Keyword {
                Value = keyword
            };

            keywordEntity.Count++;
            if (keywordEntity.Phrase != null)
            {
                SaveChanges();
                return(new[] { keywordEntity.Phrase });
            }
            if (keywordEntity.WeakReferences == null)
            {
                keywordEntity.WeakReferences = new Collection <KeywordPhraseReference>();
            }
            var weakReferences = keywordEntity.WeakReferences;

            if (!weakReferences.Any())
            {
                var outp = new List <Phrase>();
                foreach (var top in GetTopTen(keyword))
                {
                    var phrase    = Phrases.Single(x => x.Id == top.PhraseId);
                    var reference = new KeywordPhraseReference
                    {
                        Convergence = top.Rank,
                        Phrase      = phrase,
                        Keyword     = keywordEntity
                    };
                    KeywordPhraseReferences.Add(reference);
                    outp.Add(reference.Phrase);
                }
                SaveChanges();
                if (outp.Count != 10)
                {
                    throw new Exception("Powinno być dokładnie 10 luźnych referencji!");
                }
                return(outp);
            }

            var mergedReferences = MergeWeakReferences(weakReferences, GetTopTen(keyword));

            SaveChanges();
            return(mergedReferences.Select(x => x.Phrase));
        }