Exemple #1
0
        public ResultWordCount ProcessCounting(string Context, string SearchKeyWords)
        {
            List <WordCount> _wordcount = new List <WordCount>();
            ResultWordCount  result     = new ResultWordCount();

            //split each word in context to list
            string[] _context = SearchKeyWords.Split(',');

            //search each word in context and add count into wordcount
            foreach (string word in _context)
            {
                int   _count = new int();
                var   rx     = new Regex(word, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                Match match  = rx.Match(Context);
                while (match.Success)
                {
                    _count += 1;
                    match   = match.NextMatch();
                }
                _wordcount.Add(new WordCount {
                    Word = word, Count = _count
                });
            }
            result.Output_Result = _wordcount;

            return(result);
        }
Exemple #2
0
        public ActionResult WordCount(FormCollection collection)
        {
            ViewBag.Message = "Your Word Count page.";
            ICounter _counter;

            //Get input from view
            ResultWordCount result         = new ResultWordCount();
            InputWordCount  inputWordCount = new InputWordCount
            {
                Input_SearchContext  = Convert.ToString(collection["Context"]),
                Input_SearchKeyWords = Convert.ToString(collection["SearchKeyWords"])
            };

            // --------------------

            if (!string.IsNullOrEmpty(inputWordCount.Input_SearchContext))     //If Context is there, perform search. Otherwise, return empty search result
            {
                if (string.IsNullOrEmpty(inputWordCount.Input_SearchKeyWords)) //if Search keyword is not specify, perform counting for each word in context. Otherwise, count based on keywords.
                {
                    _counter = new WordCounter();
                }
                else
                {
                    _counter = new KeyWordCounter();
                }

                result = _counter.ProcessCounting(inputWordCount.Input_SearchContext, inputWordCount.Input_SearchKeyWords); //Set output to View

                Session["result"] = result;                                                                                 //put result in session to ensure result kept in webgrid for sorting.
            }
            else
            {
                if ((ResultWordCount)Session["result"] == null)
                {
                    List <WordCount> _result = new List <WordCount>();
                    _result.Add(new WordCount {
                        Word = "", Count = 0
                    });
                    result.Output_Result = _result;
                }
                else
                {
                    result = (ResultWordCount)Session["result"]; //get result from session to ensure result kept in webgrid for sorting.
                }
            }

            return(View(result));
        }