2020-05-01 06:57:24 +00:00
|
|
|
// LICENSE
|
2020-03-25 09:24:43 +00:00
|
|
|
//
|
|
|
|
// This software is dual-licensed to the public domain and under the following
|
|
|
|
// license: you are granted a perpetual, irrevocable license to copy, modify,
|
|
|
|
// publish, and distribute this file as you see fit.
|
|
|
|
//
|
|
|
|
// VERSION
|
|
|
|
// 0.2.0 (2017-02-18) Scored matches perform exhaustive search for best score
|
|
|
|
// 0.1.0 (2016-03-28) Initial release
|
|
|
|
//
|
|
|
|
// AUTHOR
|
|
|
|
// Forrest Smith
|
|
|
|
//
|
|
|
|
// NOTES
|
|
|
|
// Compiling
|
|
|
|
// You MUST add '#define FTS_FUZZY_MATCH_IMPLEMENTATION' before including this header in ONE source file to create implementation.
|
|
|
|
//
|
|
|
|
// fuzzy_match_simple(...)
|
|
|
|
// Returns true if each character in pattern is found sequentially within str
|
|
|
|
//
|
|
|
|
// fuzzy_match(...)
|
|
|
|
// Returns true if pattern is found AND calculates a score.
|
|
|
|
// Performs exhaustive search via recursion to find all possible matches and match with highest score.
|
|
|
|
// Scores values have no intrinsic meaning. Possible score range is not normalized and varies with pattern.
|
|
|
|
// Recursion is limited internally (default=10) to prevent degenerate cases (pattern="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
2020-05-01 06:57:24 +00:00
|
|
|
// Uses uint8_t for match indices. Therefore patterns are limited to max_matches characters.
|
2020-03-25 09:24:43 +00:00
|
|
|
// Score system should be tuned for YOUR use case. Words, sentences, file names, or method names all prefer different tuning.
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef FTS_FUZZY_MATCH_H
|
|
|
|
#define FTS_FUZZY_MATCH_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdint> // uint8_t
|
|
|
|
#include <ctype.h> // ::tolower, ::toupper
|
|
|
|
#include <cstring> // memcpy
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
2020-05-03 13:45:47 +00:00
|
|
|
#include "../Utils/ASCIIFolding.hpp"
|
|
|
|
|
2020-03-25 09:24:43 +00:00
|
|
|
// Public interface
|
|
|
|
namespace fts {
|
2020-05-01 06:57:24 +00:00
|
|
|
using char_type = wchar_t;
|
|
|
|
using pos_type = uint16_t;
|
|
|
|
static constexpr pos_type stopper = pos_type(-1);
|
|
|
|
static constexpr int max_matches = 255;
|
2020-03-25 09:24:43 +00:00
|
|
|
|
2020-05-01 06:57:24 +00:00
|
|
|
static bool fuzzy_match(char_type const * pattern, char_type const * str, int & outScore);
|
|
|
|
static bool fuzzy_match(char_type const * pattern, char_type const * str, int & outScore, pos_type * matches);
|
|
|
|
}
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
#ifdef FTS_FUZZY_MATCH_IMPLEMENTATION
|
|
|
|
namespace fts {
|
|
|
|
|
|
|
|
// Forward declarations for "private" implementation
|
|
|
|
namespace fuzzy_internal {
|
2020-05-03 12:14:40 +00:00
|
|
|
static bool fuzzy_match_recursive(const char_type * pattern, const char_type * str, int & outScore, const char_type * const strBegin,
|
2020-05-01 06:57:24 +00:00
|
|
|
pos_type const * srcMatches, pos_type * newMatches, int nextMatch,
|
2020-03-25 09:24:43 +00:00
|
|
|
int & recursionCount, int recursionLimit);
|
2020-05-01 06:57:24 +00:00
|
|
|
static void copy_matches(pos_type * dst, pos_type const* src);
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Public interface
|
2020-05-01 06:57:24 +00:00
|
|
|
static bool fuzzy_match(char_type const * pattern, char_type const * str, int & outScore) {
|
|
|
|
pos_type matches[max_matches + 1]; // with the room for the stopper
|
|
|
|
matches[0] = stopper;
|
|
|
|
return fuzzy_match(pattern, str, outScore, matches);
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 06:57:24 +00:00
|
|
|
static bool fuzzy_match(char_type const * pattern, char_type const * str, int & outScore, pos_type * matches) {
|
2020-03-25 09:24:43 +00:00
|
|
|
int recursionCount = 0;
|
2020-05-03 12:14:40 +00:00
|
|
|
static constexpr int recursionLimit = 10;
|
2020-05-01 06:57:24 +00:00
|
|
|
return fuzzy_internal::fuzzy_match_recursive(pattern, str, outScore, str, nullptr, matches, 0, recursionCount, recursionLimit);
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private implementation
|
2020-05-01 06:57:24 +00:00
|
|
|
static bool fuzzy_internal::fuzzy_match_recursive(
|
|
|
|
// Pattern to match over str.
|
|
|
|
const char_type * pattern,
|
|
|
|
// Text to match the pattern over.
|
|
|
|
const char_type * str,
|
|
|
|
// Score of the pattern matching str. Output variable.
|
|
|
|
int & outScore,
|
2020-05-03 12:14:40 +00:00
|
|
|
// The very start of str, for calculating indices of matches and for calculating matches from the start of the input string.
|
|
|
|
const char_type * const strBegin,
|
2020-05-01 06:57:24 +00:00
|
|
|
// Matches when entering this function.
|
|
|
|
pos_type const * srcMatches,
|
|
|
|
// Output matches.
|
|
|
|
pos_type * matches,
|
|
|
|
// Number of matched characters stored in srcMatches when entering this function, also tracking the successive matches.
|
|
|
|
int nextMatch,
|
|
|
|
// Recursion count is input / output to track the maximum depth reached.
|
2020-05-03 12:14:40 +00:00
|
|
|
// Was given by reference &recursionCount, see discussion in https://github.com/forrestthewoods/lib_fts/issues/21
|
|
|
|
// int & recursionCount,
|
2020-05-04 14:18:20 +00:00
|
|
|
int recursionCount,
|
2020-05-01 06:57:24 +00:00
|
|
|
int recursionLimit)
|
2020-03-25 09:24:43 +00:00
|
|
|
{
|
|
|
|
// Count recursions
|
2020-05-01 06:57:24 +00:00
|
|
|
if (++ recursionCount >= recursionLimit)
|
2020-03-25 09:24:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Detect end of strings
|
|
|
|
if (*pattern == '\0' || *str == '\0')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Recursion params
|
|
|
|
bool recursiveMatch = false;
|
2020-05-01 06:57:24 +00:00
|
|
|
pos_type bestRecursiveMatches[max_matches + 1]; // with the room for the stopper
|
2020-03-25 09:24:43 +00:00
|
|
|
int bestRecursiveScore = 0;
|
|
|
|
|
|
|
|
// Loop through pattern and str looking for a match
|
|
|
|
bool first_match = true;
|
|
|
|
while (*pattern != '\0' && *str != '\0') {
|
2020-05-03 13:45:47 +00:00
|
|
|
|
|
|
|
int num_matched = std::tolower(*pattern) == std::tolower(*str) ? 1 : 0;
|
|
|
|
bool folded_match = false;
|
|
|
|
if (! num_matched) {
|
|
|
|
char tmp[4];
|
|
|
|
char *end = Slic3r::fold_to_ascii(*str, tmp);
|
|
|
|
char *c = tmp;
|
|
|
|
for (const wchar_t* d = pattern; c != end && *d != 0 && wchar_t(std::tolower(*c)) == std::tolower(*d); ++c, ++d);
|
|
|
|
if (c == end) {
|
|
|
|
folded_match = true;
|
|
|
|
num_matched = end - tmp;
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Found match
|
2020-05-03 13:45:47 +00:00
|
|
|
if (num_matched) {
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Supplied matches buffer was too short
|
2020-05-03 13:45:47 +00:00
|
|
|
if (nextMatch + num_matched > max_matches)
|
2020-03-25 09:24:43 +00:00
|
|
|
return false;
|
2020-05-03 13:45:47 +00:00
|
|
|
|
2020-03-25 09:24:43 +00:00
|
|
|
// "Copy-on-Write" srcMatches into matches
|
|
|
|
if (first_match && srcMatches) {
|
2020-05-01 06:57:24 +00:00
|
|
|
memcpy(matches, srcMatches, sizeof(pos_type) * (nextMatch + 1)); // including the stopper
|
2020-03-25 09:24:43 +00:00
|
|
|
first_match = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursive call that "skips" this match
|
2020-05-03 12:14:40 +00:00
|
|
|
pos_type recursiveMatches[max_matches + 1]; // with the room for the stopper
|
2020-03-25 09:24:43 +00:00
|
|
|
int recursiveScore;
|
2020-05-01 06:57:24 +00:00
|
|
|
if (fuzzy_match_recursive(pattern, str + 1, recursiveScore, strBegin, matches, recursiveMatches, nextMatch, recursionCount, recursionLimit)) {
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Pick best recursive score
|
|
|
|
if (!recursiveMatch || recursiveScore > bestRecursiveScore) {
|
2020-05-01 06:57:24 +00:00
|
|
|
copy_matches(bestRecursiveMatches, recursiveMatches);
|
|
|
|
bestRecursiveScore = recursiveScore;
|
2020-03-25 09:24:43 +00:00
|
|
|
}
|
|
|
|
recursiveMatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance
|
2020-05-01 06:57:24 +00:00
|
|
|
matches[nextMatch++] = (pos_type)(str - strBegin);
|
|
|
|
// Write a stopper sign.
|
|
|
|
matches[nextMatch] = stopper;
|
2020-05-03 13:45:47 +00:00
|
|
|
// Advance pattern by the number of matched characters (could be more if ASCII folding triggers in).
|
|
|
|
pattern += num_matched;
|
|
|
|
}
|
2020-03-25 09:24:43 +00:00
|
|
|
++str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if full pattern was matched
|
2020-05-03 12:14:40 +00:00
|
|
|
bool matched = *pattern == '\0';
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Calculate score
|
|
|
|
if (matched) {
|
2020-05-03 12:14:40 +00:00
|
|
|
static constexpr int sequential_bonus = 15; // bonus for adjacent matches
|
|
|
|
static constexpr int separator_bonus = 30; // bonus if match occurs after a separator
|
|
|
|
static constexpr int camel_bonus = 30; // bonus if match is uppercase and prev is lower
|
|
|
|
static constexpr int first_letter_bonus = 15; // bonus if the first letter is matched
|
2020-03-25 09:24:43 +00:00
|
|
|
|
2020-05-03 12:14:40 +00:00
|
|
|
static constexpr int leading_letter_penalty = -5; // penalty applied for every letter in str before the first match
|
|
|
|
static constexpr int max_leading_letter_penalty = -15; // maximum penalty for leading letters
|
|
|
|
static constexpr int unmatched_letter_penalty = -1; // penalty for every letter that doesn't matter
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Iterate str to end
|
|
|
|
while (*str != '\0')
|
|
|
|
++str;
|
|
|
|
|
|
|
|
// Initialize score
|
|
|
|
outScore = 100;
|
|
|
|
|
2020-05-03 12:14:40 +00:00
|
|
|
// Apply leading letter penalty or bonus.
|
|
|
|
outScore += matches[0] == 0 ?
|
|
|
|
first_letter_bonus :
|
|
|
|
std::max(matches[0] * leading_letter_penalty, max_leading_letter_penalty);
|
2020-03-25 09:24:43 +00:00
|
|
|
|
2020-05-03 13:45:47 +00:00
|
|
|
// Apply unmatched letters after the end penalty
|
|
|
|
// outScore += (int(str - strBegin) - matches[nextMatch-1] + 1) * unmatched_letter_penalty;
|
2020-03-25 09:24:43 +00:00
|
|
|
// Apply unmatched penalty
|
2020-05-03 13:45:47 +00:00
|
|
|
outScore += (int(str - strBegin) - nextMatch) * unmatched_letter_penalty;
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Apply ordering bonuses
|
|
|
|
for (int i = 0; i < nextMatch; ++i) {
|
2020-05-01 06:57:24 +00:00
|
|
|
pos_type currIdx = matches[i];
|
2020-03-25 09:24:43 +00:00
|
|
|
|
|
|
|
// Check for bonuses based on neighbor character value
|
|
|
|
if (currIdx > 0) {
|
2020-05-03 12:14:40 +00:00
|
|
|
if (i > 0 && currIdx == matches[i - 1] + 1)
|
|
|
|
// Sequential
|
|
|
|
outScore += sequential_bonus;
|
2020-05-04 14:18:20 +00:00
|
|
|
/*
|
2020-03-25 09:24:43 +00:00
|
|
|
// Camel case
|
2020-05-03 12:14:40 +00:00
|
|
|
char_type prev = strBegin[currIdx - 1];
|
|
|
|
if (std::islower(prev) && std::isupper(strBegin[currIdx]))
|
2020-03-25 09:24:43 +00:00
|
|
|
outScore += camel_bonus;
|
2020-05-04 14:18:20 +00:00
|
|
|
*/
|
2020-03-25 09:24:43 +00:00
|
|
|
// Separator
|
2020-05-03 12:14:40 +00:00
|
|
|
if (prev == '_' || prev == ' ')
|
2020-03-25 09:24:43 +00:00
|
|
|
outScore += separator_bonus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return best result
|
|
|
|
if (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {
|
|
|
|
// Recursive score is better than "this"
|
2020-05-01 06:57:24 +00:00
|
|
|
copy_matches(matches, bestRecursiveMatches);
|
2020-03-25 09:24:43 +00:00
|
|
|
outScore = bestRecursiveScore;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (matched) {
|
|
|
|
// "this" score is better than recursive
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no match
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 06:57:24 +00:00
|
|
|
|
|
|
|
// Copy matches up to a stopper.
|
|
|
|
static void fuzzy_internal::copy_matches(pos_type * dst, pos_type const* src)
|
|
|
|
{
|
|
|
|
while (*src != stopper)
|
|
|
|
*dst++ = *src++;
|
|
|
|
*dst = stopper;
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:24:43 +00:00
|
|
|
} // namespace fts
|
|
|
|
|
|
|
|
#endif // FTS_FUZZY_MATCH_IMPLEMENTATION
|
|
|
|
|
|
|
|
#endif // FTS_FUZZY_MATCH_H
|