1 -- Functions for term normalisation and access to the 'word' table.
3 CREATE OR REPLACE FUNCTION transliteration(text) RETURNS text
4 AS '{modulepath}/nominatim.so', 'transliteration'
5 LANGUAGE c IMMUTABLE STRICT;
8 CREATE OR REPLACE FUNCTION gettokenstring(text) RETURNS text
9 AS '{modulepath}/nominatim.so', 'gettokenstring'
10 LANGUAGE c IMMUTABLE STRICT;
13 CREATE OR REPLACE FUNCTION make_standard_name(name TEXT) RETURNS TEXT
18 o := public.gettokenstring(public.transliteration(name));
19 RETURN trim(substr(o,1,length(o)));
22 LANGUAGE plpgsql IMMUTABLE;
24 -- returns NULL if the word is too common
25 CREATE OR REPLACE FUNCTION getorcreate_word_id(lookup_word TEXT)
30 return_word_id INTEGER;
33 lookup_token := trim(lookup_word);
34 SELECT min(word_id), max(search_name_count) FROM word
35 WHERE word_token = lookup_token and class is null and type is null
36 INTO return_word_id, count;
37 IF return_word_id IS NULL THEN
38 return_word_id := nextval('seq_word');
39 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, null, null, 0);
41 IF count > get_maxwordfreq() THEN
42 return_word_id := NULL;
45 RETURN return_word_id;
51 CREATE OR REPLACE FUNCTION getorcreate_housenumber_id(lookup_word TEXT)
56 return_word_id INTEGER;
58 lookup_token := ' ' || trim(lookup_word);
59 SELECT min(word_id) FROM word
60 WHERE word_token = lookup_token and class='place' and type='house'
62 IF return_word_id IS NULL THEN
63 return_word_id := nextval('seq_word');
64 INSERT INTO word VALUES (return_word_id, lookup_token, null,
65 'place', 'house', null, 0);
67 RETURN return_word_id;
73 CREATE OR REPLACE FUNCTION getorcreate_postcode_id(postcode TEXT)
79 return_word_id INTEGER;
81 lookup_word := upper(trim(postcode));
82 lookup_token := ' ' || make_standard_name(lookup_word);
83 SELECT min(word_id) FROM word
84 WHERE word_token = lookup_token and class='place' and type='postcode'
86 IF return_word_id IS NULL THEN
87 return_word_id := nextval('seq_word');
88 INSERT INTO word VALUES (return_word_id, lookup_token, lookup_word,
89 'place', 'postcode', null, 0);
91 RETURN return_word_id;
97 CREATE OR REPLACE FUNCTION getorcreate_country(lookup_word TEXT,
98 lookup_country_code varchar(2))
103 return_word_id INTEGER;
105 lookup_token := ' '||trim(lookup_word);
106 SELECT min(word_id) FROM word
107 WHERE word_token = lookup_token and country_code=lookup_country_code
109 IF return_word_id IS NULL THEN
110 return_word_id := nextval('seq_word');
111 INSERT INTO word VALUES (return_word_id, lookup_token, null,
112 null, null, lookup_country_code, 0);
114 RETURN return_word_id;
120 CREATE OR REPLACE FUNCTION getorcreate_amenity(lookup_word TEXT, normalized_word TEXT,
121 lookup_class text, lookup_type text)
126 return_word_id INTEGER;
128 lookup_token := ' '||trim(lookup_word);
129 SELECT min(word_id) FROM word
130 WHERE word_token = lookup_token and word = normalized_word
131 and class = lookup_class and type = lookup_type
133 IF return_word_id IS NULL THEN
134 return_word_id := nextval('seq_word');
135 INSERT INTO word VALUES (return_word_id, lookup_token, normalized_word,
136 lookup_class, lookup_type, null, 0);
138 RETURN return_word_id;
144 CREATE OR REPLACE FUNCTION getorcreate_amenityoperator(lookup_word TEXT,
145 normalized_word TEXT,
153 return_word_id INTEGER;
155 lookup_token := ' '||trim(lookup_word);
156 SELECT min(word_id) FROM word
157 WHERE word_token = lookup_token and word = normalized_word
158 and class = lookup_class and type = lookup_type and operator = op
160 IF return_word_id IS NULL THEN
161 return_word_id := nextval('seq_word');
162 INSERT INTO word VALUES (return_word_id, lookup_token, normalized_word,
163 lookup_class, lookup_type, null, 0, op);
165 RETURN return_word_id;
171 CREATE OR REPLACE FUNCTION getorcreate_name_id(lookup_word TEXT, src_word TEXT)
176 nospace_lookup_token TEXT;
177 return_word_id INTEGER;
179 lookup_token := ' '||trim(lookup_word);
180 SELECT min(word_id) FROM word
181 WHERE word_token = lookup_token and class is null and type is null
183 IF return_word_id IS NULL THEN
184 return_word_id := nextval('seq_word');
185 INSERT INTO word VALUES (return_word_id, lookup_token, src_word,
186 null, null, null, 0);
188 RETURN return_word_id;
194 CREATE OR REPLACE FUNCTION getorcreate_name_id(lookup_word TEXT)
199 RETURN getorcreate_name_id(lookup_word, '');
204 -- Normalize a string and lookup its word ids (partial words).
205 CREATE OR REPLACE FUNCTION addr_ids_from_name(lookup_word TEXT)
211 return_word_id INTEGER[];
213 lookup_token := make_standard_name(lookup_word);
214 SELECT array_agg(word_id) FROM word
215 WHERE word_token = lookup_token and class is null and type is null
217 IF return_word_id IS NULL THEN
218 id := nextval('seq_word');
219 INSERT INTO word VALUES (id, lookup_token, null, null, null, null, 0);
220 return_word_id = ARRAY[id];
222 RETURN return_word_id;
228 -- Normalize a string and look up its name ids (full words).
229 CREATE OR REPLACE FUNCTION word_ids_from_name(lookup_word TEXT)
234 return_word_ids INTEGER[];
236 lookup_token := ' '|| make_standard_name(lookup_word);
237 SELECT array_agg(word_id) FROM word
238 WHERE word_token = lookup_token and class is null and type is null
239 INTO return_word_ids;
240 RETURN return_word_ids;
243 LANGUAGE plpgsql STABLE STRICT;
246 CREATE OR REPLACE FUNCTION create_country(src HSTORE, country_code varchar(2))
256 FOR item IN SELECT (each(src)).* LOOP
258 s := make_standard_name(item.value);
259 w := getorcreate_country(s, country_code);
261 words := regexp_split_to_array(item.value, E'[,;()]');
262 IF array_upper(words, 1) != 1 THEN
263 FOR j IN 1..array_upper(words, 1) LOOP
264 s := make_standard_name(words[j]);
266 w := getorcreate_country(s, country_code);
276 CREATE OR REPLACE FUNCTION make_keywords(src HSTORE)
287 result := '{}'::INTEGER[];
289 FOR item IN SELECT (each(src)).* LOOP
291 s := make_standard_name(item.value);
292 w := getorcreate_name_id(s, item.value);
294 IF not(ARRAY[w] <@ result) THEN
295 result := result || w;
298 w := getorcreate_word_id(s);
300 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
301 result := result || w;
304 words := string_to_array(s, ' ');
305 IF array_upper(words, 1) IS NOT NULL THEN
306 FOR j IN 1..array_upper(words, 1) LOOP
307 IF (words[j] != '') THEN
308 w = getorcreate_word_id(words[j]);
309 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
310 result := result || w;
316 words := regexp_split_to_array(item.value, E'[,;()]');
317 IF array_upper(words, 1) != 1 THEN
318 FOR j IN 1..array_upper(words, 1) LOOP
319 s := make_standard_name(words[j]);
321 w := getorcreate_word_id(s);
322 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
323 result := result || w;
329 s := regexp_replace(item.value, '市$', '');
330 IF s != item.value THEN
331 s := make_standard_name(s);
333 w := getorcreate_name_id(s, item.value);
334 IF NOT (ARRAY[w] <@ result) THEN
335 result := result || w;
348 CREATE OR REPLACE FUNCTION make_keywords(src TEXT)
359 result := '{}'::INTEGER[];
361 s := make_standard_name(src);
362 w := getorcreate_name_id(s, src);
364 IF NOT (ARRAY[w] <@ result) THEN
365 result := result || w;
368 w := getorcreate_word_id(s);
370 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
371 result := result || w;
374 words := string_to_array(s, ' ');
375 IF array_upper(words, 1) IS NOT NULL THEN
376 FOR j IN 1..array_upper(words, 1) LOOP
377 IF (words[j] != '') THEN
378 w = getorcreate_word_id(words[j]);
379 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
380 result := result || w;
386 words := regexp_split_to_array(src, E'[,;()]');
387 IF array_upper(words, 1) != 1 THEN
388 FOR j IN 1..array_upper(words, 1) LOOP
389 s := make_standard_name(words[j]);
391 w := getorcreate_word_id(s);
392 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
393 result := result || w;
399 s := regexp_replace(src, '市$', '');
401 s := make_standard_name(s);
403 w := getorcreate_name_id(s, src);
404 IF NOT (ARRAY[w] <@ result) THEN
405 result := result || w;