public static IEnumerable<KeyValuePair<T, Double>> Match<T>(this IEnumerable<T> list, String keys, Func<T, String> keySelector)
{
var rs = new List<KeyValuePair<T, Double>>();
if (list == null || !list.Any()) return rs;
if (keys.IsNullOrWhiteSpace()) return rs;
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
var ks = keys.Split(" ").OrderBy(_ => _.Length).ToArray();
// 计算每个项到关键字的权重
foreach (var item in list)
{
var name = keySelector(item);
if (name.IsNullOrEmpty()) continue;
var dist = ks.Sum(e =>
{
var kv = Match(name, e, e.Length);
return kv.Key - kv.Value * 0.5;
});
if (dist > 0)
{
var val = dist / keys.Length;
//var val = dist;
rs.Add(new KeyValuePair<T, Double>(item, val));
}
}
return rs;
}