System.StringHelper.Match C# (CSharp) 메소드

Match() 공개 정적인 메소드

模糊匹配
public static Match ( IEnumerable list, String keys, Func keySelector ) : Double>>.IEnumerable
list IEnumerable
keys String
keySelector Func
리턴 Double>>.IEnumerable
        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;
        }

Same methods

StringHelper::Match ( String str, String key, Int32 maxError ) : Int32>.KeyValuePair
StringHelper::Match ( IEnumerable list, String keys, Func keySelector, Int32 count, Double confidence = 0.5 ) : IEnumerable