示例#1
0
            public void ShouldReturnDamerauLevenshteinDistanceOf2()
            {
                var result = DamerauLevenshteinDistance.Calculate(InputRam, InputMom);

                Assert.AreEqual(2, result);
            }
        /// <summary>
        ///
        /// </summary>
        /// <param name="comparisons"></param>
        /// <param name="concFiles"></param>
        /// <returns></returns>
        private List <string> pairWithPk(IEnumerable <TreatmentComparison> comparisons, List <string> files)
        {
            var pairedFiles = Enumerable.Repeat <string>(null, comparisons.Count()).ToList();
            var distance    = new DamerauLevenshteinDistance();

            // Initialize scores array
            var scoresArray = comparisons.Select((c, i) => new { c, i })
                              .ToDictionary(row => row.i, row =>
            {
                var pkFilename = Path.GetFileNameWithoutExtension(row.c.PkFile.Path).ToLower();
                return(files.Select((f, j) => new { f, j })
                       .ToDictionary(col => col.j, col =>
                                     distance.Calculate(
                                         Path.GetFileNameWithoutExtension(col.f).ToLower(), pkFilename)
                                     ));
            });

            // Iteratively extract the best matches
            while (scoresArray.Any() && scoresArray.Count * scoresArray.First().Value.Count > 1)
            {
                // find the current best match
                int currentScore = int.MaxValue, pkId = -1, fId = -1;
                foreach (var row in scoresArray)
                {
                    int rowScore = int.MaxValue, rowfId = -1;
                    foreach (var col in row.Value)
                    {
                        if (col.Value < rowScore)
                        {
                            rowfId   = col.Key;
                            rowScore = col.Value;
                        }
                    }
                    if (rowScore < currentScore)
                    {
                        pkId         = row.Key;
                        fId          = rowfId;
                        currentScore = rowScore;
                    }
                }

                // Save selected match
                pairedFiles[pkId] = files[fId];

                // Clean scores array for next iteration
                scoresArray.Remove(pkId);
                foreach (var row in scoresArray)
                {
                    row.Value.Remove(fId);
                }
            }

            // if one match remaining
            if (scoresArray.Any())
            {
                pairedFiles[scoresArray.First().Key] =
                    files[scoresArray.First().Value.First().Key];
            }

            return(pairedFiles);
        }