| | | 1 | | using System.Linq; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using SimMetrics.Net; |
| | | 4 | | using SimMetrics.Net.API; |
| | | 5 | | using SimMetrics.Net.Metric; |
| | | 6 | | using WireMock.Validation; |
| | | 7 | | |
| | | 8 | | namespace WireMock.Matchers |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// SimMetricsMatcher |
| | | 12 | | /// </summary> |
| | | 13 | | /// <seealso cref="IStringMatcher" /> |
| | | 14 | | public class SimMetricsMatcher : IStringMatcher |
| | | 15 | | { |
| | | 16 | | private readonly string[] _patterns; |
| | | 17 | | private readonly SimMetricType _simMetricType; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="pattern">The pattern.</param> |
| | | 23 | | /// <param name="simMetricType">The SimMetric Type</param> |
| | 8 | 24 | | public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : thi |
| | 8 | 25 | | { |
| | 8 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="patterns">The patterns.</param> |
| | | 32 | | /// <param name="simMetricType">The SimMetric Type</param> |
| | 8 | 33 | | public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) |
| | 8 | 34 | | { |
| | 8 | 35 | | Check.NotNullOrEmpty(patterns, nameof(patterns)); |
| | | 36 | | |
| | 8 | 37 | | _patterns = patterns; |
| | 8 | 38 | | _simMetricType = simMetricType; |
| | 8 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 42 | | public double IsMatch(string input) |
| | 4 | 43 | | { |
| | 4 | 44 | | IStringMetric m = GetStringMetricType(); |
| | | 45 | | |
| | 12 | 46 | | return MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input))); |
| | 4 | 47 | | } |
| | | 48 | | |
| | | 49 | | private IStringMetric GetStringMetricType() |
| | 4 | 50 | | { |
| | 4 | 51 | | switch (_simMetricType) |
| | | 52 | | { |
| | | 53 | | case SimMetricType.BlockDistance: |
| | 0 | 54 | | return new BlockDistance(); |
| | | 55 | | case SimMetricType.ChapmanLengthDeviation: |
| | 0 | 56 | | return new ChapmanLengthDeviation(); |
| | | 57 | | case SimMetricType.CosineSimilarity: |
| | 0 | 58 | | return new CosineSimilarity(); |
| | | 59 | | case SimMetricType.DiceSimilarity: |
| | 0 | 60 | | return new DiceSimilarity(); |
| | | 61 | | case SimMetricType.EuclideanDistance: |
| | 0 | 62 | | return new EuclideanDistance(); |
| | | 63 | | case SimMetricType.JaccardSimilarity: |
| | 0 | 64 | | return new JaccardSimilarity(); |
| | | 65 | | case SimMetricType.Jaro: |
| | 0 | 66 | | return new Jaro(); |
| | | 67 | | case SimMetricType.JaroWinkler: |
| | 0 | 68 | | return new JaroWinkler(); |
| | | 69 | | case SimMetricType.MatchingCoefficient: |
| | 0 | 70 | | return new MatchingCoefficient(); |
| | | 71 | | case SimMetricType.MongeElkan: |
| | 0 | 72 | | return new MongeElkan(); |
| | | 73 | | case SimMetricType.NeedlemanWunch: |
| | 0 | 74 | | return new NeedlemanWunch(); |
| | | 75 | | case SimMetricType.OverlapCoefficient: |
| | 0 | 76 | | return new OverlapCoefficient(); |
| | | 77 | | case SimMetricType.QGramsDistance: |
| | 0 | 78 | | return new QGramsDistance(); |
| | | 79 | | case SimMetricType.SmithWaterman: |
| | 0 | 80 | | return new SmithWaterman(); |
| | | 81 | | case SimMetricType.SmithWatermanGotoh: |
| | 0 | 82 | | return new SmithWatermanGotoh(); |
| | | 83 | | case SimMetricType.SmithWatermanGotohWindowedAffine: |
| | 0 | 84 | | return new SmithWatermanGotohWindowedAffine(); |
| | | 85 | | case SimMetricType.ChapmanMeanLength: |
| | 0 | 86 | | return new ChapmanMeanLength(); |
| | | 87 | | default: |
| | 4 | 88 | | return new Levenstein(); |
| | | 89 | | } |
| | 4 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 93 | | public string[] GetPatterns() |
| | 3 | 94 | | { |
| | 3 | 95 | | return _patterns; |
| | 3 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc cref="IMatcher.GetName"/> |
| | | 99 | | public string GetName() |
| | 1 | 100 | | { |
| | 1 | 101 | | return $"SimMetricsMatcher.{_simMetricType}"; |
| | 1 | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |