Пример #1
0
        public float CompareRecords(object obj1, object obj2, CompareDefinitionGroup compareDefinitionGroup, out string explainPlan)
        {
            if (obj1 == null)
            {
                throw new ArgumentNullException("obj1");
            }
            if (obj2 == null)
            {
                throw new ArgumentNullException("obj2");
            }

            var record1 = new Dictionary <string, string>();
            var record2 = new Dictionary <string, string>();

            if (compareDefinitionGroup == null)
            {
                // build a group from annotations
                compareDefinitionGroup = this.GenerateCompareDefinitionGroup(obj1);
            }

            // get values
            foreach (var compareDefinition in compareDefinitionGroup.CompareDefinitions)
            {
                foreach (var compareField in compareDefinition.CompareFields)
                {
                    var value1 = ReflectionUtil.GetPropertyValue(obj1, compareField.Name1);
                    record1.Add(compareField.Name1, value1.ToStringOrEmpty());

                    var value2 = ReflectionUtil.GetPropertyValue(obj2, compareField.Name2);
                    record2.Add(compareField.Name2, value2.ToStringOrEmpty());
                }
            }

            return(this.CompareRecords(record1, record2, compareDefinitionGroup, out explainPlan));
        }
Пример #2
0
        public void SaveCompareDefinitionGroup(string fileName, CompareDefinitionGroup compareDefinitionGroup)
        {
            var serializer = new XmlSerializerHelper <CompareDefinitionGroup>();

            serializer.FileName = fileName;
            serializer.Save(compareDefinitionGroup);
        }
Пример #3
0
        private CompareDefinitionGroup GenerateCompareDefinitionGroup(object obj1)
        {
            var compareDefinitionGroup = new CompareDefinitionGroup();

            // read atrribute on Class
            var matchingAttr = obj1.GetType().GetCustomAttributes(typeof(MatchingAttribute), false).FirstOrDefault() as MatchingAttribute;

            if (matchingAttr != null)
            {
                compareDefinitionGroup.Aggregator = (Aggregator)Activator.CreateInstance(matchingAttr.Aggregator);
            }

            foreach (var prop in obj1.GetType().GetProperties())
            {
                var fieldAttr = prop.GetCustomAttributes(typeof(MatchingFieldAttribute), false).FirstOrDefault() as MatchingFieldAttribute;
                if (fieldAttr != null)
                {
                    // CompareDefinition
                    var compareDef = compareDefinitionGroup.CompareDefinitions.FirstOrDefault(x => x.Name == fieldAttr.CompareDefinition);
                    if (compareDef == null)
                    {
                        compareDef = new CompareDefinition()
                        {
                            Name = fieldAttr.CompareDefinition
                        };

                        compareDefinitionGroup.CompareDefinitions.Add(compareDef);
                    }

                    // CompareField
                    var compareField = compareDef.CompareFields.FirstOrDefault(x => x.Name1 == prop.Name);
                    if (compareField == null)
                    {
                        compareField = new CompareField()
                        {
                            Name1         = prop.Name,
                            Name2         = prop.Name,
                            FuzzyComparer = (StringFuzzyComparer)Activator.CreateInstance(fieldAttr.FuzzyComparer)
                        };
                        compareDef.CompareFields.Add(compareField);
                    }
                }
            }
            return(compareDefinitionGroup);
        }
Пример #4
0
        private void buttonMatch_Click(object sender, EventArgs e)
        {
            string explainPlan = "";
            float  result      = 0.0f;

            if (this.chkToogleMode.Checked)
            {
                // Comparedefinition is extracted from the dataannotations on the properties
                result = MatchingService.Instance.CompareRecords(this.adr1, this.adr2, null, out explainPlan);
            }
            else
            {
                // create a Comparedefintion for the dynmaic data
                var defGroup = new CompareDefinitionGroup()
                {
                    Aggregator = new MaximumAggregator(),

                    CompareDefinitions = new List <CompareDefinition>()
                    {
                        new CompareDefinition()
                        {
                            Name          = "AddressDefinition",
                            Aggregator    = new AverageAggregator(),
                            CompareFields = new List <CompareField>()
                            {
                                new CompareField()
                                {
                                    Name1         = "Firstname",
                                    Name2         = "Firstname",
                                    FuzzyComparer = new NameComparer()
                                },

                                new CompareField()
                                {
                                    Name1         = "Surname",
                                    Name2         = "Surname",
                                    FuzzyComparer = new NameComparer()
                                },

                                new CompareField()
                                {
                                    Name1         = "Street",
                                    Name2         = "Street",
                                    FuzzyComparer = new DamerauLevenshteinDistance()
                                },

                                new CompareField()
                                {
                                    Name1         = "Zip",
                                    Name2         = "Zip",
                                    FuzzyComparer = new Identity()
                                },

                                new CompareField()
                                {
                                    Name1         = "City",
                                    Name2         = "City",
                                    FuzzyComparer = new CityComparer()
                                },

                                new CompareField()
                                {
                                    Name1         = "Phone",
                                    Name2         = "Phone",
                                    FuzzyComparer = new PhoneComparer()
                                }
                            }
                        }
                    }
                };

                var record1 = new Dictionary <string, string>()
                {
                    { "Firstname", this.adr1.Firstname },
                    { "Surname", this.adr1.Surname },
                    { "Street", this.adr1.Street },
                    { "Zip", this.adr1.Zip },
                    { "City", this.adr1.City },
                    { "Phone", this.adr1.Phone }
                };

                var record2 = new Dictionary <string, string>()
                {
                    { "Firstname", this.adr2.Firstname },
                    { "Surname", this.adr2.Surname },
                    { "Street", this.adr2.Street },
                    { "Zip", this.adr2.Zip },
                    { "City", this.adr2.City },
                    { "Phone", this.adr2.Phone }
                };

                result = MatchingService.Instance.CompareRecords(record1, record2, defGroup, out explainPlan);
            }

            this.txtSimiliarity.Text = result.ToString();
            this.txtExplainPlan.Text = explainPlan;
        }
Пример #5
0
        public float CompareRecords(DataTable table1, DataTable table2, CompareDefinitionGroup compareDefinitionGroup, out string explainPlan)
        {
            explainPlan = "";

            if (table1 == null)
            {
                throw new ArgumentNullException("table1");
            }
            if (table2 == null)
            {
                throw new ArgumentNullException("table2");
            }
            if (compareDefinitionGroup == null)
            {
                throw new ArgumentNullException("compareDefinitionGroup");
            }
            if (compareDefinitionGroup.CompareDefinitions == null)
            {
                throw new ArgumentNullException("compareDefinitionGroup.CompareDefinitions");
            }

            var   matchScores     = new List <float>();
            float finalMatchScore = 0;

            table1.Columns.Add("Key");
            table2.Columns.Add("Key");

            var compareAlgorithm = GenericFactory.GetInstance <CompareAlgorithm>("SortedNeighborHood");

            compareAlgorithm.Table1 = table1;
            compareAlgorithm.Table2 = table2;

            // no comparedefinition, generate a comparedefinition with all fields
            if (!compareDefinitionGroup.CompareDefinitions.Any())
            {
                var compareDefinition = this.GenerateDefaultCompareDefinition(table1, table2);
                compareDefinitionGroup.CompareDefinitions.Add(compareDefinition);
            }

            foreach (var compareDefinition in compareDefinitionGroup.CompareDefinitions)
            {
                compareAlgorithm.Matches.Clear();
                compareAlgorithm.PossibleMatches.Clear();

                compareAlgorithm.RowComparer.CompareDefinition = compareDefinition;

                compareAlgorithm.KeyFields1.Clear();
                compareAlgorithm.KeyFields1.Add("Key");

                compareAlgorithm.KeyFields2.Clear();
                compareAlgorithm.KeyFields2.Add("Key");

                compareAlgorithm.MatchLimit         = 0;
                compareAlgorithm.PossibleMatchLimit = 0;
                compareAlgorithm.Execute();

                var match = compareAlgorithm.Matches.First();

                explainPlan = explainPlan.Append("-----Group: " + compareDefinition.Name + "------");

                var explainations = compareAlgorithm.RowComparer.Explain(match.From.Value, match.To.Value);

                explainPlan = explainPlan.Append(string.Join(Environment.NewLine, explainations));
                explainPlan = explainPlan.Append("Result = " + match.Cost + " (" + compareDefinition.Aggregator.ToStringOrEmpty() + ")");
                explainPlan = explainPlan.Append("----------------------------------------");

                matchScores.Add((float)match.Cost);
            }

            finalMatchScore = compareDefinitionGroup.Aggregator.AggregatedSimilarity(matchScores.ToArray());
            explainPlan     = explainPlan.Append("final result = " + finalMatchScore + " (" + compareDefinitionGroup.Aggregator.ToStringOrEmpty() + ")");

            return(finalMatchScore);
        }
Пример #6
0
        public float CompareRecords(Dictionary <string, string> record1, Dictionary <string, string> record2, CompareDefinitionGroup compareDefinitionGroup, out string explainPlan)
        {
            if (record1 == null)
            {
                throw new ArgumentNullException("record1");
            }
            if (record2 == null)
            {
                throw new ArgumentNullException("record2");
            }

            var table1 = record1.ToDataTable();
            var table2 = record2.ToDataTable();

            return(this.CompareRecords(table1, table2, compareDefinitionGroup, out explainPlan));
        }
Пример #7
0
        private void BtnCheckDoublets_Click(object sender, RoutedEventArgs e)
        {
            // create a Comparedefintion for the dynmaic data
            var defGroup = new CompareDefinitionGroup()
            {
                Aggregator = new MaximumAggregator(),

                CompareDefinitions = new List <CompareDefinition>()
                {
                    new CompareDefinition()
                    {
                        Name          = "AddressDefinition",
                        Aggregator    = new AverageAggregator(),
                        CompareFields = new List <CompareField>()
                        {
                            new CompareField()
                            {
                                Name1         = "Firstname",
                                Name2         = "Firstname",
                                FuzzyComparer = new NameComparer()
                            },

                            new CompareField()
                            {
                                Name1         = "Surname",
                                Name2         = "Surname",
                                FuzzyComparer = new NameComparer()
                            },

                            new CompareField()
                            {
                                Name1         = "Street",
                                Name2         = "Street",
                                FuzzyComparer = new DamerauLevenshteinDistance()
                            },

                            new CompareField()
                            {
                                Name1         = "Zip",
                                Name2         = "Zip",
                                FuzzyComparer = new Identity()
                            },

                            new CompareField()
                            {
                                Name1         = "City",
                                Name2         = "City",
                                FuzzyComparer = new CityComparer()
                            },

                            new CompareField()
                            {
                                Name1         = "Phone",
                                Name2         = "Phone",
                                FuzzyComparer = new PhoneComparer()
                            }
                        }
                    }
                }
            };

            var record1 = new Dictionary <string, string>()
            {
                { "Firstname", this.adr1.Firstname },
                { "Surname", this.adr1.Surname },
                { "Street", this.adr1.Street },
                { "Zip", this.adr1.Zip },
                { "City", this.adr1.City },
                { "Phone", this.adr1.Phone }
            };

            var record2 = new Dictionary <string, string>()
            {
                { "Firstname", this.adr2.Firstname },
                { "Surname", this.adr2.Surname },
                { "Street", this.adr2.Street },
                { "Zip", this.adr2.Zip },
                { "City", this.adr2.City },
                { "Phone", this.adr2.Phone }
            };

            this.txtResult.Text = "";

            string sessionKey = this.matchingWebServiceClient.CreateSession("nobody", "123456");

            if (!string.IsNullOrEmpty(sessionKey))
            {
                var result = this.matchingWebServiceClient.CompareRecords(sessionKey, record1, record2, defGroup);
                this.matchingWebServiceClient.CloseSession(sessionKey);

                this.txtResult.Text = this.adr1.ToString() + Environment.NewLine +
                                      Environment.NewLine +
                                      this.adr2.ToString() + Environment.NewLine +
                                      "Result=" + result;
            }
            else
            {
                this.txtResult.Text = "No session.";
            }
        }
Пример #8
0
        public float CompareRecords(string sessionKey, Dictionary <string, string> record1, Dictionary <string, string> record2, CompareDefinitionGroup compareDefinitionGroup)
        {
            string errorMsg = Authenticator.Instance.ValidateSession(sessionKey);

            if (!string.IsNullOrEmpty(errorMsg))
            {
                return(-1);
            }

            string explainPlan = "";

            return(MatchingService.Instance.CompareRecords(record1.CastToGenericDictionary(),
                                                           record2.CastToGenericDictionary(),
                                                           compareDefinitionGroup, out explainPlan));
        }