function test_gp_string_similarity() { $string1 = 'Word'; $string2 = 'Word!'; $string3 = 'Word'; $similarity = gp_string_similarity($string1, $string2); $similarity_2 = gp_string_similarity($string1, $string3); $this->assertEquals($similarity, 0.775); $this->assertEquals($similarity_2, 1); }
function closest_original($input, $other_strings) { if (empty($other_strings)) { return null; } $input_length = gp_strlen($input); $closest_similarity = 0; foreach ($other_strings as $compared_string) { $compared_string_length = gp_strlen($compared_string); $max_length_diff = apply_filters('gp_original_import_max_length_diff', 0.5); if (abs(($input_length - $compared_string_length) / $input_length) > $max_length_diff) { continue; } $similarity = gp_string_similarity($input, $compared_string); if ($similarity > $closest_similarity) { $closest = $compared_string; $closest_similarity = $similarity; } } if (!isset($closest)) { return null; } $min_score = apply_filters('gp_original_import_min_similarity_diff', 0.8); $close_enough = $closest_similarity > $min_score; do_action('post_string_similiary_test', $input, $closest, $closest_similarity, $close_enough); if ($close_enough) { return $closest; } else { return null; } }
public function closest_original($input, $other_strings) { if (empty($other_strings)) { return null; } $input_length = gp_strlen($input); $closest_similarity = 0; foreach ($other_strings as $compared_string) { $compared_string_length = gp_strlen($compared_string); /** * Filter the maximum length difference allowed when comparing originals for a close match when importing. * * @since 1.0.0 * * @param float $max_length_diff The times compared string length can differ from the input string. */ $max_length_diff = apply_filters('gp_original_import_max_length_diff', 0.5); if (abs(($input_length - $compared_string_length) / $input_length) > $max_length_diff) { continue; } $similarity = gp_string_similarity($input, $compared_string); if ($similarity > $closest_similarity) { $closest = $compared_string; $closest_similarity = $similarity; } } if (!isset($closest)) { return null; } /** * Filter the minimum allowed similarity to be considered as a close match. * * @since 1.0.0 * * @param float $similarity Minimum allowed similarity. */ $min_score = apply_filters('gp_original_import_min_similarity_diff', 0.8); $close_enough = $closest_similarity > $min_score; /** * Fires before determining string similarity. * * @since 1.0.0 * * @param string $input The original string to match against. * @param string $closest Closest matching string. * @param float $closest_similarity The similarity between strings that was calculated. * @param bool $close_enough Whether the closest was be determined as close enough match. */ do_action('gp_post_string_similiary_test', $input, $closest, $closest_similarity, $close_enough); if ($close_enough) { return $closest; } else { return null; } }