|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace XtractQuery.Parsers |
| 8 | +{ |
| 9 | + public class FunctionDictionaryParser |
| 10 | + { |
| 11 | + public static Dictionary<int, string> ParseDictionary() |
| 12 | + { |
| 13 | + string filePath = "FunctionDictionary.json"; |
| 14 | + |
| 15 | + if (!File.Exists(filePath)) |
| 16 | + { |
| 17 | + Console.WriteLine("File not found: " + filePath); |
| 18 | + return new Dictionary<int, string>(); |
| 19 | + } |
| 20 | + |
| 21 | + string json = File.ReadAllText(filePath); |
| 22 | + var jsonData = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); |
| 23 | + |
| 24 | + var formattedDictionary = new Dictionary<int, string>(); |
| 25 | + |
| 26 | + foreach (var kvp in jsonData) |
| 27 | + { |
| 28 | + int subNumber = ExtractSubNumber(kvp.Key); |
| 29 | + if (subNumber != -1) |
| 30 | + { |
| 31 | + formattedDictionary[subNumber] = kvp.Value; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return formattedDictionary; |
| 36 | + } |
| 37 | + |
| 38 | + private static int ExtractSubNumber(string key) |
| 39 | + { |
| 40 | + var match = Regex.Match(key, @"sub(\d+)"); |
| 41 | + return match.Success && match.Groups.Count > 1 |
| 42 | + ? int.TryParse(match.Groups[1].Value, out int subNumber) ? subNumber : -1 |
| 43 | + : -1; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + public static void MergeIntoDictionary(IDictionary<int, string> targetDictionary) |
| 49 | + { |
| 50 | + var parsedDictionary = ParseDictionary(); |
| 51 | + |
| 52 | + foreach (var kvp in parsedDictionary) |
| 53 | + { |
| 54 | + targetDictionary[kvp.Key] = kvp.Value; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments