def test_sorted_by_popularity(self): assert jautils.sorted_by_popularity( [u'山', u'田', u'xxx', u'yyy', u'zzz']) == \ [u'xxx', u'yyy', u'zzz', u'山', u'田'] assert jautils.sorted_by_popularity( [u'山', u'田', u'はなこ']) == \ [u'はなこ', u'山', u'田'] assert jautils.sorted_by_popularity( [u'山', u'田', u'龍', u'太', u'郎']) == \ [u'龍', u'太', u'郎', u'山', u'田']
def sort_query_words(query_words): """Sort query_words so that the query filters created from query_words are more effective and consistent when truncated due to NeedIndexError, and return the sorted list.""" # (1) Sort them lexicographically so that we return consistent search # results for query 'AA BB CC DD' and 'DD AA BB CC' even when filters # are truncated. sorted_query_words = sorted(query_words) # (2) Sort them according to popularity so that less popular query words, # which are usually more effective filters, come first. sorted_query_words = jautils.sorted_by_popularity(sorted_query_words) # (3) Sort them according to the lengths so that longer query words, # which are usually more effective filters, come first. return sorted(sorted_query_words, key=len, reverse=True)