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

Use APIv4 for contact lookup and improve probability calculation #448

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 26 additions & 17 deletions api/v3/BankingLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @package CiviBanking Extension
*/

use Civi\Api4\Contact;

/**
* Will provide a name based lookup for contacts. It is designed to take care of
Expand Down Expand Up @@ -292,37 +293,45 @@ function _civicrm_api3_banking_lookup_contactbyname_sql($name_mutations, $params
/**
* find some contacts via API
*/
function _civicrm_api3_banking_lookup_contactbyname_api($name_mutations, $params) {
$contacts_found = array();
// query quicksearch for each combination
function _civicrm_api3_banking_lookup_contactbyname_api($name_mutations, $params): array {
$contacts_probability = [];
$occurrence_count = [];
// query Contact.autocomplete for each combination
foreach ($name_mutations as $name_mutation) {
$result = civicrm_api3('Contact', 'getquick', array('name' => $name_mutation));
foreach ($result['values'] as $contact) {
$result = Contact::autocomplete()
->setInput($name_mutation)
->execute();
/** @phpstan-var array{id: int, label: string, icon: string, description: list<string>} $contact_autocomplete */
foreach ($result as $contact_autocomplete) {
$contact_id = $contact_autocomplete['id'];
$occurrence_count[$contact_id] = ($occurrence_count[$contact_id] ?? 0) + 1;
// get the current maximum similarity...
if (isset($contacts_found[$contact['id']])) {
$probability = $contacts_found[$contact['id']];
} else {
$probability = 0.0;
}
$probability = $contacts_probability[$contact_id] ?? 0.0;

// now, we'll have to find the maximum similarity with any of the name mutations
$compare_name = strtolower($contact['sort_name']);
$contact_name = strtolower($contact_autocomplete['label']);
foreach ($name_mutations as $name_mutation) {
$new_probability = 0.0;
similar_text(strtolower($name_mutation), $compare_name, $new_probability);
//error_log("Compare '$name_mutation' to '".$contact['sort_name']."' => $new_probability");
similar_text(strtolower($name_mutation), $contact_name, $new_probability);
$new_probability /= 100.0;
// square value for better distribution, multiply by 0.999 to avoid 100% match based on name
$new_probability = $new_probability * $new_probability * 0.999;
if ($new_probability > $probability) {
// square value for better distribution, multiply by 0.999 to avoid 100% match based on name
$probability = $new_probability * $new_probability * 0.999;
$probability = $new_probability;
}
}
$contacts_probability[$contact_id] = $probability;
}
}

$contacts_found[$contact['id']] = $probability;
if ([] !== $contacts_probability) {
$max_occurrence = max($occurrence_count);
foreach ($contacts_probability as $contact_id => $probability) {
$contacts_probability[$contact_id] = $probability * $occurrence_count[$contact_id] / $max_occurrence;
}
}

return $contacts_found;
return $contacts_probability;
}

/**
Expand Down