public static IEnumerable <SimMetricResult> GetSimilarities(this string first, IEnumerable <string> second, SimMetricType simMetricType = SimMetricType.Levenstein, bool convertToPercentage = false) { var list = new List <SimMetricResult>(); foreach (var item in second) { var rank = first.GetSimilarity(item, simMetricType); list.Add(new SimMetricResult { Item = item, Score = convertToPercentage.GetPercentageIf(rank) }); } return(list); }
public static IEnumerable <string> ContainsFuzzy(this IEnumerable <string> source, string search, double threshold = 0.7, SimMetricType simMetricType = SimMetricType.Levenstein) { foreach (var item in source) { var status = item.ContainsFuzzy(search, threshold, simMetricType); if (status) { yield return(item); } } }
public static SimMetricResult GetMinSimilarityInfo(this string first, IEnumerable <string> second, SimMetricType simMetricType = SimMetricType.Levenstein, bool convertToPercentage = false) { var list = new List <SimMetricResult>(); foreach (var item in second) { var rank = first.GetSimilarity(item, simMetricType); list.Add(new SimMetricResult { Item = item, Score = convertToPercentage.GetPercentageIf(rank) }); } var result = list.OrderBy(x => x.Score).First(); return(result); }
private static double ApproximatelyEquals(this string firstWord, string secondWord, SimMetricType simMetricType = SimMetricType.Levenstein) { switch (simMetricType) { case SimMetricType.BlockDistance: var sim2 = new BlockDistance(); return(sim2.GetSimilarity(firstWord, secondWord)); case SimMetricType.ChapmanLengthDeviation: var sim3 = new ChapmanLengthDeviation(); return(sim3.GetSimilarity(firstWord, secondWord)); case SimMetricType.CosineSimilarity: var sim4 = new CosineSimilarity(); return(sim4.GetSimilarity(firstWord, secondWord)); case SimMetricType.DiceSimilarity: var sim5 = new DiceSimilarity(); return(sim5.GetSimilarity(firstWord, secondWord)); case SimMetricType.EuclideanDistance: var sim6 = new EuclideanDistance(); return(sim6.GetSimilarity(firstWord, secondWord)); case SimMetricType.JaccardSimilarity: var sim7 = new JaccardSimilarity(); return(sim7.GetSimilarity(firstWord, secondWord)); case SimMetricType.Jaro: var sim8 = new Jaro(); return(sim8.GetSimilarity(firstWord, secondWord)); case SimMetricType.JaroWinkler: var sim9 = new JaroWinkler(); return(sim9.GetSimilarity(firstWord, secondWord)); case SimMetricType.MatchingCoefficient: var sim10 = new MatchingCoefficient(); return(sim10.GetSimilarity(firstWord, secondWord)); case SimMetricType.MongeElkan: var sim11 = new MongeElkan(); return(sim11.GetSimilarity(firstWord, secondWord)); case SimMetricType.NeedlemanWunch: var sim12 = new NeedlemanWunch(); return(sim12.GetSimilarity(firstWord, secondWord)); case SimMetricType.OverlapCoefficient: var sim13 = new OverlapCoefficient(); return(sim13.GetSimilarity(firstWord, secondWord)); case SimMetricType.QGramsDistance: var sim14 = new QGramsDistance(); return(sim14.GetSimilarity(firstWord, secondWord)); case SimMetricType.SmithWaterman: var sim15 = new SmithWaterman(); return(sim15.GetSimilarity(firstWord, secondWord)); case SimMetricType.SmithWatermanGotoh: var sim16 = new SmithWatermanGotoh(); return(sim16.GetSimilarity(firstWord, secondWord)); case SimMetricType.SmithWatermanGotohWindowedAffine: var sim17 = new SmithWatermanGotohWindowedAffine(); return(sim17.GetSimilarity(firstWord, secondWord)); case SimMetricType.ChapmanMeanLength: var sim18 = new ChapmanMeanLength(); return(sim18.GetSimilarity(firstWord, secondWord)); default: var sim1 = new Levenstein(); return(sim1.GetSimilarity(firstWord, secondWord)); } }
public static double Similarity(this string source, string target, bool ignoreCase = false, SimMetricType simMetricType = SimMetricType.Levenstein) { source = ignoreCase ? source.ToLowerInvariant() : source; target = ignoreCase ? target.ToLowerInvariant() : target; return(source.ApproximatelyEquals(target, simMetricType)); }
public IMatcher Map([CanBeNull] MatcherModel matcher) { if (matcher == null) { return(null); } string[] parts = matcher.Name.Split('.'); string matcherName = parts[0]; string matcherType = parts.Length > 1 ? parts[1] : null; string[] stringPatterns = matcher.Patterns != null?matcher.Patterns.OfType <string>().ToArray() : new[] { matcher.Pattern as string }; MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch; switch (matcherName) { case "CSharpCodeMatcher": if (_settings.AllowCSharpCodeMatcher == true) { return(new CSharpCodeMatcher(matchBehaviour, stringPatterns)); } throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because IWireMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'."); case "LinqMatcher": return(new LinqMatcher(matchBehaviour, stringPatterns)); case "ExactMatcher": return(new ExactMatcher(matchBehaviour, stringPatterns)); case "ExactObjectMatcher": return(CreateExactObjectMatcher(matchBehaviour, stringPatterns[0])); case "RegexMatcher": return(new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true)); case "JsonMatcher": return(new JsonMatcher(matchBehaviour, matcher.Pattern, matcher.IgnoreCase == true)); case "JsonPathMatcher": return(new JsonPathMatcher(matchBehaviour, stringPatterns)); case "JmesPathMatcher": return(new JmesPathMatcher(matchBehaviour, stringPatterns)); case "XPathMatcher": return(new XPathMatcher(matchBehaviour, stringPatterns)); case "WildcardMatcher": return(new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true)); case "ContentTypeMatcher": return(new ContentTypeMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true)); case "SimMetricsMatcher": SimMetricType type = SimMetricType.Levenstein; if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type)) { throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported."); } return(new SimMetricsMatcher(matchBehaviour, stringPatterns, type)); default: throw new NotSupportedException($"Matcher '{matcherName}' is not supported."); } }