コード例 #1
0
        public Dictionary <string, ParseBookFileResult> countWordsInFile(string file)
        {
            try
            {
                var wordCount = new Dictionary <string, ParseBookFileResult>();

                var ConcretePrimeChecker = new PrimeFromArray();
                var ParserResult         = new ParseBookFileResult(ConcretePrimeChecker);

                var content = System.IO.File.ReadAllText(file);

                var wordPattern = new Regex(@"\w+");

                foreach (Match match in wordPattern.Matches(content))
                {
                    if (!wordCount.ContainsKey(match.Value))
                    {
                        ParserResult       = new ParseBookFileResult(ConcretePrimeChecker);
                        ParserResult.count = 1;
                        wordCount.Add(match.Value, ParserResult);
                    }
                    else
                    {
                        wordCount[match.Value].count++;
                    }
                }
                return(wordCount);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #2
0
        public Dictionary <string, ParseBookFileResult> countWordsInFile(string pathfile)
        {
            try {
                var wordCount            = new Dictionary <string, ParseBookFileResult>();
                var ConcretePrimeChecker = new PrimeFromArray();
                var ParserResult         = new ParseBookFileResult(ConcretePrimeChecker);

                using (var fileStream = File.Open(pathfile, FileMode.Open, FileAccess.Read))
                    using (var streamReader = new StreamReader(fileStream))
                    {
                        string line;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            var words = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                            foreach (var word in words)
                            {
                                if (!wordCount.ContainsKey(word.StripPunctuation().ToString().ToLower()))
                                {
                                    ParserResult       = new ParseBookFileResult(ConcretePrimeChecker);
                                    ParserResult.count = 1;
                                    wordCount.Add(word.StripPunctuation().ToString().ToLower(), ParserResult);
                                }
                                else
                                {
                                    wordCount[word.StripPunctuation().ToString().ToLower()].count++;
                                }
                            }
                        }
                    }


                return(wordCount);
            }
            catch (Exception e)
            {
                throw e;
            }
        }