コード例 #1
0
        private void ShouldReturnTopicSubItemsListForSideBar()
        {
            var firstSubitem = new SubItem(TextHelper.AnyString, "first-subitem", TextHelper.AnyString, TextHelper.AnyString,
                                           TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var subItems = new List <SubItem> {
                firstSubitem
            };
            var firstSecondaryitem = new SubItem(TextHelper.AnyString, "first-secondaryitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var secondaryItems     = new List <SubItem> {
                firstSecondaryitem
            };

            var topic   = new Topic(TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, subItems, secondaryItems, new List <SubItem>(), new List <Crumb>(), new List <Alert>(), false, TextHelper.AnyString, null, String.Empty, new List <ExpandingLinkBox>(), String.Empty, string.Empty, true);
            var article = new ProcessedArticle(TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <ProcessedSection>(),
                                               TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <Crumb>(), new List <Alert>(), topic, new List <Alert>(), null, new DateTime(), new bool());

            var articleViewModel = new ArticleViewModel(article);

            bool showMoreButton;
            var  sidebarSubItems = articleViewModel.SidebarSubItems(out showMoreButton);

            sidebarSubItems.Count().Should().Be(2);
            sidebarSubItems.ToList()[0].Should().Be(firstSubitem);
            sidebarSubItems.ToList()[1].Should().Be(firstSecondaryitem);
            showMoreButton.Should().Be(false);
        }
コード例 #2
0
        public void CalculateSimilariteArticle2NullExc()
        {
            var calculator = new CosineSimilarityCalculator();
            var article    = new ProcessedArticle(new Dictionary <IToken, TfIdf>());

            calculator.CalculateSimilarity(article, null);
        }
コード例 #3
0
        private ProcessedArticle GetProcessedArticle(string url)
        {
            IArticle         unprocessedArticle = _articleProvider.Get(url);
            ProcessedArticle result             = _articleProcessor.Process(unprocessedArticle);

            return(result);
        }
コード例 #4
0
        public async Task GetsAlertsInlineForASectionInAnArticle()
        {
            var alertsInline = new List <Alert>
            {
                new Alert("title", "subheading", "body", Severity.Warning, new DateTime(0001, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                          new DateTime(9999, 9, 9, 0, 0, 0, DateTimeKind.Utc), String.Empty, false)
            };

            var processedSection = new ProcessedSection("title", "slug", string.Empty, "body", new List <Profile>(), new List <Document>(), alertsInline);

            var article = new ProcessedArticle(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty,
                                               new List <ProcessedSection>()
            {
                processedSection
            }, string.Empty, string.Empty, string.Empty, new List <Crumb>()
            {
            }, new List <Alert>(), new NullTopic(), alertsInline, null, new DateTime(), new bool());

            _articleRepository.Setup(o => o.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(new HttpResponse(200, article, string.Empty));

            var indexPage = await _controller.Article("healthy-living", DefaultMessage, string.Empty, string.Empty) as ViewResult;;
            var result    = indexPage.ViewData.Model as ArticleViewModel;

            result.Article.Sections.FirstOrDefault().AlertsInline.Should().HaveCount(1);
            result.Article.Sections.FirstOrDefault().AlertsInline.First().Title.Should().Be("title");
            result.Article.Sections.FirstOrDefault().AlertsInline.First().SubHeading.Should().Be("subheading");
            result.Article.Sections.FirstOrDefault().AlertsInline.First().Body.Should().Be("<p>body</p>\n");
            result.Article.Sections.FirstOrDefault().AlertsInline.First().Severity.Should().Be(Severity.Warning);
        }
コード例 #5
0
        public async Task MultipleSectionsArticleWithNoSectionSlugReturnsFirstSection()
        {
            const string articleSlug = "physical-activity";
            var          sectionOne  = new ProcessedSection("Overview", "physical-activity-overview", string.Empty, "body", new List <Profile>(), new List <Document>(), new List <Alert>());
            var          sectionTwo  = new ProcessedSection("Types of Physical Activity", TextHelper.AnyString, TextHelper.AnyString, "body", new List <Profile>(), new List <Document>(), new List <Alert>());

            var article = new ProcessedArticle(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty,
                                               new List <ProcessedSection>()
            {
                sectionOne, sectionTwo
            }, string.Empty, string.Empty, string.Empty, new List <Crumb>()
            {
            },
                                               new List <Alert>(), new NullTopic(), new List <Alert>(), null, new DateTime(), new bool());

            var response = new HttpResponse(200, article, string.Empty);

            _articleRepository.Setup(o => o.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(response);

            var view             = await _controller.Article(articleSlug, DefaultMessage, string.Empty, string.Empty) as ViewResult;;
            var displayedArticle = view.ViewData.Model as ArticleViewModel;

            displayedArticle.DisplayedSection.Title.Should().Contain("Overview");
            displayedArticle.DisplayedSection.Slug.Should().Be("physical-activity-overview");
            displayedArticle.ShouldShowArticleSummary.Should().BeTrue();
        }
コード例 #6
0
        public async Task MultipleSectionsArticleWithSectionSlugViewDataCanonicalUrlShouldNotBeNull()
        {
            const string articleSlug = "physical-activity";
            const string sectionSlug = "physical-activity-overview";

            var sectionOne = new ProcessedSection("Overview", "physical-activity-overview", string.Empty, "body", new List <Profile>(), new List <Document>(), new List <Alert>());
            var sectionTwo = new ProcessedSection("Types of Physical Activity", TextHelper.AnyString, TextHelper.AnyString, "body", new List <Profile>(), new List <Document>(), new List <Alert>());

            var article = new ProcessedArticle(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty,
                                               new List <ProcessedSection>()
            {
                sectionOne, sectionTwo
            }, string.Empty, string.Empty, string.Empty,
                                               new List <Crumb>()
            {
            }, new List <Alert>(), new NullTopic(), new List <Alert>(), null, new DateTime(), new bool());

            var response = new HttpResponse(200, article, string.Empty);

            _articleRepository.Setup(o => o.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(response);

            var view = await _controller.ArticleWithSection(articleSlug, sectionSlug, DefaultMessage, "", "") as ViewResult;;

            view.ViewData["CanonicalUrl"].Should().NotBeNull();

            string canonicalUrl = (string)view.ViewData["CanonicalUrl"];

            canonicalUrl.Should().Contain("physical-activity");
            canonicalUrl.Should().NotContain("physical-activity-overview");
        }
コード例 #7
0
        public async Task GivenNavigateToArticleReturnsArticleView()
        {
            const string articleSlug = "physical-activity";
            var          article     = new ProcessedArticle("Physical Activity", "physical-activity",
                                                            "Being active is great for your body", "teaser", "meta description", new List <ProcessedSection>()
            {
                DummySection()
            },
                                                            "fa-icon", "af981b9771822643da7a03a9ae95886f/runners.jpg", "af981b9771822643da7a03a9ae95886f/runners.jpg",
                                                            new List <Crumb>()
            {
                new Crumb("title", "slug", "type")
            }, new List <Alert>(), new NullTopic(), new List <Alert>(), null, new DateTime(), new bool());

            _articleRepository.Setup(o => o.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(new HttpResponse(200, article, string.Empty));

            var articlePage = await _controller.Article(articleSlug, DefaultMessage, string.Empty, string.Empty) as ViewResult;;
            var viewModel   = articlePage.ViewData.Model as ArticleViewModel;

            viewModel.Article.Title.Should().Contain("Physical Activity");
            viewModel.Article.NavigationLink.Should().Be("/physical-activity");
            viewModel.Article.Body.Should().Contain("Being active is great for your body");
            viewModel.Article.BackgroundImage.Should().Contain("af981b9771822643da7a03a9ae95886f/runners.jpg");
            viewModel.Article.Image.Should().Contain("af981b9771822643da7a03a9ae95886f/runners.jpg");
            viewModel.Article.Icon.Should().Contain("fa-icon");
            viewModel.Article.Sections.Count().Should().Be(1);
        }
コード例 #8
0
        public double CalculateSimilarity(ProcessedArticle article1, ProcessedArticle article2)
        {
            if (article1 == null || article2 == null)
            {
                throw new ArgumentNullException();
            }

            double numerator = 0.0;
            double sqrsum1 = 0.0, sqrsum2 = 0.0;

            foreach (TfIdf tfIdf in article1.BagOfWords.Values)
            {
                double tfidf2 = article2.BagOfWords.ContainsKey(tfIdf.Token)
                                    ? article2.BagOfWords[tfIdf.Token].Value
                                    : 0.0;
                ProcessTfIdfValue(tfIdf.Value, tfidf2, ref numerator, ref sqrsum1, ref sqrsum2);
            }
            foreach (TfIdf tfIdf in article2.BagOfWords.Values.Where(v => !article1.BagOfWords.ContainsKey(v.Token)))
            {
                ProcessTfIdfValue(0.0, tfIdf.Value, ref numerator, ref sqrsum1, ref sqrsum2);
            }

            double similarity = numerator / (Math.Sqrt(sqrsum1) * Math.Sqrt(sqrsum2));

            return(similarity);
        }
コード例 #9
0
        public void CalculateSimilarityTest()
        {
            var calculator = new CosineSimilarityCalculator();
            var token1     = MockRepository.GenerateStub <IToken>();

            token1.Stub(t => t.Text)
            .Return("a");
            var token2 = MockRepository.GenerateStub <IToken>();

            token2.Stub(t => t.Text)
            .Return("b");
            var token3 = MockRepository.GenerateStub <IToken>();

            token3.Stub(t => t.Text)
            .Return("c");
            var bow1 = new Dictionary <IToken, TfIdf>
            {
                { token1, new TfIdf(0.3, token1) },
                { token2, new TfIdf(0.1, token2) }
            };
            var bow2 = new Dictionary <IToken, TfIdf>
            {
                { token2, new TfIdf(0.5, token2) },
                { token3, new TfIdf(0.2, token3) }
            };
            var processed1 = new ProcessedArticle(bow1);
            var processed2 = new ProcessedArticle(bow2);

            double similarity = calculator.CalculateSimilarity(processed1, processed2);

            Assert.IsTrue(similarity.IsAbout(0.294, 0.001));
        }
コード例 #10
0
 private void SetArticlesCanonicalUrl(string articleSlug, string sectionSlug, ProcessedArticle article)
 {
     if (article.Sections.Any() && article.Sections.First().Slug == sectionSlug)
     {
         ViewData["CanonicalUrl"] = "/" + articleSlug;
     }
 }
コード例 #11
0
        public void ShouldAddErrorMessageToArticleBodyWithFormTagInsideIfEmptySlugGiven()
        {
            var processedArticle = new ProcessedArticle("title", "slug", _bodyWithContactUsMessageTag, "teaser", "meta description", new List <ProcessedSection>(), "icon", "backgroundImage", "image", new List <Crumb>(), new List <Alert>(), DefaultTopic(), new List <Alert>(), null, new DateTime(), new bool());

            _tagParser.Parse(processedArticle, Message, "");

            processedArticle.Body.Should().Be($"This is some content <p>{ Message }</p> <form><form>");
        }
コード例 #12
0
 public ArticleViewModel(ProcessedArticle article, string sectionSlug)
 {
     Article                  = article;
     DisplayedSection         = GetSectionOrThrowSectionNotFound(sectionSlug);
     ShouldShowArticleSummary = (Article.Sections.First().Slug == DisplayedSection.Slug);
     OgTitleMetaData          = string.Concat(Article.Title, !string.IsNullOrEmpty(DisplayedSection.Title) ? " - " : "", DisplayedSection.Title);
     HideLastUpdated          = Article.HideLastUpdated;
 }
コード例 #13
0
 public ArticleViewModel(ProcessedArticle article)
 {
     Article                  = article;
     DisplayedSection         = FirstOrNull(article.Sections);
     HideLastUpdated          = Article.HideLastUpdated;
     ShouldShowArticleSummary = true;
     ShouldShowCanonicalLink  = false;
     OgTitleMetaData          = Article.Title;
 }
コード例 #14
0
        public async Task ArticleWithSectionMetaDescriptionReturnsViewDataWithMetaDescription()
        {
            // Arrange
            var expectedMetaDescription = "test meta description";
            var sectionSlug             = "test-slug";
            var section = new ProcessedSection(
                string.Empty,
                sectionSlug,
                expectedMetaDescription,
                string.Empty,
                null,
                null,
                null
                );
            var article = new ProcessedArticle(
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                new List <ProcessedSection> {
                section
            },
                string.Empty,
                string.Empty,
                null,
                null,
                null,
                null,
                null,
                null,
                new DateTime(),
                new bool()
                );

            _articleRepository
            .Setup(_ => _.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new HttpResponse(200, article, string.Empty));

            // Act
            var result = await _controller
                         .ArticleWithSection(
                string.Empty,
                sectionSlug,
                string.Empty,
                string.Empty,
                string.Empty) as ViewResult;

            var resultModel = result.ViewData.Model as ArticleViewModel;

            // Assert
            resultModel.Should().NotBeNull();
            resultModel?.DisplayedSection.MetaDescription.Should().Be(expectedMetaDescription);
        }
コード例 #15
0
        public void ShouldDoNothingIfSlugProvidedButNoSectionsAreProvided()
        {
            var slug             = "this-is-a-slug";
            var processedArticle = new ProcessedArticle("title", "slug", DefaultBody, "teaser", "meta description", new List <ProcessedSection>()
            {
            }, "icon", "backgroundImage", "image", new List <Crumb>(), new List <Alert>(), DefaultTopic(), new List <Alert>(), null, new DateTime(), new bool());

            _tagParser.Parse(processedArticle, Message, slug);

            processedArticle.Body.Should().Be(DefaultBody);
        }
コード例 #16
0
        public void ShouldRenderMessage()
        {
            var slug = "this-is-a-slug";

            var processedArticle = new ProcessedArticle("title", "slug", DefaultBody, "teaser", "meta description", new List <ProcessedSection>()
            {
            }, "icon", "backgroundImage", "image", new List <Crumb>(), new List <Alert>(), DefaultTopic(), new List <Alert>(), null, new DateTime(), new bool());

            _tagParser.Parse(processedArticle, Message, slug);

            _viewRenderer.Verify(o => o.Render("ContactUsMessage", Message), Times.Once);
        }
コード例 #17
0
        public double Compare(string url1, string url2)
        {
            if (string.IsNullOrWhiteSpace(url1) || string.IsNullOrWhiteSpace(url2))
            {
                throw new ArgumentException("Invalid URL");
            }

            ProcessedArticle article1 = GetProcessedArticle(url1);
            ProcessedArticle article2 = GetProcessedArticle(url2);

            double result = _similarityCalculator.CalculateSimilarity(article1, article2);

            return(result);
        }
コード例 #18
0
        public void ShouldAddErrorMessageToSectionBodyWithFormTagInsideIfCorrespondingSlugGiven()
        {
            var slug             = "this-is-a-slug";
            var section          = ProcessedSectionWithDefaultSlugAndBody();
            var anotherSection   = ProcessedSectionWithDefaultSlugAndBody(slug: slug, body: _bodyWithContactUsMessageTag);
            var processedArticle = new ProcessedArticle("title", "slug", DefaultBody, "teaser", "meta description", new List <ProcessedSection>()
            {
                section, anotherSection
            }, "icon", "backgroundImage", "image", new List <Crumb>(), new List <Alert>(), DefaultTopic(), new List <Alert>(), null, new DateTime(), new bool());

            _tagParser.Parse(processedArticle, Message, slug);

            processedArticle.Body.Should().Be(DefaultBody);
            section.Body.Should().Be(DefaultBody);
            anotherSection.Body.Should().Be($"This is some content <p>{ Message }</p> <form><form>");
        }
コード例 #19
0
        public void ShouldNotAddAnyMessageIfNoMessageGiven(string message)
        {
            var slug             = "this-is-a-slug";
            var section          = ProcessedSectionWithDefaultSlugAndBody();
            var anotherSection   = ProcessedSectionWithDefaultSlugAndBody(slug: slug, body: _bodyWithContactUsMessageTag);
            var processedArticle = new ProcessedArticle("title", "slug", DefaultBody, "teaser", "meta description", new List <ProcessedSection>()
            {
                section, anotherSection
            }, "icon", "backgroundImage", "image", new List <Crumb>(), new List <Alert>(), DefaultTopic(), new List <Alert>(), null, new DateTime(), new bool());

            _tagParser.Parse(processedArticle, message, slug);

            processedArticle.Body.Should().Be(DefaultBody);
            section.Body.Should().Be(DefaultBody);
            anotherSection.Body.Should().Be(_bodyWithContactUsMessageTag);

            _viewRenderer.Verify(o => o.Render(It.IsAny <string>(), It.IsAny <object>()), Times.Never);
        }
コード例 #20
0
        private void ShouldSetMetaDescription(string sectionMeta, string articleMeta, string expectedMeta)
        {
            // Arrange
            var sectionSlug = "test-slug";
            var section     = new ProcessedSection(
                string.Empty,
                sectionSlug,
                sectionMeta,
                string.Empty,
                null,
                null,
                null
                );
            var article = new ProcessedArticle(
                string.Empty,
                string.Empty,
                string.Empty,
                string.Empty,
                articleMeta,
                new List <ProcessedSection> {
                section
            },
                string.Empty,
                string.Empty,
                null,
                null,
                null,
                null,
                null,
                null,
                new DateTime(),
                new bool()
                );

            // Act
            var model = new ArticleViewModel(article, sectionSlug);

            // Assert
            model.MetaDescription.Should().Be(expectedMeta);
        }
コード例 #21
0
        private void ShouldReturnSixTopicsSubItemsForSideBar()
        {
            var firstSubItem   = new SubItem(TextHelper.AnyString, "first-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var secondSubItem  = new SubItem(TextHelper.AnyString, "second-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var thirdSubItem   = new SubItem(TextHelper.AnyString, "third-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var fourthSubItem  = new SubItem(TextHelper.AnyString, "fourth-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var fifthSubItem   = new SubItem(TextHelper.AnyString, "fifth-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var sixthSubItem   = new SubItem(TextHelper.AnyString, "sixth-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var seventhSubItem = new SubItem(TextHelper.AnyString, "seventh-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());
            var eightSubItem   = new SubItem(TextHelper.AnyString, "eigth-subitem", TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <SubItem>());

            var subItems = new List <SubItem> {
                firstSubItem, secondSubItem, thirdSubItem, fourthSubItem
            };
            var secondaryItems = new List <SubItem> {
                fifthSubItem, sixthSubItem, seventhSubItem, eightSubItem
            };

            var topic = new Topic(TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString,
                                  subItems, secondaryItems, new List <SubItem>(), new List <Crumb>(), new List <Alert>(), false, TextHelper.AnyString, null, "expandingLinkText", new List <ExpandingLinkBox>(), string.Empty, string.Empty, true);
            var article = new ProcessedArticle(TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <ProcessedSection>(), TextHelper.AnyString, TextHelper.AnyString, TextHelper.AnyString, new List <Crumb>(), new List <Alert>(), topic, new List <Alert>(), null, new DateTime(), new bool());

            var articleViewModel = new ArticleViewModel(article);

            bool showMoreButton;
            var  sidebarSubItems = articleViewModel.SidebarSubItems(out showMoreButton);

            sidebarSubItems.Count().Should().Be(6);
            sidebarSubItems.ToList()[0].Should().Be(firstSubItem);
            sidebarSubItems.ToList()[1].Should().Be(secondSubItem);
            sidebarSubItems.ToList()[2].Should().Be(thirdSubItem);
            sidebarSubItems.ToList()[3].Should().Be(fourthSubItem);
            sidebarSubItems.ToList()[4].Should().Be(fifthSubItem);
            sidebarSubItems.ToList()[5].Should().Be(sixthSubItem);
            showMoreButton.Should().Be(true);
        }
コード例 #22
0
ファイル: Knn.cs プロジェクト: jurczewski/KSR
        public static string guesLabel(IMetric metric, int k, List <ProcessedArticle> trainingArticles, ProcessedArticle articleToLabel)
        {
            List <ProcessedArticle> knn = trainingArticles.OrderBy(a => metric.GetDistance(a.getFeatureVector(), articleToLabel.getFeatureVector())).ToList();

            return(knn.Take(k).GroupBy(a => a.label).OrderBy(g => g.Count()).Last().Key);
        }