public int[] Match(IReadOnlyList <string> pattern, IReadOnlyList <string> search) { if (search.Count < pattern.Count) { var rMatch = Match(search, pattern); var nMatch = new int[pattern.Count]; for (int i = 0; i < nMatch.Length; i++) { nMatch[i] = -1; } for (int i = 0; i < rMatch.Length; i++) { if (rMatch[i] >= 0) { nMatch[rMatch[i]] = i; } } return(nMatch); } if (pattern.Count == 0) { return(new int[0]); } if (pattern.Count == search.Count) { return(Enumerable.Range(0, pattern.Count).ToArray()); } var mm = new MatchMatrix(pattern, search, maxOffset); float bestScore = mm.Initialize(-maxOffset); int[] bestMatch = mm.Path(); while (mm.CanStepForward) { float score = mm.StepForward(); if (score > bestScore) { bestScore = score; bestMatch = mm.Path(); } } if (bestScore == 0) { return(Enumerable.Range(0, pattern.Count).ToArray()); } return(bestMatch); }
private int[] FindMatch(int loc, IReadOnlyList <string> wmContext, out float bestScore) { bestScore = MinScore; int[] bestMatch = null; var mmForward = new MatchMatrix(wmContext, wmLines, MaxMatchOffset); float score = mmForward.Initialize(loc); if (score >= bestScore) { bestScore = score; bestMatch = mmForward.Path(); } var mmReverse = new MatchMatrix(wmContext, wmLines, MaxMatchOffset); mmReverse.Initialize(loc); int warnDist = OffsetWarnDistance(wmContext.Count, textLines.Count); for (int i = 0; mmForward.CanStepForward || mmReverse.CanStepBackward; i++) { //within the warning range it's a straight up fight //past the warning range, quality is reduced by 10% per warning range float penalty = i < warnDist ? 0 : 0.1f * i / warnDist; score = mmForward.StepForward() - penalty; if (score > bestScore) { bestScore = score; bestMatch = mmForward.Path(); } score = mmReverse.StepBackward() - penalty; if (score > bestScore) { bestScore = score; bestMatch = mmReverse.Path(); } //aint getting any better than this if (bestScore + penalty > 1f) { break; } } return(bestMatch); }