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
1=== modified file 'src/geonames-query.c'
2--- src/geonames-query.c 2016-04-20 15:49:48 +0000
3+++ src/geonames-query.c 2017-02-01 15:41:11 +0000
4@@ -82,22 +82,33 @@
5
6 static gdouble
7 match_query (gchar **query_tokens,
8- const gchar *potential_hit)
9+ const gchar *potential_hit,
10+ gboolean *all_prefix_match)
11 {
12 g_auto(GStrv) tokens = NULL;
13 gint i;
14 gdouble weight = 0.0;
15
16+ *all_prefix_match = TRUE;
17+
18 tokens = g_str_tokenize_and_fold (potential_hit, NULL, NULL);
19 for (i = 0; query_tokens[i]; i++)
20 {
21- if (tokens[i] == 0)
22- return 0.0;
23-
24- if (!str_prefix_matches (tokens[i], query_tokens[i]))
25- return 0.0;
26-
27- weight += (gdouble) strlen (query_tokens[i]) / strlen (tokens[i]);
28+ if (tokens[i] == 0) {
29+ *all_prefix_match = FALSE;
30+ return 0.0;
31+ }
32+
33+ if (str_prefix_matches (tokens[i], query_tokens[i]))
34+ {
35+ weight += (gdouble) strlen (query_tokens[i]) / strlen (tokens[i]);
36+ }
37+ else
38+ {
39+ const gdouble aux = match_query(query_tokens, &potential_hit[strlen (tokens[i])], all_prefix_match);
40+ *all_prefix_match = FALSE;
41+ return aux;
42+ }
43 }
44
45 return weight / i;
46@@ -120,9 +131,12 @@
47 gdouble best_weight)
48 {
49 gdouble weight;
50+ gboolean all_prefix_match;
51
52- weight = match_query (query_tokens, name);
53+ weight = match_query (query_tokens, name, &all_prefix_match);
54 weight *= (gdouble) CLAMP (population, 1, 1000000) / 1000000;
55+ if (all_prefix_match)
56+ weight += 1;
57
58 return MAX (weight, best_weight);
59 }
60
61=== modified file 'tests/test-geonames.c'
62--- tests/test-geonames.c 2016-04-04 18:21:31 +0000
63+++ tests/test-geonames.c 2017-02-01 15:41:11 +0000
64@@ -30,6 +30,39 @@
65 }
66
67 static void
68+assert_contains (const gchar *query,
69+ const gchar *expected_city,
70+ const gchar *expected_country_code,
71+ gdouble expected_latitude,
72+ gdouble expected_longitude,
73+ guint expected_population)
74+{
75+ g_autofree gint *indices;
76+ guint i, len;
77+ g_autoptr(GeonamesCity) city = NULL;
78+
79+ indices = geonames_query_cities_sync (query, GEONAMES_QUERY_DEFAULT, &len, NULL, NULL);
80+ g_assert (indices);
81+ g_assert_cmpint (indices[len], ==, -1);
82+
83+ g_assert_cmpint (len, >, 0);
84+
85+ for (i = 0; i < len; ++i) {
86+ city = geonames_get_city (indices[i]);
87+
88+ if (g_strcmp0(geonames_city_get_name (city), expected_city) == 0) {
89+ g_assert_cmpstr (geonames_city_get_country_code (city), ==, expected_country_code);
90+ g_assert_cmpfloat (geonames_city_get_latitude (city), ==, expected_latitude);
91+ g_assert_cmpfloat (geonames_city_get_longitude (city), ==, expected_longitude);
92+ g_assert_cmpint (geonames_city_get_population (city), ==, expected_population);
93+ break;
94+ }
95+ }
96+ g_assert (i < len);
97+}
98+
99+
100+static void
101 assert_first_stats (const gchar *query,
102 const gchar *expected_city,
103 const gchar *expected_country_code,
104@@ -115,6 +148,13 @@
105 }
106
107 static void
108+test_cities_without_some_words (void)
109+{
110+ assert_contains ("hospitalet", "L'Hospitalet de Llobregat", "ES", 41.35967, 2.10028, 257038);
111+ assert_contains ("hague", "The Hague", "NL", 52.07667, 4.29861, 474292);
112+}
113+
114+static void
115 test_edge_cases (void)
116 {
117 gint *indices;
118@@ -142,6 +182,7 @@
119 g_test_add_func ("/common-cities", test_common_cities);
120 g_test_add_func ("/translations", test_translations);
121 g_test_add_func ("/edge-cases", test_edge_cases);
122+ g_test_add_func ("/cities-without-some-words", test_cities_without_some_words);
123
124 return g_test_run ();
125 }

Subscribers

People subscribed via source and target branches