Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
add transliteration support in search requests #13
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost committed Oct 27, 2023
1 parent 57332b1 commit ca92db8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ See also: [SQLite tree](https://github.com/YGGverse/YGGo/tree/sqliteway)
* Engine sources [MIT License](https://github.com/YGGverse/YGGo/blob/main/LICENSE)
* Home page animation by [alvarotrigo](https://codepen.io/alvarotrigo/pen/GRvYNax)
* CLI logo by [patorjk.com](https://patorjk.com/software/taag/#p=display&f=Slant&t=YGGo!)
* Transliteration by [php-translit](https://github.com/ashtokalo/php-translit)
* Identicons by [jdenticon](https://github.com/dmester/jdenticon-php)

#### Feedback
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"yggverse/cache": ">=0.3.0",
"symfony/dom-crawler": "^6.3",
"symfony/css-selector": "^6.3",
"jdenticon/jdenticon": "^1.0"
"jdenticon/jdenticon": "^1.0",
"ashtokalo/php-translit": "^0.2.0"
},
"license": "MIT",
"autoload": {
Expand Down
62 changes: 61 additions & 1 deletion src/library/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ static public function searchQuery(string $query, string $mode = 'default') {

if (mb_strlen($word) > 1) {

$words[] = sprintf('(%s*)', $word);
$words[] = sprintf('%s*', $word);

foreach (self::_generateWordForms($word) as $wordForm)
{
$words[] = sprintf('(*%s*)', $wordForm);
}
}
}

Expand All @@ -132,4 +137,59 @@ static public function plural(int $number, array $texts) {

return $texts[(($number % 100) > 4 && ($number % 100) < 20) ? 2 : $cases[min($number % 10, 5)]];
}

// Word forms generator to improve search results
// e.g. transliteration rules for latin filenames
private static function _generateWordForms(
string $keyword,
// #13 supported locales:
// https://github.com/ashtokalo/php-translit
array $transliteration = [
'be',
'bg',
'el',
'hy',
'kk',
'mk',
'ru',
'ka',
'uk'
],
// Additional char forms
array $charForms =
[
'c' => 'k',
'k' => 'c',
]
): array
{
$wordForms = [];

// Apply transliteration
foreach ($transliteration as $locale)
{
$wordForms[] = \ashtokalo\translit\Translit::object()->convert(
$keyword,
$locale
);
}

// Apply char forms
foreach ($wordForms as $wordForm)
{
foreach ($charForms as $from => $to)
{
$wordForms[] = str_replace(
$from,
$to,
$wordForm
);
}
}

// Remove duplicates
return array_unique(
$wordForms
);
}
}

0 comments on commit ca92db8

Please sign in to comment.