Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-171: levenshtein sorting Input v/s result labels (not description) #172

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions src/Controller/AuthAutocompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subje
if ($results && is_array($results)) {
// Cut the results to the desired number
// Easier than dealing with EACH API's custom return options
// Sort by levenstein
usort($results, fn($a, $b) => levenshtein($input, $a['label'] ?? '') <=> levenshtein($input, $b['label'] ?? ''));
if (count($results) > $count) {
$results = array_slice($results, 0, $count);
}
Expand Down Expand Up @@ -359,9 +361,9 @@ protected function wikidata($input) {
$json_error = json_last_error();
if ($json_error == JSON_ERROR_NONE) {
//WIKIdata will give is an success key will always return at least one, the query string
if (count($jsondata) > 1) {
if ($jsondata['success'] == 1) {
foreach ($jsondata['search'] as $key => $item) {
if (count($jsondata) > 0) {
if (($jsondata['success'] ?? 0) == 1) {
foreach (($jsondata['search'] ?? []) as $key => $item) {
$desc = (isset($item['description'])) ? '(' . $item['description'] . ')' : NULL;
$results[] = [
$label = empty($desc) ? $item['label'] : $item['label'] . ' ' . $desc,
Expand Down Expand Up @@ -496,11 +498,12 @@ protected function getty($input, $vocab = 'aat', $mode = 'fuzzy') {
$search_terms = str_replace($toremove, ' ', $clean_input);
$search_terms = array_map('trim', $search_terms);
$search_terms = array_filter($search_terms);
$original_search = $search_terms;
if (count($search_terms) > 0) {
$search_terms = strtolower(implode('* ', $search_terms));
$search_terms = $search_terms.'*'; //adds an extra * for the last term
}
$original_search = $search_terms;

$query_terms = <<<SPARQL
select distinct ?S ?T ?P ?Note {
?S a gvp:Concept; luc:term !searchterm; skos:inScheme <http://vocab.getty.edu/!vocab/>.
Expand Down Expand Up @@ -531,7 +534,6 @@ protected function getty($input, $vocab = 'aat', $mode = 'fuzzy') {
]);
}


$bodies = [];
$baseurl = 'http://vocab.getty.edu/sparql.json';
// I leave this as an array in case we want to combine modes in the future.
Expand Down Expand Up @@ -573,18 +575,35 @@ protected function getty($input, $vocab = 'aat', $mode = 'fuzzy') {
$json_error = json_last_error();
if ($json_error == JSON_ERROR_NONE) {
if (isset($jsondata['results']) && count($jsondata['results']['bindings']) > 0) {
if (is_array($original_search)) {
$original_search_string = implode(" ", $original_search);
}
else {
$original_search_string = $original_search;
}
foreach ($jsondata['results']['bindings'] as $key => $item) {
// We reapply original search because i had no luck with SPARQL binding the search for exact
// So we have no T
$term = isset($item['T']['value']) ? $item['T']['value'] : $original_search;
$term = isset($item['T']['value']) ? $item['T']['value'] : $original_search_string;
$parent = isset($item['P']['value']) ? ' | Parent of: ' . $item['P']['value'] : '';
$note = isset($item['Note']['value']) ? ' | (' . $item['Note']['value'] . ')' : '';
$uri = isset($item['S']['value']) ? $item['S']['value'] : '';
$results[] = [
'value' => $uri,
'label' => $term . $parent . $note,
'desc' => $parent . $note,
];
if ((strtolower(trim($term ?? '')) == strtolower($original_search_string)) ||
str_starts_with(strtolower(trim($term ?? '')), strtolower($original_search_string))
) {
array_unshift($results, [
'value' => $uri,
'label' => $term . $parent . $note,
'desc' => $parent . $note,
]);
}
else {
$results[] = [
'value' => $uri,
'label' => $term . $parent . $note,
'desc' => $parent . $note,
];
}
}
}
}
Expand Down Expand Up @@ -632,8 +651,7 @@ protected function viaf($input) {
$jsondata = json_decode($body, TRUE) ?? [];
$json_error = json_last_error();
if ($json_error == JSON_ERROR_NONE) {
//WIKIdata will give is an success key will always return at least one, the query string
if (count($jsondata) > 1) {
if (count($jsondata) > 0) {
if (isset($jsondata['result']) && is_array($jsondata['result']) && count($jsondata['result']) >= 1) {
foreach ($jsondata['result'] as $key => $item) {
$desc = (isset($item['nametype'])) ? '(' . $item['nametype'] . ')' : NULL;
Expand Down Expand Up @@ -933,7 +951,7 @@ protected function mesh($input, $vocab, $rdftype) {
'value' => $entry['resource'],
'label' => $entry['label'],
]);
}
}
else {
$results[] = [
'value' => $entry['resource'],
Expand Down