本文整理汇总了PHP中Strings::levenshteinQuality方法的典型用法代码示例。如果您正苦于以下问题:PHP Strings::levenshteinQuality方法的具体用法?PHP Strings::levenshteinQuality怎么用?PHP Strings::levenshteinQuality使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Strings
的用法示例。
在下文中一共展示了Strings::levenshteinQuality方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getTranslationMemoryResults
/**
* Return an array of search results from our Translation Memory API
* service with a quality index based on the levenshtein distance.
*
* @param array $entities The entities we want to analyse
* @param array $array_strings The strings to look into [locale1 strings, locale2 strings]
* @param string $search The string to search for
* @param int $max_results Optional, default to 200, the max number of results we return
* @param int $min_quality Optional, default to 0, The minimal quality index to filter result
* @return array An array of strings as [source => string, target => string, quality=> Levenshtein index]
*/
public static function getTranslationMemoryResults($entities, $array_strings, $search, $max_results = 200, $min_quality = 0)
{
$search_results = array_values(self::getTMXResults($entities, $array_strings));
$output = [];
foreach ($search_results as $set) {
// We only want results for which we have a translation
if ($set[1]) {
$quality = Strings::levenshteinQuality($search, $set[0]);
if ($quality >= $min_quality) {
$output[] = ['source' => $set[0], 'target' => $set[1], 'quality' => $quality];
}
}
}
// We sort by quality to get the best results first
usort($output, function ($a, $b) {
return $a['quality'] < $b['quality'];
});
if ($max_results > 0) {
array_splice($output, $max_results);
}
return $output;
}
示例2: getTranslationMemoryResults
/**
* Return an array of search results from our Translation Memory API
* service with a quality index based on the levenshtein distance.
*
* @param array $source_strings The source reference strings with entities as keys
* @param array $target_strings The target strings to look into with entities as keys
* @param string $search The string to search for
* @param int $max_results Optional, default to 200, the max number of results we return
* @param int $min_quality Optional, default to 0, The minimal quality index to filter result
* @return array An array of strings as [source => string, target => string, quality=> Levenshtein index]
*/
public static function getTranslationMemoryResults($source_strings, $target_strings, $search, $max_results = 200, $min_quality = 0)
{
$search_results = array_values(self::getTMXResults(array_keys($source_strings), [$source_strings, $target_strings]));
$output = [];
foreach ($search_results as $set) {
// We only want results for which we have a translation
if ($set[1]) {
$quality = round(Strings::levenshteinQuality($search, $set[0]), 2);
if ($quality >= $min_quality) {
$output[] = ['source' => $set[0], 'target' => $set[1], 'quality' => $quality];
}
}
}
// Remove duplicate results
$output = array_unique($output, SORT_REGULAR);
// We sort by quality to get the best results first
usort($output, function ($a, $b) {
return $a['quality'] < $b['quality'];
});
if ($max_results > 0) {
array_splice($output, $max_results);
}
return $output;
}
示例3: getTranslationMemoryResults
/**
* Return an array of search results from our Translation Memory API
* service with a quality index based on the levenshtein distance.
*
* @param array $strings The source and target strings to look into
* @param string $search The string to search for
* @param int $max_results Optional, default to 200, the max number of results we return
* @param int $min_quality Optional, default to 0, The minimal quality index to filter result
* @return array An array of strings as [source => string, target => string, quality=> Levenshtein index]
*/
public static function getTranslationMemoryResults($strings, $search, $max_results = 200, $min_quality = 0)
{
if (empty($strings)) {
return [];
}
/*
Here we prepare an output array with source and target strings plus
a quality index.
$set[0] is the source string (usually English) on which we
calculate a quality index based on the Levenshtein algorithm.
$set[1] is the target string, that is the language we want
translations from.
*/
foreach ($strings as $set) {
$quality = round(Strings::levenshteinQuality($search, $set[0]), 2);
if ($quality >= $min_quality) {
$output[] = ['source' => $set[0], 'target' => $set[1], 'quality' => $quality];
}
}
// Remove duplicate results
$output = array_unique($output, SORT_REGULAR);
// We sort by quality to get the best results first
usort($output, function ($a, $b) {
return $a['quality'] < $b['quality'];
});
if ($max_results > 0) {
array_splice($output, $max_results);
}
return $output;
}