using System.Collections.Generic; using Newtonsoft.Json.Linq; using System.Linq; namespace analyzeJSON { public record WordCountResult(T NodeCounts, int TotalWordCount); public class Statistics { private readonly Dictionary 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()); nodeCounts[tokenName] += wordCount; totalWordCount += wordCount; } public WordCountResult> Result => new(nodeCounts, totalWordCount); } }