Плагин кластеризации данных
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | using System; using System.Collections.Generic; using System.Text; using System.Data; using DynamicPluginData; using System.Text.RegularExpressions; namespace DatacolDynamicPluginNS { public class DynamicPluginClass { static Dictionary< string , List< string >> ClasterizationRules = new Dictionary< string , List< string >>(); static Dictionary< string , Dictionary< string , double >> SumsForClasters = new Dictionary< string , Dictionary< string , double >>(); static Dictionary< string , int > ClasterCounts = new Dictionary< string , int >(); static List< string > FieldsToAverage = new List< string >(); //{"param1","param2"} static bool CalculateSumsAndAverages = true ; public static DataTable preExportData(DataTable dataTable, ItemInfo itemInfo, GlobalInfo globalInfo) { dataTable.Columns.Add( "claster" ); ClasterizationRules.Add( "Some Claster Name" , new List< string > { "token1" , "token2" } ); /* ClasterizationRules.Add ( "", new List<string> { "","" } ); */ foreach ( string Claster in ClasterizationRules.Keys) { ClasterCounts.Add(Claster, 0); } for ( int i = 0; i < dataTable.Rows.Count; i++) { dataTable.Rows[i][ "claster" ] = String.Join( "," , getClasters(dataTable.Rows[i][0].ToString()).ToArray()); } if (CalculateSumsAndAverages) { dataTable.Columns.Add( "count" ); for ( int i = 0; i < dataTable.Rows.Count; i++) { if (String.IsNullOrEmpty(dataTable.Rows[i][ "claster" ].ToString())) continue ; foreach (DataColumn dc in dataTable.Columns) { addToSumsAveragesDictionary(dataTable.Rows[i][ "claster" ].ToString(), dc.ColumnName, dataTable.Rows[i][dc.ColumnName].ToString()); } } DataRow r = dataTable.NewRow(); r[0] = "Total report on sums, averages and counts for clasters (added by Datacol)" ; dataTable.Rows.Add(dataTable.NewRow()); dataTable.Rows.Add(r); foreach ( string Claster in SumsForClasters.Keys) { r = dataTable.NewRow(); r[ "claster" ] = Claster; if (ClasterCounts.ContainsKey(Claster)) r[ "count" ] = ClasterCounts[Claster]; foreach ( string Parameter in SumsForClasters[Claster].Keys) { if (ClasterCounts.ContainsKey(Claster) && ClasterCounts[Claster] > 0) { if (FieldsToAverage.Contains(Parameter)) r[Parameter] = (Math.Round(SumsForClasters[Claster][Parameter] / ClasterCounts[Claster])).ToString(); else r[Parameter] = SumsForClasters[Claster][Parameter].ToString(); } } dataTable.Rows.Add(r); } } return dataTable; } static void addToSumsAveragesDictionary( string Claster, string DataFieldName, string Value) { double ValueDouble = 0; if (!Double.TryParse(Value, out ValueDouble)) return ; if (!SumsForClasters.ContainsKey(Claster)) { SumsForClasters.Add(Claster, new Dictionary< string , double >()); } if (!SumsForClasters[Claster].ContainsKey(DataFieldName)) { SumsForClasters[Claster].Add(DataFieldName, 0); } SumsForClasters[Claster][DataFieldName] += ValueDouble; } static List< string > getClasters( string str) { List< string > Clasters = new List< string >(); foreach ( string Claster in ClasterizationRules.Keys) { bool found = false ; foreach ( string Token in ClasterizationRules[Claster]) { try { if (Regex.IsMatch(str, Token, RegexOptions.Singleline | RegexOptions.IgnoreCase)) { ClasterCounts[Claster]++; Clasters.Add(Claster); found = true ; break ; } } catch {} } if (found) break ; } return Clasters; } } } |