Skip to content

Commit

Permalink
Improve search for entities
Browse files Browse the repository at this point in the history
* Search also in entity names if search on full entity fails (useful for "perfect match")
* Don't display the table header if there are no results
  • Loading branch information
flodolo committed Jul 29, 2015
1 parent f3dabbb commit 4b38cac
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 18 deletions.
42 changes: 36 additions & 6 deletions app/classes/Transvision/ShowResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static function highlight($string, $locale = 'fr')
{
$replacements = [
' ' => '<span class="highlight-gray"> </span>',
'' => '<span class="highlight-gray">…</span>',
'' => '<span class="highlight-gray">…</span>',
];

switch ($locale) {
Expand Down Expand Up @@ -217,9 +217,9 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca

// Don't highlight search matches in entities when searching strings
if ($search_options['search_type'] == 'strings') {
$result_entity = ShowResults::formatEntity($key);
$result_entity = Self::formatEntity($key);
} else {
$result_entity = ShowResults::formatEntity($key, $recherche[0]);
$result_entity = Self::formatEntity($key, $recherche[0]);
}

$component = explode('/', $key)[0];
Expand Down Expand Up @@ -268,9 +268,9 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca
}

$replacements = [
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
'&hellip;' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
];

Expand Down Expand Up @@ -403,4 +403,34 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca

return $table;
}

/**
* Search entity names: search full entity IDs (including path and filename),
* then search entity names (without the full path) if there are no results.
*
* @param array $source_strings Array of source strings
* @param string $regex Regular expression to search entity names
*
* @return array List of matching entity names
*/
public static function searchEntities($source_strings, $regex)
{
// Search through the full entity ID
$entities = preg_grep($regex, array_keys($source_strings));

/* If there are no results, search also through the entity names.
* This is needed for "perfect match" when only the entity name is
* provided.
*/
if (empty($entities)) {
$entity_names = [];
foreach ($source_strings as $entity => $translation) {
$entity_names[$entity] = explode(':', $entity)[1];
}
$entities = preg_grep($regex, $entity_names);
$entities = array_keys($entities);
}

return $entities;
}
}
4 changes: 2 additions & 2 deletions app/models/api/repository_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$regex = '~' . $whole_word . trim('^' . preg_quote($initial_search, '~') . '$') .
$whole_word . '~' . $case_sensitive . 'u';
if ($request->parameters[2] == 'entities') {
$entities = preg_grep($regex, array_keys($source_strings));
$entities = ShowResults::searchEntities($source_strings, $regex);
$source_strings = array_intersect_key($source_strings, array_flip($entities));
} else {
$source_strings = preg_grep($regex, $source_strings);
Expand All @@ -33,7 +33,7 @@
$regex = '~' . $whole_word . preg_quote($word, '~') .
$whole_word . '~' . $case_sensitive . 'u';
if ($request->parameters[2] == 'entities') {
$entities = preg_grep($regex, array_keys($source_strings));
$entities = ShowResults::searchEntities($source_strings, $regex);
$source_strings = array_intersect_key($source_strings, array_flip($entities));
} else {
$source_strings = preg_grep($regex, $source_strings);
Expand Down
4 changes: 2 additions & 2 deletions app/models/mainsearch_entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
$extra_column_header = '';
}

// Display a search hint for the closest string we have if we have no search results
$entities = preg_grep($main_regex, array_keys($tmx_source));
$entities = ShowResults::searchEntities($tmx_source, $main_regex);

// Display a search hint for the closest string we have if we have no search results
if (count($entities) == 0) {
$merged_strings = [];

Expand Down
2 changes: 1 addition & 1 deletion app/models/mainsearch_strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

if ($check['search_type'] == 'strings_entities') {
$entities = preg_grep($main_regex, array_keys($tmx_source));
$entities = ShowResults::searchEntities($tmx_source, $main_regex);
foreach ($entities as $entity) {
$locale1_strings[$entity] = $tmx_source[$entity];
}
Expand Down
7 changes: 4 additions & 3 deletions app/views/results_entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<span class='celltitle'>Entity</span>
<a class='resultpermalink tag' id='{$anchor_name}' href='#{$anchor_name}' title='Permalink to this result'>link</a>
<a class='l10n tag' href='/string/?entity={$entity}&amp;repo={$check['repo']}' title='List all translations for this entity'>l10n</a>
<a class='link_to_entity' href='/{$entity_link}'>" . ShowResults::formatEntity($entity, $my_search) . "</a>
<a class='link_to_entity' href='/{$entity_link}'>" . ShowResults::formatEntity($entity, $initial_search) . "</a>
</td>
<td dir='{$direction1}'>
<span class='celltitle'>{$source_locale}</span>
Expand All @@ -109,5 +109,6 @@
}

$table .= "</table>\n\n";

print $table;
if ($entities) {
print $table;
}
2 changes: 1 addition & 1 deletion tests/functional/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
['v1/search/strings/central/en-US/fr/tralala/', 200, '[]'],
['v1/locales/central/', 200, '["ar","ast","cs","de","en-GB","en-US","eo","es-AR","es-CL","es-ES","es-MX","fa","fr","fy-NL","gl","he","hu","id","it","ja","ja-JP-mac","kk","ko","lt","lv","nb-NO","nl","nn-NO","pl","pt-BR","pt-PT","ru","sk","sl","sv-SE","th","tr","uk","vi","zh-CN","zh-TW"]'],
['v1/locales/iDontExist/', 400, '{"error":"The repo queried (iDontExist) doesn\'t exist."}'],
['v1/repositories/', 200, '["release","beta","aurora","central","gaia_1_3","gaia_1_4","gaia_2_0","gaia_2_1","gaia","mozilla_org"]'],
['v1/repositories/', 200, '["release","beta","aurora","central","gaia_1_3","gaia_1_4","gaia_2_0","gaia_2_1","gaia_2_2","gaia","mozilla_org"]'],
['v1/tm/central/en-US/fr/Bookmark/?max_results=3&min_quality=80', 200, '[{"source":"Bookmark","target":"Marquer cette page","quality":100},{"source":"Bookmark","target":"Marque-page","quality":100},{"source":"Bookmarks","target":"Marque-pages","quality":88.888888888889}]'],
['v1/versions/', 200, '{"v1":"stable"}'],
];
Expand Down
60 changes: 60 additions & 0 deletions tests/units/Transvision/ShowResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,64 @@ public function testGeRepositorySearchResults($a, $b, $c)
->array($obj->getRepositorySearchResults($a, $b))
->isIdenticalTo($c);
}

public function searchEntitiesDP()
{
return [
[
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
'browser/chrome/browser/migration/migration.properties:sourceNameIE1' => 'test2',
],
'~^browser/chrome/browser/migration/migration.properties:sourceNameIE$~i',
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE',
],
],
[
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
'browser/chrome/browser/migration/migration.properties:sourceNameIE1' => 'test2',
],
'~sourceNameIE~i',
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE',
'browser/chrome/browser/migration/migration.properties:sourceNameIE1',
],
],
[
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
'browser/chrome/browser/migration/migration.properties:sourceNameIE1' => 'test2',
],
'~^sourceNameIE$~i',
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE',
],
],
[
[
'browser/chrome/browser/migration/migration.properties:sourceNameIE' => 'test',
],
'~foobar~',
[],
],
[
[],
'~foobar~',
[],
],
];
}

/**
* @dataProvider searchEntitiesDP
*/
public function testSearchEntities($a, $b, $c)
{
$obj = new _ShowResults();
$this
->array($obj->searchEntities($a, $b))
->isEqualTo($c);
}
}
6 changes: 3 additions & 3 deletions tests/units/Transvision/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public function multipleStringReplaceDP()
return [
[
[
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
' ' => '<span class="highlight-gray" title="Non breakable space"> </span>', // nbsp highlight
'' => '<span class="highlight-red" title="Thin space"> </span>', // thin space highlight
'' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
'&hellip;' => '<span class="highlight-gray">…</span>', // right ellipsis highlight
],
'&hellip;  …',
Expand Down

0 comments on commit 4b38cac

Please sign in to comment.