Merge lp:~aacid/geonames/more_liberal_city_search into lp:geonames

Proposed by Albert Astals Cid
Status: Merged
Approved by: Michael Terry
Approved revision: 27
Merged at revision: 27
Proposed branch: lp:~aacid/geonames/more_liberal_city_search
Merge into: lp:geonames
Diff against target: 125 lines (+64/-9)
2 files modified
src/geonames-query.c (+23/-9)
tests/test-geonames.c (+41/-0)
To merge this branch: bzr merge lp:~aacid/geonames/more_liberal_city_search
Reviewer Review Type Date Requested Status
Sebastien Bacher Approve
Review via email: mp+316102@code.launchpad.net

Commit message

Be less strict on city search

Include matches that ignore words at the beggining (i.e. searching for Hague will return "The Hague" as result)

Description of the change

Was wondering if we wanted a new flag to GeonamesQueryFlags or making it by default less strict is ok.

To post a comment you must log in.
Revision history for this message
Sebastien Bacher (seb128) wrote :

Thanks but as discussed on IRC that contredicts what is describe on bug #1454186 / on the design wiki, the matching should be done on full word or start of words not in subtrings in the middle of a word

review: Needs Fixing
27. By Albert Astals Cid

Be less strict on city search

Include matches that ignore words at the beggining (i.e. searching for Hague will return "The Hague" as result)

Revision history for this message
Albert Astals Cid (aacid) wrote :

> Thanks but as discussed on IRC that contredicts what is describe on bug
> #1454186 / on the design wiki, the matching should be done on full word or
> start of words not in subtrings in the middle of a word

Ok, changed to only "ignore" words at the beginning.

Revision history for this message
Sebastien Bacher (seb128) wrote :

Thanks, looks fine and works as it should

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/geonames-query.c'
--- src/geonames-query.c 2016-04-20 15:49:48 +0000
+++ src/geonames-query.c 2017-02-01 15:41:11 +0000
@@ -82,22 +82,33 @@
8282
83static gdouble83static gdouble
84match_query (gchar **query_tokens,84match_query (gchar **query_tokens,
85 const gchar *potential_hit)85 const gchar *potential_hit,
86 gboolean *all_prefix_match)
86{87{
87 g_auto(GStrv) tokens = NULL;88 g_auto(GStrv) tokens = NULL;
88 gint i;89 gint i;
89 gdouble weight = 0.0;90 gdouble weight = 0.0;
9091
92 *all_prefix_match = TRUE;
93
91 tokens = g_str_tokenize_and_fold (potential_hit, NULL, NULL);94 tokens = g_str_tokenize_and_fold (potential_hit, NULL, NULL);
92 for (i = 0; query_tokens[i]; i++)95 for (i = 0; query_tokens[i]; i++)
93 {96 {
94 if (tokens[i] == 0)97 if (tokens[i] == 0) {
95 return 0.0;98 *all_prefix_match = FALSE;
9699 return 0.0;
97 if (!str_prefix_matches (tokens[i], query_tokens[i]))100 }
98 return 0.0;101
99102 if (str_prefix_matches (tokens[i], query_tokens[i]))
100 weight += (gdouble) strlen (query_tokens[i]) / strlen (tokens[i]);103 {
104 weight += (gdouble) strlen (query_tokens[i]) / strlen (tokens[i]);
105 }
106 else
107 {
108 const gdouble aux = match_query(query_tokens, &potential_hit[strlen (tokens[i])], all_prefix_match);
109 *all_prefix_match = FALSE;
110 return aux;
111 }
101 }112 }
102113
103 return weight / i;114 return weight / i;
@@ -120,9 +131,12 @@
120 gdouble best_weight)131 gdouble best_weight)
121{132{
122 gdouble weight;133 gdouble weight;
134 gboolean all_prefix_match;
123135
124 weight = match_query (query_tokens, name);136 weight = match_query (query_tokens, name, &all_prefix_match);
125 weight *= (gdouble) CLAMP (population, 1, 1000000) / 1000000;137 weight *= (gdouble) CLAMP (population, 1, 1000000) / 1000000;
138 if (all_prefix_match)
139 weight += 1;
126140
127 return MAX (weight, best_weight);141 return MAX (weight, best_weight);
128}142}
129143
=== modified file 'tests/test-geonames.c'
--- tests/test-geonames.c 2016-04-04 18:21:31 +0000
+++ tests/test-geonames.c 2017-02-01 15:41:11 +0000
@@ -30,6 +30,39 @@
30}30}
3131
32static void32static void
33assert_contains (const gchar *query,
34 const gchar *expected_city,
35 const gchar *expected_country_code,
36 gdouble expected_latitude,
37 gdouble expected_longitude,
38 guint expected_population)
39{
40 g_autofree gint *indices;
41 guint i, len;
42 g_autoptr(GeonamesCity) city = NULL;
43
44 indices = geonames_query_cities_sync (query, GEONAMES_QUERY_DEFAULT, &len, NULL, NULL);
45 g_assert (indices);
46 g_assert_cmpint (indices[len], ==, -1);
47
48 g_assert_cmpint (len, >, 0);
49
50 for (i = 0; i < len; ++i) {
51 city = geonames_get_city (indices[i]);
52
53 if (g_strcmp0(geonames_city_get_name (city), expected_city) == 0) {
54 g_assert_cmpstr (geonames_city_get_country_code (city), ==, expected_country_code);
55 g_assert_cmpfloat (geonames_city_get_latitude (city), ==, expected_latitude);
56 g_assert_cmpfloat (geonames_city_get_longitude (city), ==, expected_longitude);
57 g_assert_cmpint (geonames_city_get_population (city), ==, expected_population);
58 break;
59 }
60 }
61 g_assert (i < len);
62}
63
64
65static void
33assert_first_stats (const gchar *query,66assert_first_stats (const gchar *query,
34 const gchar *expected_city,67 const gchar *expected_city,
35 const gchar *expected_country_code,68 const gchar *expected_country_code,
@@ -115,6 +148,13 @@
115}148}
116149
117static void150static void
151test_cities_without_some_words (void)
152{
153 assert_contains ("hospitalet", "L'Hospitalet de Llobregat", "ES", 41.35967, 2.10028, 257038);
154 assert_contains ("hague", "The Hague", "NL", 52.07667, 4.29861, 474292);
155}
156
157static void
118test_edge_cases (void)158test_edge_cases (void)
119{159{
120 gint *indices;160 gint *indices;
@@ -142,6 +182,7 @@
142 g_test_add_func ("/common-cities", test_common_cities);182 g_test_add_func ("/common-cities", test_common_cities);
143 g_test_add_func ("/translations", test_translations);183 g_test_add_func ("/translations", test_translations);
144 g_test_add_func ("/edge-cases", test_edge_cases);184 g_test_add_func ("/edge-cases", test_edge_cases);
185 g_test_add_func ("/cities-without-some-words", test_cities_without_some_words);
145186
146 return g_test_run ();187 return g_test_run ();
147}188}

Subscribers

People subscribed via source and target branches