| | | 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 | | /// <inheritdoc cref="IMatcher.MatchBehaviour"/> |
| | 4 | 20 | | public MatchBehaviour MatchBehaviour { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="pattern">The pattern.</param> |
| | | 26 | | /// <param name="simMetricType">The SimMetric Type</param> |
| | 5 | 27 | | public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : thi |
| | 5 | 28 | | { |
| | 5 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 35 | | /// <param name="pattern">The pattern.</param> |
| | | 36 | | /// <param name="simMetricType">The SimMetric Type</param> |
| | 3 | 37 | | public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, SimMetricType simMetricType = |
| | 3 | 38 | | { |
| | 3 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="patterns">The patterns.</param> |
| | | 45 | | /// <param name="simMetricType">The SimMetric Type</param> |
| | 5 | 46 | | public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : |
| | 5 | 47 | | { |
| | 5 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 54 | | /// <param name="patterns">The patterns.</param> |
| | | 55 | | /// <param name="simMetricType">The SimMetric Type</param> |
| | 8 | 56 | | public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, SimMetricType simMetricType |
| | 8 | 57 | | { |
| | 8 | 58 | | Check.NotNullOrEmpty(patterns, nameof(patterns)); |
| | | 59 | | |
| | 8 | 60 | | MatchBehaviour = matchBehaviour; |
| | 8 | 61 | | _patterns = patterns; |
| | 8 | 62 | | _simMetricType = simMetricType; |
| | 8 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 66 | | public double IsMatch(string input) |
| | 4 | 67 | | { |
| | 4 | 68 | | IStringMetric m = GetStringMetricType(); |
| | | 69 | | |
| | 12 | 70 | | return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => m.GetSimilarit |
| | 4 | 71 | | } |
| | | 72 | | |
| | | 73 | | private IStringMetric GetStringMetricType() |
| | 4 | 74 | | { |
| | 4 | 75 | | switch (_simMetricType) |
| | | 76 | | { |
| | | 77 | | case SimMetricType.BlockDistance: |
| | 0 | 78 | | return new BlockDistance(); |
| | | 79 | | case SimMetricType.ChapmanLengthDeviation: |
| | 0 | 80 | | return new ChapmanLengthDeviation(); |
| | | 81 | | case SimMetricType.CosineSimilarity: |
| | 0 | 82 | | return new CosineSimilarity(); |
| | | 83 | | case SimMetricType.DiceSimilarity: |
| | 0 | 84 | | return new DiceSimilarity(); |
| | | 85 | | case SimMetricType.EuclideanDistance: |
| | 0 | 86 | | return new EuclideanDistance(); |
| | | 87 | | case SimMetricType.JaccardSimilarity: |
| | 0 | 88 | | return new JaccardSimilarity(); |
| | | 89 | | case SimMetricType.Jaro: |
| | 0 | 90 | | return new Jaro(); |
| | | 91 | | case SimMetricType.JaroWinkler: |
| | 0 | 92 | | return new JaroWinkler(); |
| | | 93 | | case SimMetricType.MatchingCoefficient: |
| | 0 | 94 | | return new MatchingCoefficient(); |
| | | 95 | | case SimMetricType.MongeElkan: |
| | 0 | 96 | | return new MongeElkan(); |
| | | 97 | | case SimMetricType.NeedlemanWunch: |
| | 0 | 98 | | return new NeedlemanWunch(); |
| | | 99 | | case SimMetricType.OverlapCoefficient: |
| | 0 | 100 | | return new OverlapCoefficient(); |
| | | 101 | | case SimMetricType.QGramsDistance: |
| | 0 | 102 | | return new QGramsDistance(); |
| | | 103 | | case SimMetricType.SmithWaterman: |
| | 0 | 104 | | return new SmithWaterman(); |
| | | 105 | | case SimMetricType.SmithWatermanGotoh: |
| | 0 | 106 | | return new SmithWatermanGotoh(); |
| | | 107 | | case SimMetricType.SmithWatermanGotohWindowedAffine: |
| | 0 | 108 | | return new SmithWatermanGotohWindowedAffine(); |
| | | 109 | | case SimMetricType.ChapmanMeanLength: |
| | 0 | 110 | | return new ChapmanMeanLength(); |
| | | 111 | | default: |
| | 4 | 112 | | return new Levenstein(); |
| | | 113 | | } |
| | 4 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 117 | | public string[] GetPatterns() |
| | 3 | 118 | | { |
| | 3 | 119 | | return _patterns; |
| | 3 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <inheritdoc cref="IMatcher.Name"/> |
| | 1 | 123 | | public string Name => $"SimMetricsMatcher.{_simMetricType}"; |
| | | 124 | | } |
| | | 125 | | } |