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 -- Normalize a string and look up its name ids.
240 CREATE OR REPLACE FUNCTION word_ids_from_name(lookup_word TEXT)
245 return_word_ids INTEGER[];
247 lookup_token := ' '|| make_standard_name(lookup_word);
248 SELECT array_agg(word_id) FROM word
249 WHERE word_token = lookup_token and class is null and type is null
250 INTO return_word_ids;
251 RETURN return_word_ids;
254 LANGUAGE plpgsql STABLE STRICT;
257 CREATE OR REPLACE FUNCTION create_country(src HSTORE, country_code varchar(2))
267 FOR item IN SELECT (each(src)).* LOOP
269 s := make_standard_name(item.value);
270 w := getorcreate_country(s, country_code);
272 words := regexp_split_to_array(item.value, E'[,;()]');
273 IF array_upper(words, 1) != 1 THEN
274 FOR j IN 1..array_upper(words, 1) LOOP
275 s := make_standard_name(words[j]);
277 w := getorcreate_country(s, country_code);
287 CREATE OR REPLACE FUNCTION make_keywords(src HSTORE)
298 result := '{}'::INTEGER[];
300 FOR item IN SELECT (each(src)).* LOOP
302 s := make_standard_name(item.value);
303 w := getorcreate_name_id(s, item.value);
305 IF not(ARRAY[w] <@ result) THEN
306 result := result || w;
309 w := getorcreate_word_id(s);
311 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
312 result := result || w;
315 words := string_to_array(s, ' ');
316 IF array_upper(words, 1) IS NOT NULL THEN
317 FOR j IN 1..array_upper(words, 1) LOOP
318 IF (words[j] != '') THEN
319 w = getorcreate_word_id(words[j]);
320 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
321 result := result || w;
327 words := regexp_split_to_array(item.value, E'[,;()]');
328 IF array_upper(words, 1) != 1 THEN
329 FOR j IN 1..array_upper(words, 1) LOOP
330 s := make_standard_name(words[j]);
332 w := getorcreate_word_id(s);
333 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
334 result := result || w;
340 s := regexp_replace(item.value, '市$', '');
341 IF s != item.value THEN
342 s := make_standard_name(s);
344 w := getorcreate_name_id(s, item.value);
345 IF NOT (ARRAY[w] <@ result) THEN
346 result := result || w;
359 CREATE OR REPLACE FUNCTION make_keywords(src TEXT)
370 result := '{}'::INTEGER[];
372 s := make_standard_name(src);
373 w := getorcreate_name_id(s, src);
375 IF NOT (ARRAY[w] <@ result) THEN
376 result := result || w;
379 w := getorcreate_word_id(s);
381 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
382 result := result || w;
385 words := string_to_array(s, ' ');
386 IF array_upper(words, 1) IS NOT NULL THEN
387 FOR j IN 1..array_upper(words, 1) LOOP
388 IF (words[j] != '') THEN
389 w = getorcreate_word_id(words[j]);
390 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
391 result := result || w;
397 words := regexp_split_to_array(src, E'[,;()]');
398 IF array_upper(words, 1) != 1 THEN
399 FOR j IN 1..array_upper(words, 1) LOOP
400 s := make_standard_name(words[j]);
402 w := getorcreate_word_id(s);
403 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
404 result := result || w;
410 s := regexp_replace(src, '市$', '');
412 s := make_standard_name(s);
414 w := getorcreate_name_id(s, src);
415 IF NOT (ARRAY[w] <@ result) THEN
416 result := result || w;