For "foo" vs "Foo", the all-lowercase one wins

This commit is contained in:
Strahinja Val Markovic 2012-04-16 22:13:05 -07:00
parent 9019a6f827
commit fedfcb8e44
5 changed files with 34 additions and 8 deletions

View File

@ -18,6 +18,10 @@
#include "standard.h"
#include "Candidate.h"
#include <cctype>
#include <boost/algorithm/string.hpp>
using boost::algorithm::all;
using boost::algorithm::is_lower;
namespace YouCompleteMe
{
@ -57,15 +61,18 @@ Bitset LetterBitsetFromString( const std::string &text )
return letter_bitset;
}
Candidate::Candidate( const std::string &text )
:
text_( text ),
word_boundary_chars_( GetWordBoundaryChars( text ) ),
text_is_lowercase_( all( text, is_lower() ) ),
letters_present_( LetterBitsetFromString( text ) ),
root_node_( new LetterNode( text ) )
{
}
Result Candidate::QueryMatchResult( const std::string &query ) const
{
LetterNode *node = root_node_.get();
@ -80,7 +87,8 @@ Result Candidate::QueryMatchResult( const std::string &query ) const
node = list->front();
}
return Result( true, &text_, word_boundary_chars_, query );
return Result( true, &text_, text_is_lowercase_, word_boundary_chars_,
query );
}
} // namespace YouCompleteMe

View File

@ -59,6 +59,7 @@ private:
std::string text_;
std::string word_boundary_chars_;
bool text_is_lowercase_;
Bitset letters_present_;
boost::scoped_ptr< LetterNode > root_node_;
};

View File

@ -52,12 +52,14 @@ Result::Result( bool is_subsequence )
ratio_of_word_boundary_chars_in_query_( 0 ),
word_boundary_char_utilization_( 0 ),
query_is_candidate_prefix_( false ),
text_is_lowercase_( false ),
text_( NULL )
{
}
Result::Result( bool is_subsequence,
const std::string *text,
bool text_is_lowercase,
const std::string &word_boundary_chars,
const std::string &query )
:
@ -66,10 +68,11 @@ Result::Result( bool is_subsequence,
ratio_of_word_boundary_chars_in_query_( 0 ),
word_boundary_char_utilization_( 0 ),
query_is_candidate_prefix_( false ),
text_is_lowercase_( text_is_lowercase ),
text_( text )
{
if ( is_subsequence )
SetResultFeaturesFromQuery( query, word_boundary_chars );
SetResultFeaturesFromQuery( word_boundary_chars, query );
}
@ -82,6 +85,7 @@ Result::Result( const Result& other )
other.ratio_of_word_boundary_chars_in_query_ ),
word_boundary_char_utilization_( other.word_boundary_char_utilization_ ),
query_is_candidate_prefix_( other.query_is_candidate_prefix_ ),
text_is_lowercase_( other.text_is_lowercase_ ),
text_( other.text_ )
{
}
@ -124,9 +128,7 @@ bool Result::operator< ( const Result &other ) const {
}
if ( query_is_candidate_prefix_ != other.query_is_candidate_prefix_ )
{
return query_is_candidate_prefix_;
}
if ( !equal_wb_ratios )
{
@ -142,17 +144,18 @@ bool Result::operator< ( const Result &other ) const {
}
if ( text_->length() != other.text_->length() )
{
return text_->length() < other.text_->length();
}
if ( text_is_lowercase_ != other.text_is_lowercase_ )
return text_is_lowercase_;
return *text_ < *other.text_;
}
void Result::SetResultFeaturesFromQuery(
const std::string &query,
const std::string &word_boundary_chars )
const std::string &word_boundary_chars,
const std::string &query)
{
if ( query.empty() || text_->empty() )
return;

View File

@ -30,6 +30,7 @@ public:
Result( bool is_subsequence,
const std::string *text,
bool text_is_lowercase,
const std::string &word_boundary_chars,
const std::string &query );
@ -62,6 +63,7 @@ private:
// number of word boundary matches / number of all word boundary chars
double word_boundary_char_utilization_;
bool query_is_candidate_prefix_;
bool text_is_lowercase_;
const std::string *text_;
};

View File

@ -197,5 +197,17 @@ TEST_F( CompleterTest, ShorterCandidateWins )
"CompleterTest" ) );
}
TEST_F( CompleterTest, SameLowercaseCandidateWins )
{
Pylist results;
Completer( Candidates(
"foobar",
"Foobar" ) ).GetCandidatesForQuery( "foo", results );
EXPECT_THAT( ToStringVector( results ),
ElementsAre( "foobar",
"Foobar" ) );
}
} // namespace YouCompleteMe