private static IEnumerable <IGrouping <string, string> > GetStemmedWordsGroups(IEnumerable <string> words) { return(words.Where(w => !WordCloudFiltering.IsStopWord(w)) .Select(w => Regex.Replace(w, @"[^a-z0-9]", string.Empty, RegexOptions.IgnoreCase)) .Where(w => !WordCloudFiltering.IsNumber(w)) .GroupBy(w => w, w => w)); }
public virtual void AddNewAnswer(SurveyAnswer surveyAnswer) { if (surveyAnswer == null) { return; } var newSurveyAnswersSummary = new SurveyAnswersSummary(surveyAnswer.SlugName) { TotalAnswers = 1 }; foreach (var questionAnswer in surveyAnswer.QuestionAnswers) { var questionType = (QuestionType)Enum.Parse(typeof(QuestionType), questionAnswer.QuestionType); var questionAnswersSummary = new QuestionAnswersSummary { QuestionType = questionType, QuestionText = questionAnswer.QuestionText, PossibleAnswers = questionAnswer.PossibleAnswers }; newSurveyAnswersSummary.QuestionAnswersSummaries.Add(questionAnswersSummary); switch (questionType) { case QuestionType.SimpleText: var words = questionAnswer.Answer.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var wordCounts = new Dictionary <string, int>(); var stemmedWords = words.Where(w => !WordCloudFiltering.IsStopWord(w)) .Select(w => Regex.Replace(w, @"[^a-z0-9]", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) .Where(w => !WordCloudFiltering.IsNumber(w)); stemmedWords.GroupBy(w => w, w => w).ToList() .ForEach(g => wordCounts.Add(g.Key, g.Count())); questionAnswersSummary.AnswersSummary = JsonConvert.SerializeObject(wordCounts); break; case QuestionType.MultipleChoice: var choiceCounts = new Dictionary <string, int> { { questionAnswer.Answer, 1 } }; questionAnswersSummary.AnswersSummary = JsonConvert.SerializeObject(choiceCounts); break; case QuestionType.FiveStars: var answer = double.Parse(questionAnswer.Answer); questionAnswersSummary.AnswersSummary = answer.ToString("F", CultureInfo.InvariantCulture); break; default: throw new ArgumentOutOfRangeException(); } } this.MergeWith(newSurveyAnswersSummary); }