Exemplo n.º 1
0
        public IDictionary <string, int> FindKeywords(string pageContent)
        {
            if (string.IsNullOrEmpty(pageContent))
            {
                return(new Dictionary <string, int>());
            }

            string keywordsContent = _htmlWeb.SelectNodeAttribute(pageContent, MetaXPath, ContentAttribute);

            if (string.IsNullOrEmpty(keywordsContent))
            {
                return(new Dictionary <string, int>());
            }

            IEnumerable <string> keywords = keywordsContent.Split(',')
                                            .Select(keyword => keyword[0] == ' ' ? keyword.Remove(0, 1) : keyword)
                                            .Distinct();

            string body = _htmlWeb.SelectNode(pageContent, BodyXPath);

            if (string.IsNullOrEmpty(body))
            {
                return(new Dictionary <string, int>());
            }

            return(keywords
                   .Select(key => new { Key = key, Regex.Matches(body, key, RegexOptions.IgnoreCase | RegexOptions.Compiled).Count })
                   .OrderByDescending(key => key.Count)
                   .ToDictionary(k => k.Key, k => k.Count));
        }
Exemplo n.º 2
0
        public void GetPageContent_KeywordsNotExist_ReturnEmptyDictionary(string keywords)
        {
            IHtmlWebWrapper htmlWeb = Substitute.For <IHtmlWebWrapper>();

            htmlWeb.SelectNodeAttribute(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>()).Returns(keywords);
            KeywordsFinder keyworsdFinder = new KeywordsFinder(htmlWeb);


            var result = keyworsdFinder.FindKeywords("Test");


            result.Should().BeEmpty();
        }
Exemplo n.º 3
0
        public void GetPageContent_KeywordsAndBodyExist_ReturnProperDictionary()
        {
            IHtmlWebWrapper htmlWeb = Substitute.For <IHtmlWebWrapper>();

            htmlWeb.SelectNode(Arg.Any <string>(), Arg.Any <string>()).Returns("keywrod keyword1 keyword2 keyword keyword2");
            htmlWeb.SelectNodeAttribute(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>()).Returns("keyword1, keyword2, keyword1");
            KeywordsFinder keyworsdFinder = new KeywordsFinder(htmlWeb);


            var result = keyworsdFinder.FindKeywords("Test");


            result.Should().BeEquivalentTo(new Dictionary <string, int>
            {
                { "keyword2", 2 },
                { "keyword1", 1 },
            });
        }