-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathL-diversity_code.cs
174 lines (164 loc) · 5.84 KB
/
L-diversity_code.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace DataArmor
{
internal class L_diversity_code
{
public decimal L;
public List<string> QID = new List<string>();
public List<string> SA;
public DataTable data;
public List <DataTable> anonymizedDataTable;
public List<DataTable> not_satisfy_l=new List<DataTable>();
public List<DataTable> satisfy_l= new List<DataTable>();
public L_diversity_code(DataTable data, List<string> QID,decimal l, List<string>SA) {
this .data = data;
this .QID = QID;
this .SA = SA;
this.L = l;
Dictionary<string, string[]> expa_val = new Dictionary<string, string[] > ();
string[] e = { "", "" };
this.anonymizedDataTable = ClusterData(data,QID);
foreach (string s in this.SA) {
foreach (var p in this.anonymizedDataTable) {
if( LDiversityCheck(p, s))
{
satisfy_l.Add(p);
}
else
{
not_satisfy_l .Add(p);
foreach(DataRow dr in p.Rows)
{
if (!(p.AsEnumerable().Count(row => row[s].ToString() == dr[s].ToString()) == 1))
{
if (!expa_val.ContainsKey(dr[s].ToString()))
{
expa_val.Add(dr[s].ToString(), e);
}
}
}
}
}
}
string[] keysCopy = expa_val.Keys.ToArray();
foreach (string s in keysCopy)
{
string userInput = GetUserInput(s);
string[] expanding_values = userInput.Split(',');
expa_val[s] = expanding_values;
}
Expanding(expa_val);
satisfy_l.AddRange(not_satisfy_l);
this.data = mergeTable.MergeTables(satisfy_l);
}
public void Expanding(Dictionary<string, string[]> expa_val)
{
foreach (string s in SA)
{
foreach (DataTable dt in not_satisfy_l)
{
int i = 0;
foreach (DataRow dr in dt.Rows)
{
string val = dr[s].ToString();
if (expa_val.ContainsKey(val))
{
if (i >= expa_val[val].Length)
{
i = 0;
}
dr[s] = expa_val[val][i];
i++;
}
}
}
}
}
private string GetUserInput(string s)
{
string prompt = $"Enter Expanding values for {s} seperated by ,";
string expand="";
bool t = true;
do
{
if (!t)
{
prompt = $"Enter Proper Expanding Values for {s} (seperated by ,)";
}
expand = Microsoft.VisualBasic.Interaction.InputBox(prompt, "Expanding");
t=false;
} while (expand == ""|| expand.Split(',').Count()==1);
return expand;
}
public Boolean LDiversityCheck(DataTable partition, string sensitiveAttribute)
{
// Count distinct values of the sensitive attribute
int distinctValues = partition.AsEnumerable()
.Select(row => row[sensitiveAttribute])
.Distinct()
.Count();
if (distinctValues >= this.L)
{
return true;
}
else
{
return false;
}
}
// clustering
public List<DataTable> ClusterData(DataTable data, List<string> QID)
{
Dictionary<string, DataTable> clusters = new Dictionary<string, DataTable>();
foreach (DataRow row in data.Rows)
{
List<string> m = new List<string>();
foreach (string s in QID)
{
if (row[s].ToString() == "******")
{
continue;
}
else
{
m.Add(s);
}
}
string key = GenerateKey(row, m);
string[] l = key.Split(' ');
foreach (string k in clusters.Keys)
{
int c = 0;
foreach (string s in l)
{
if (k.Contains(s))
{
c++;
}
}
if (c == l.Length)
{
key = k;
break;
}
}
if (!clusters.ContainsKey(key))
{
DataTable cluster = data.Clone();
cluster.TableName = key;
clusters.Add(key, cluster);
}
clusters[key].ImportRow(row);
}
return new List<DataTable>(clusters.Values);
}
private string GenerateKey(DataRow row, List<string> QID)
{
string key = string.Join(" ", QID.ConvertAll(column => row[column].ToString()));
return key;
}
}
}