コード例 #1
0
ファイル: WordsAnalyzer.cs プロジェクト: CaptainBelyash/di
 private Dictionary<string, int> GetWordsCounts(IReadOnlyCollection <string> words)
 {
     return words.Select(word => normalizer.Normalize(word))
             .Where(word => !filters.Any(f => f.ShouldExclude(word)))
             .GroupBy(word => word)
             .ToDictionary(group => group.Key, group => group.Count());
 }
コード例 #2
0
        public Result <Dictionary <string, int> > CountWords(IEnumerable <string> words)
        {
            var resultDictionary = new Dictionary <string, int>();

            foreach (var word in words)
            {
                var normalizedWordResult = normalizer.Normalize(word);
                if (!normalizedWordResult.IsSuccess)
                {
                    return(Result.Fail <Dictionary <string, int> >(
                               $"Cannot normalize word {word} {normalizedWordResult.Error}"));
                }
                var normalizedWord = normalizedWordResult.Value;
                if (filter.IsExcluded(normalizedWord))
                {
                    continue;
                }
                if (resultDictionary.ContainsKey(normalizedWord))
                {
                    resultDictionary[normalizedWord]++;
                }
                else
                {
                    resultDictionary[normalizedWord] = 1;
                }
            }

            return(Result.Ok(resultDictionary));
        }
コード例 #3
0
        public Image GetNewTagcloud(IEnumerable <string> words)
        {
            var neededWords = words
                              .Where(word => wordsFilters.All(filter => filter.Validate(word)))
                              .Select(word => wordNormalizer.Normalize(word))
                              .ToList();
            var counts = neededWords
                         .GroupBy(word => word)
                         .Select(group => new Word(group.Key, (double)group.Count() / neededWords.Count));

            return(drawer.GetTagsCloud(counts));
        }
コード例 #4
0
        private Result <List <string> > NormalizeWords(IEnumerable <string> words)
        {
            var normalizedWords = new List <string>();

            foreach (var word in words)
            {
                var normalizeResult = normalizer.Normalize(word);
                if (!normalizeResult.IsSuccess)
                {
                    return(Result.Fail <List <string> >(normalizeResult.Error));
                }
                normalizedWords.Add(normalizeResult.Value);
            }
            return(Result.Ok(normalizedWords));
        }
コード例 #5
0
ファイル: BlackListWordsFilter.cs プロジェクト: Inree/di
 public bool Validate(string word)
 {
     return(!BlackList.Contains(normalizer.Normalize(word)));
 }