diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-14 20:56:25 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-14 20:56:25 +0000 |
commit | 6dc8400659bb0506cf99c351a069a1a73c88a619 (patch) | |
tree | bea1a93c4391dbddae936a077502342ad58e02af /chrome/browser/autocomplete/contact_provider_chromeos.cc | |
parent | dd8a3588142c286b63b4e668afca220e1e2b61af (diff) | |
download | chromium_src-6dc8400659bb0506cf99c351a069a1a73c88a619.zip chromium_src-6dc8400659bb0506cf99c351a069a1a73c88a619.tar.gz chromium_src-6dc8400659bb0506cf99c351a069a1a73c88a619.tar.bz2 |
autocomplete: Make ContactProvider rank contacts by affinity
This adds an 'affinity' field to the contacts::Contact
protocol message and makes the ContactProvider class use it
to order matches.
BUG=141877
TEST=none
Review URL: https://chromiumcodereview.appspot.com/10916304
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@156883 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete/contact_provider_chromeos.cc')
-rw-r--r-- | chrome/browser/autocomplete/contact_provider_chromeos.cc | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/chrome/browser/autocomplete/contact_provider_chromeos.cc b/chrome/browser/autocomplete/contact_provider_chromeos.cc index 2e7ff9a..2d1c489 100644 --- a/chrome/browser/autocomplete/contact_provider_chromeos.cc +++ b/chrome/browser/autocomplete/contact_provider_chromeos.cc @@ -5,6 +5,7 @@ #include "chrome/browser/autocomplete/contact_provider_chromeos.h" #include <algorithm> +#include <cmath> #include "base/i18n/break_iterator.h" #include "base/i18n/string_search.h" @@ -18,6 +19,17 @@ namespace { +// Default affinity assigned to contacts whose |affinity| field is unset. +// TODO(derat): Set this to something reasonable (probably 0.0) once we're +// getting affinity for contacts. +float kDefaultAffinity = 1.0; + +// Base match relevance assigned to a contact with an affinity of 0.0. +int kBaseRelevance = 1300; + +// Maximum boost to relevance for a contact with an affinity of 1.0. +int kAffinityRelevanceBoost = 200; + // Returns true if |word_to_find| is a prefix of |name_to_search| and marks the // matching text in |classifications| (which corresponds to the contact's full // name). |name_index_in_full_name| contains |name_to_search|'s index within @@ -56,13 +68,15 @@ struct ContactProvider::ContactData { ContactData(const string16& full_name, const string16& given_name, const string16& family_name, - const std::string& contact_id) + const std::string& contact_id, + float affinity) : full_name(full_name), given_name(given_name), family_name(family_name), given_name_index(string16::npos), family_name_index(string16::npos), - contact_id(contact_id) { + contact_id(contact_id), + affinity(affinity) { base::i18n::StringSearchIgnoringCaseAndAccents( given_name, full_name, &given_name_index, NULL); base::i18n::StringSearchIgnoringCaseAndAccents( @@ -80,6 +94,9 @@ struct ContactProvider::ContactData { // Unique ID used to look up additional contact information. std::string contact_id; + + // Affinity between the user and this contact, in the range [0.0, 1.0]. + float affinity; }; ContactProvider::ContactProvider( @@ -115,8 +132,11 @@ void ContactProvider::Start(const AutocompleteInput& input, } } + // |contacts_| is ordered by descending affinity. Since affinity is currently + // the only signal used for computing relevance, we can stop after we've found + // kMaxMatches results. for (ContactDataVector::const_iterator it = contacts_.begin(); - it != contacts_.end(); ++it) + it != contacts_.end() && matches_.size() < kMaxMatches; ++it) AddContactIfMatched(input, input_words, *it); } @@ -133,6 +153,12 @@ ContactProvider::~ContactProvider() { contact_manager_->RemoveObserver(this, profile_); } +// static +bool ContactProvider::CompareAffinity(const ContactData& a, + const ContactData& b) { + return a.affinity > b.affinity; +} + void ContactProvider::RefreshContacts() { if (!contact_manager_.get()) return; @@ -141,6 +167,7 @@ void ContactProvider::RefreshContacts() { contact_manager_->GetAllContacts(profile_); contacts_.clear(); + contacts_.reserve(contacts->size()); for (contacts::ContactPointers::const_iterator it = contacts->begin(); it != contacts->end(); ++it) { const contacts::Contact& contact = **it; @@ -150,13 +177,16 @@ void ContactProvider::RefreshContacts() { AutocompleteMatch::SanitizeString(UTF8ToUTF16(contact.given_name())); string16 family_name = AutocompleteMatch::SanitizeString(UTF8ToUTF16(contact.family_name())); + float affinity = + contact.has_affinity() ? contact.affinity() : kDefaultAffinity; if (!full_name.empty()) { contacts_.push_back( - ContactData( - full_name, given_name, family_name, contact.contact_id())); + ContactData(full_name, given_name, family_name, contact.contact_id(), + affinity)); } } + std::sort(contacts_.begin(), contacts_.end(), CompareAffinity); } void ContactProvider::AddContactIfMatched( @@ -200,8 +230,8 @@ AutocompleteMatch ContactProvider::CreateAutocompleteMatch( match.inline_autocomplete_offset = string16::npos; match.contents = contact.full_name; match.fill_into_edit = match.contents; - // TODO(derat): Implement ranking. - match.relevance = 1; + match.relevance = kBaseRelevance + + static_cast<int>(roundf(kAffinityRelevanceBoost * contact.affinity)); match.RecordAdditionalInfo(kMatchContactIdKey, contact.contact_id); return match; } |