Ejemplo n.º 1
0
        public List <Person> GetAnonymizedData(IEnumerable <Person> people)
        {
            List <Person> anonymzedData;

            if (_propertyName == null)
            {
                var propertyInfo = Reflections.GetPropertyInfo(new Person(), _anonimizedExpression);
                anonymzedData = people.Select(p =>
                {
                    var clone = p.Clone();
                    propertyInfo.SetValue(clone, Convert.ChangeType("*", propertyInfo.PropertyType), null);
                    return(clone);
                })
                                .ToList();
            }
            else
            {
                anonymzedData = people.Select(p =>
                {
                    var clone = p.Clone();
                    clone.GetType().GetProperty(_propertyName)?.SetValue(clone, "*");
                    return(clone);
                })
                                .ToList();
            }

            return(anonymzedData);
        }
Ejemplo n.º 2
0
        private bool AreGroupAnonymzedMaximally(IEnumerable <PeopleGroup <string> > groups, int commonLength)
        {
            var result = groups.Any(g => g.Count < ParameterK ||
                                    g.People.Any(p =>
            {
                var propertyInfo = Reflections.GetPropertyInfo(p, _anonimizedExpression);
                return(propertyInfo.GetValue(p).ToString().Length < commonLength);
            })
                                    );

            return(result);
        }
        public List <Person> GetAnonymizedData(IEnumerable <Person> people)
        {
            if (people == null || !people.Any())
            {
                return(new List <Person>());
            }

            var groupsOrderedByLength = people.GroupBy(p =>
            {
                var propertyInfo = Reflections.GetPropertyInfo(p, _anonimizedExpression);
                return(propertyInfo.GetValue(p).ToString().Length);
            })
                                        .Select(gPeople => new
            {
                Value  = gPeople.Select(_anonimizedProperty).First().ToString().Length,
                People = gPeople.ToList(),
                Count  = gPeople.Count()
            })
                                        .OrderBy(p => p.Value);

            var result = new List <List <Person> >();
            var currentIntervalGroup = new List <Person>();

            foreach (var group in groupsOrderedByLength)
            {
                if (currentIntervalGroup.Count < ParameterK)
                {
                    currentIntervalGroup.AddRange(group.People);
                }
                else
                {
                    result.Add(currentIntervalGroup);
                    currentIntervalGroup = new List <Person>(group.People);
                }
            }
            //Handle last left group
            if (currentIntervalGroup.Count < ParameterK && result.Any())
            {
                result.Last().AddRange(currentIntervalGroup);
            }
            else
            {
                result.Add(currentIntervalGroup);
            }


            var flattenedResult = result.Select(GetPeopleWithAnnonymazedAgeRange).SelectMany(x => x).ToList();

            return(flattenedResult);
        }
Ejemplo n.º 4
0
        public List <Person> GetPeopleWithAnonymizedProperty(List <Person> people)
        {
            var max              = people.Select(_anonimizedProperty).Max(v => Convert.ToInt32(v));
            var min              = people.Select(_anonimizedProperty).Min(v => Convert.ToInt32(v));
            var propertyInfo     = Reflections.GetPropertyInfo(new Person(), _anonimizedExpression);
            var anonymizedPeople = people.Select(p =>
            {
                var clone           = p.Clone();
                var anonymizedValue = min == max ? min.ToString() : $"{min} - {max}";
                propertyInfo.SetValue(clone, Convert.ChangeType(anonymizedValue, propertyInfo.PropertyType), null);
                return(clone);
            })
                                   .ToList();

            return(anonymizedPeople);
        }
        public static Dictionary <string, int> CalculateNumberIdenticalLengthElements <T>(IEnumerable <Person> people, Expression <Func <Person, T> > selectedExpression)
        {
            var selectedProperty = selectedExpression.Compile();
            var groups           = people.GroupBy(p =>
            {
                var propertyInfo = Reflections.GetPropertyInfo(p, selectedExpression);
                return(propertyInfo.GetValue(p).ToString().Length);
            })
                                   .Select(gPeople => new
            {
                Value = gPeople.Select(selectedProperty).First().ToString().Length + " liter",
                Count = gPeople.Count()
            }).ToDictionary(item => item.Value, item => item.Count);

            return(groups);
        }
        public List <Person> GetPeopleWithAnnonymazedAgeRange(List <Person> people)
        {
            var max = people.Select(_anonimizedProperty).Max(x => x.ToString().Length);
            var min = people.Select(_anonimizedProperty).Min(x => x.ToString().Length);

            var propertyInfo = Reflections.GetPropertyInfo(new Person(), _anonimizedExpression);

            var anonymzedPeople = people.Select(p =>
            {
                var clone           = p.Clone();
                var anonymyzedValue = min == max ? min.ToString() : $"{min} - {max}";
                propertyInfo.SetValue(clone, Convert.ChangeType($"{anonymyzedValue} letters", propertyInfo.PropertyType), null);
                return(clone);
            })
                                  .ToList();

            return(anonymzedPeople);
        }
Ejemplo n.º 7
0
        private List <Person> GetPeopleWithAnnonymazedAgeRange(List <PeopleGroup <string> > groups)
        {
            var propertyInfo    = Reflections.GetPropertyInfo(new Person(), _anonimizedExpression);
            var anonymzedPeople = groups.SelectMany(g =>
            {
                var anonymzed = g.People.Select(p =>
                {
                    var clone           = p.Clone();
                    var anonymyzedValue = g.Value == "*" ? "*" : $"{g.Value}...";
                    propertyInfo.SetValue(clone, Convert.ChangeType(anonymyzedValue, propertyInfo.PropertyType), null);
                    return(clone);
                });
                return(anonymzed);
            })
                                  .ToList();

            return(anonymzedPeople);
        }
Ejemplo n.º 8
0
        private IEnumerable <PeopleGroup <string> > GetGroupedPeople(IEnumerable <PeopleGroup <string> > peopleGroup, int commonLength)
        {
            var groupsOrderdByFirstCharacter = peopleGroup.SelectMany(g => g.People.GroupBy(p =>
            {
                var propertyInfo = Reflections.GetPropertyInfo(p, _anonimizedExpression);
                return(propertyInfo.GetValue(p).ToString().Substring(0, commonLength));
            })
                                                                      .Select(gPeople => new PeopleGroup <string>(
                                                                                  people: gPeople.ToList(),
                                                                                  value: gPeople.Select(_anonimizedProperty).First().ToString().Substring(0, commonLength))
                                                                              ));

            if (AreGroupAnonymzedMaximally(groupsOrderdByFirstCharacter, commonLength + 1))
            {
                return(peopleGroup.Select(g => g));
            }

            return(GetGroupedPeople(groupsOrderdByFirstCharacter, commonLength + 1));
        }
Ejemplo n.º 9
0
        public List <Person> GetAnonymizedData(IEnumerable <Person> people)
        {
            if (people == null || !people.Any())
            {
                return(new List <Person>());
            }

            var groups = people.GroupBy(p =>
            {
                var propertyInfo = Reflections.GetPropertyInfo(p, _anonimizedExpression);
                return(propertyInfo.GetValue(p).ToString().Substring(0, 1));
            })
                         .SelectMany(gPeople =>
            {
                var group = new PeopleGroup <string>
                            (
                    people: gPeople.ToList(),
                    value: gPeople.Count() >= ParameterK
                            ? gPeople.Select(_anonimizedProperty).First().ToString().Substring(0, 1)
                            : "*"
                            );
                return(GetGroupedPeople(new List <PeopleGroup <string> > {
                    group
                }, 2));
            })
                         .OrderBy(p => p.Value)
                         .ToList();

            //There may be a case where number of people without common group (makred as *) is lower than ParamaterK
            if (IsUncommonGroupToSmall(groups))
            {
                groups = GetMergedGroups(groups).ToList();
            }

            var anonymyzedPeople = GetPeopleWithAnnonymazedAgeRange(groups.ToList());

            return(anonymyzedPeople);
        }