Esempio n. 1
0
        /// <summary>
        /// Fetch words from a website page, count the frequencies,
        /// and then put them into a list of word objects.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task <IEnumerable <HtmlWordDto> > GetWordCloudData(string url)
        {
            List <HtmlWordDto> wordCloudData = new List <HtmlWordDto>();
            string             htmlContent   = await ParseHtml(url);

            if (htmlContent == String.Empty)
            {
                return(null);
            }
            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(htmlContent);
            HtmlNode rootNode = htmlDocument.DocumentNode;

            GetWords(rootNode);
            IEnumerable <KeyValuePair <string, int> > wordsCount = CountWords();
            //Save words count data into database
            var x = _wordsDbContext.HtmlWord.OrderByDescending(i => i.count);

            foreach (var item in wordsCount)
            {
                wordCloudData.Add(
                    new HtmlWordDto()
                {
                    Text   = item.Key,
                    Weight = item.Value
                });
                var wordInDb = x.Where(w => w.Word == item.Key).FirstOrDefault();
                if (wordInDb == null)
                {
                    var hashedWord = _saltHashHelper.GenerateSaltedHash(64, item.Key);
                    _wordsDbContext.Add(
                        new HtmlWord
                    {
                        SaltedHash = hashedWord.Hash,
                        Word       = item.Key,
                        count      = item.Value,
                        Salt       = hashedWord.Salt
                    }
                        );
                }
                else
                {
                    wordInDb.count += item.Value;
                }
            }
            ;
            _wordsDbContext.SaveChanges();

            return(wordCloudData);
        }
Esempio n. 2
0
        public void CreateSaltedHashShould()
        {
            HashSalt res = _saltHashHelper.GenerateSaltedHash(64, "Jason");

            Assert.True(_saltHashHelper.VerifyStr("Jason", res));
        }