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, '');
205 CREATE OR REPLACE FUNCTION get_word_id(lookup_word TEXT)
210 return_word_id INTEGER;
212 lookup_token := trim(lookup_word);
213 SELECT min(word_id) FROM word
214 WHERE word_token = lookup_token and class is null and type is null
216 RETURN return_word_id;
219 LANGUAGE plpgsql STABLE;
222 CREATE OR REPLACE FUNCTION get_name_id(lookup_word TEXT)
227 return_word_id INTEGER;
229 lookup_token := ' '||trim(lookup_word);
230 SELECT min(word_id) FROM word
231 WHERE word_token = lookup_token and class is null and type is null
233 RETURN return_word_id;
236 LANGUAGE plpgsql STABLE;
239 CREATE OR REPLACE FUNCTION get_name_ids(lookup_word TEXT)
244 return_word_ids INTEGER[];
246 lookup_token := ' '||trim(lookup_word);
247 SELECT array_agg(word_id) FROM word
248 WHERE word_token = lookup_token and class is null and type is null
249 INTO return_word_ids;
250 RETURN return_word_ids;
253 LANGUAGE plpgsql STABLE;
256 CREATE OR REPLACE FUNCTION word_ids_from_name(lookup_word TEXT)
261 return_word_ids INTEGER[];
263 lookup_token := ' '|| make_standard_name(lookup_word);
264 SELECT array_agg(word_id) FROM word
265 WHERE word_token = lookup_token and class is null and type is null
266 INTO return_word_ids;
267 RETURN return_word_ids;
270 LANGUAGE plpgsql STABLE STRICT;
273 CREATE OR REPLACE FUNCTION create_country(src HSTORE, country_code varchar(2))
283 FOR item IN SELECT (each(src)).* LOOP
285 s := make_standard_name(item.value);
286 w := getorcreate_country(s, country_code);
288 words := regexp_split_to_array(item.value, E'[,;()]');
289 IF array_upper(words, 1) != 1 THEN
290 FOR j IN 1..array_upper(words, 1) LOOP
291 s := make_standard_name(words[j]);
293 w := getorcreate_country(s, country_code);
303 CREATE OR REPLACE FUNCTION make_keywords(src HSTORE)
314 result := '{}'::INTEGER[];
316 FOR item IN SELECT (each(src)).* LOOP
318 s := make_standard_name(item.value);
319 w := getorcreate_name_id(s, item.value);
321 IF not(ARRAY[w] <@ result) THEN
322 result := result || w;
325 w := getorcreate_word_id(s);
327 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
328 result := result || w;
331 words := string_to_array(s, ' ');
332 IF array_upper(words, 1) IS NOT NULL THEN
333 FOR j IN 1..array_upper(words, 1) LOOP
334 IF (words[j] != '') THEN
335 w = getorcreate_word_id(words[j]);
336 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
337 result := result || w;
343 words := regexp_split_to_array(item.value, E'[,;()]');
344 IF array_upper(words, 1) != 1 THEN
345 FOR j IN 1..array_upper(words, 1) LOOP
346 s := make_standard_name(words[j]);
348 w := getorcreate_word_id(s);
349 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
350 result := result || w;
356 s := regexp_replace(item.value, '市$', '');
357 IF s != item.value THEN
358 s := make_standard_name(s);
360 w := getorcreate_name_id(s, item.value);
361 IF NOT (ARRAY[w] <@ result) THEN
362 result := result || w;
375 CREATE OR REPLACE FUNCTION make_keywords(src TEXT)
386 result := '{}'::INTEGER[];
388 s := make_standard_name(src);
389 w := getorcreate_name_id(s, src);
391 IF NOT (ARRAY[w] <@ result) THEN
392 result := result || w;
395 w := getorcreate_word_id(s);
397 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
398 result := result || w;
401 words := string_to_array(s, ' ');
402 IF array_upper(words, 1) IS NOT NULL THEN
403 FOR j IN 1..array_upper(words, 1) LOOP
404 IF (words[j] != '') THEN
405 w = getorcreate_word_id(words[j]);
406 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
407 result := result || w;
413 words := regexp_split_to_array(src, E'[,;()]');
414 IF array_upper(words, 1) != 1 THEN
415 FOR j IN 1..array_upper(words, 1) LOOP
416 s := make_standard_name(words[j]);
418 w := getorcreate_word_id(s);
419 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
420 result := result || w;
426 s := regexp_replace(src, '市$', '');
428 s := make_standard_name(s);
430 w := getorcreate_name_id(s, src);
431 IF NOT (ARRAY[w] <@ result) THEN
432 result := result || w;