1234567891011121314151617181920212223242526272829303132333435 |
- using System.Collections.Generic;
- using Newtonsoft.Json.Linq;
- using System.Linq;
- namespace analyzeJSON
- {
- public record WordCountResult<T>(T NodeCounts, int TotalWordCount);
- public class Statistics
- {
- private readonly Dictionary<string, int> nodeCounts = new();
- private int totalWordCount;
- private static int CountWords(string text)
- {
- var words = text.Split(new char[] { ' ', '\t', '\n' });
- return words.Count(x => !string.IsNullOrWhiteSpace(x));
- }
- public void RunStatistics(JToken token)
- {
- if (token.Type != JTokenType.String)
- return;
- var tokenName = AnalyzeJSON.GetNameFromPath(token.Path);
- if (!nodeCounts.ContainsKey(tokenName))
- nodeCounts.Add(tokenName, 0);
- var wordCount = CountWords(token.Value<string>());
- nodeCounts[tokenName] += wordCount;
- totalWordCount += wordCount;
- }
- public WordCountResult<Dictionary<string, int>> Result => new(nodeCounts, totalWordCount);
- }
- }
|