コード例 #1
0
        /// <summary>
        /// Calculates number of occurrences of each word in a webpage.
        /// </summary>
        /// <returns>Dictionary of each word.</returns>
        public override Dictionary <string, int> CalculateOccuranceInText()
        {
            if (IsFilterStopWords && stopwordDictionary == null)
            {
                stopwordDictionary = CoreUtil.ProcessStopWords(StopWords);
            }

            if (htmlPage == null)
            {
                var htmlWeb        = new HtmlWeb();
                var lastStatusCode = HttpStatusCode.OK;

                htmlWeb.PostResponse = (request, response) =>
                {
                    if (response != null)
                    {
                        lastStatusCode = response.StatusCode;
                    }
                };

                htmlPage = htmlWeb.Load(Input);

                if (lastStatusCode != HttpStatusCode.OK)
                {
                    throw new Exception($"{Constant.WEB_REQUEST_ERROR_MESSAGE} {lastStatusCode}");
                }
            }

            var bodyText = htmlPage.DocumentNode.SelectSingleNode("//body").InnerText;

            allWordDictionary = CoreUtil.ProcessInput(bodyText, stopwordDictionary);

            return(allWordDictionary);
        }
コード例 #2
0
        /// <summary>
        /// Calculates number of occurrences of each word in a english text.
        /// </summary>
        /// <returns>Dictionary of each word.</returns>
        public override Dictionary <string, int> CalculateOccuranceInText()
        {
            if (IsFilterStopWords && stopwordDictionary == null)
            {
                stopwordDictionary = CoreUtil.ProcessStopWords(StopWords);
            }

            return(CoreUtil.ProcessInput(Input, stopwordDictionary));
        }
コード例 #3
0
        public void ProcessInput_WithEmptyText_ReturnEmptyDictionary(string input)
        {
            //arrange
            var stopWordDic = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);
            var expected    = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);

            //act
            var actual = CoreUtil.ProcessInput(input, stopWordDic);

            //assert
            Assert.AreEqual(actual, expected);
            Assert.IsEmpty(actual);
        }
コード例 #4
0
        public void ProcessInput_WithIdenticalWordsDifferentLetterCase_ReturnDictionaryOfOneKeyWithThreeOcurrance(string input)
        {
            //arrange
            var stopWordDic = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);
            var expected    = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase)
            {
                { "Hello", 3 }
            };

            //act
            var actual = CoreUtil.ProcessInput(input, stopWordDic);

            //assert
            Assert.AreEqual(actual, expected);
        }
コード例 #5
0
        public void ProcessInput_WithDifferentWordsAndPunctuations_ReturnDictionaryOfTwoKeyWithOneOcurranceEach(string input)
        {
            //arrange
            var stopWordDic = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);
            var expected    = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase)
            {
                { "Hello", 1 },
                { "World", 1 }
            };

            //act
            var actual = CoreUtil.ProcessInput(input, stopWordDic);

            //assert
            Assert.AreEqual(actual, expected);
        }