## 高效比对,返回最短编辑距离算法匹配度最高的数据 转

深圳大道

``````#region 高效比对返回匹配度最高的数据
/// <summary>
/// 高效比对返回匹配度最高的数据
/// </summary>
/// <param name="sourceList">源数据</param>
/// <param name="targetList">目标数据</param>
/// <returns></returns>
public static List<MapToData> GetAutoMapData(List<MapToData> sourceList, List<MapToData> targetList)
{
#region 高效计算匹配
List<MapToData> resultList = new List<MapToData>();
Parallel.For(0, sourceList.Count, i =>
{
var sourceValue = sourceList[i].key;
foreach (var item in targetList)
{
var targetValue = item.key;
var jsonObject = item.value;
int matchNum = LevenshteinDistance(sourceValue, targetValue);
resultList.Add(new MapToData { key = sourceValue, value = jsonObject, match = matchNum });
}
});

var q = from p in resultList
where p != null
orderby p.match descending
group new { p.key, p.value, p.match } by p.key into g
select new MapToData
{
key = g.FirstOrDefault().key,
value = g.FirstOrDefault().value,
match = g.FirstOrDefault().match
};
return q.ToList();
#endregion
}
#endregion

#region LD最短编辑距离算法

/// <summary>
/// LD最短编辑距离算法
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="target">目标字符串</param>
/// <returns></returns>
public static int LevenshteinDistance(string source, string target)
{
int cell = source.Length;
int row = target.Length;
if (cell == 0)
{
return row;
}
if (row == 0)
{
return cell;
}
int[,] matrix = new int[row + 1, cell + 1];
for (var i = 0; i <= cell; i++)
{
matrix[0, i] = i;
}
for (var j = 1; j <= row; j++)
{
matrix[j, 0] = j;
}
var tmp = 0;
for (var k = 0; k < row; k++)
{
for (var l = 0; l < cell; l++)
{
if (source[l].Equals(target[k]))
tmp = 0;
else
tmp = 1;
matrix[k + 1, l + 1] = Math.Min(Math.Min(matrix[k, l] + tmp, matrix[k + 1, l] + 1), matrix[k, l + 1] + 1);
}
}
return matrix[row, cell];
}
#endregion``````

``````public class MapToData
{
/// <summary>
/// 要匹配的字符串
/// </summary>
public string key = "";

/// <summary>
/// 匹配的结果
/// </summary>
public object value = new object();
/// <summary>
/// 匹配度
/// </summary>
public int match = 0;
}``````

### 深圳大道

JavaScript 算法与数据结构 - javascript-algorithms

2018/05/31
944
0

2015/10/13
8.6K
1

Airship
2015/03/09
444
0

daniel-D
2013/08/07
0
0
JavaScript 算法与数据结构

a独家记忆
2018/06/08
0
0

Mybatis Plus删除

/** @author beth @data 2019-10-17 00:30 */ @RunWith(SpringRunner.class) @SpringBootTest public class DeleteTest { @Autowired private UserInfoMapper userInfoMapper; /** 根据id删除......

4
0

BobwithB

5
0
java内存模型

ls_cherish

4
0

5
0
js中实现页面跳转（返回前一页、后一页）

5
0