]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/api/search/icu_tokenizer.py
Moved KANJI_MAP to global variable
[nominatim.git] / nominatim / api / search / icu_tokenizer.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Implementation of query analysis for the ICU tokenizer.
9 """
10 from typing import Tuple, Dict, List, Optional, NamedTuple, Iterator, Any, cast
11 from copy import copy
12 from collections import defaultdict
13 import dataclasses
14 import difflib
15
16 from icu import Transliterator
17
18 import sqlalchemy as sa
19
20 from nominatim.typing import SaRow
21 from nominatim.api.connection import SearchConnection
22 from nominatim.api.logging import log
23 from nominatim.api.search import query as qmod
24 from nominatim.api.search.query_analyzer_factory import AbstractQueryAnalyzer
25
26
27 DB_TO_TOKEN_TYPE = {
28     'W': qmod.TokenType.WORD,
29     'w': qmod.TokenType.PARTIAL,
30     'H': qmod.TokenType.HOUSENUMBER,
31     'P': qmod.TokenType.POSTCODE,
32     'C': qmod.TokenType.COUNTRY
33 }
34
35
36 class QueryPart(NamedTuple):
37     """ Normalized and transliterated form of a single term in the query.
38         When the term came out of a split during the transliteration,
39         the normalized string is the full word before transliteration.
40         The word number keeps track of the word before transliteration
41         and can be used to identify partial transliterated terms.
42     """
43     token: str
44     normalized: str
45     word_number: int
46
47
48 QueryParts = List[QueryPart]
49 WordDict = Dict[str, List[qmod.TokenRange]]
50
51 def yield_words(terms: List[QueryPart], start: int) -> Iterator[Tuple[str, qmod.TokenRange]]:
52     """ Return all combinations of words in the terms list after the
53         given position.
54     """
55     total = len(terms)
56     for first in range(start, total):
57         word = terms[first].token
58         yield word, qmod.TokenRange(first, first + 1)
59         for last in range(first + 1, min(first + 20, total)):
60             word = ' '.join((word, terms[last].token))
61             yield word, qmod.TokenRange(first, last + 1)
62
63
64 @dataclasses.dataclass
65 class ICUToken(qmod.Token):
66     """ Specialised token for ICU tokenizer.
67     """
68     word_token: str
69     info: Optional[Dict[str, Any]]
70
71     def get_category(self) -> Tuple[str, str]:
72         assert self.info
73         return self.info.get('class', ''), self.info.get('type', '')
74
75
76     def rematch(self, norm: str) -> None:
77         """ Check how well the token matches the given normalized string
78             and add a penalty, if necessary.
79         """
80         if not self.lookup_word:
81             return
82
83         seq = difflib.SequenceMatcher(a=self.lookup_word, b=norm)
84         distance = 0
85         for tag, afrom, ato, bfrom, bto in seq.get_opcodes():
86             if tag == 'delete' and (afrom == 0 or ato == len(self.lookup_word)):
87                 distance += 1
88             elif tag == 'replace':
89                 distance += max((ato-afrom), (bto-bfrom))
90             elif tag != 'equal':
91                 distance += abs((ato-afrom) - (bto-bfrom))
92         self.penalty += (distance/len(self.lookup_word))
93
94
95     @staticmethod
96     def from_db_row(row: SaRow) -> 'ICUToken':
97         """ Create a ICUToken from the row of the word table.
98         """
99         count = 1 if row.info is None else row.info.get('count', 1)
100
101         penalty = 0.0
102         if row.type == 'w':
103             penalty = 0.3
104         elif row.type == 'H':
105             penalty = sum(0.1 for c in row.word_token if c != ' ' and not c.isdigit())
106             if all(not c.isdigit() for c in row.word_token):
107                 penalty += 0.2 * (len(row.word_token) - 1)
108
109         if row.info is None:
110             lookup_word = row.word
111         else:
112             lookup_word = row.info.get('lookup', row.word)
113         if lookup_word:
114             lookup_word = lookup_word.split('@', 1)[0]
115         else:
116             lookup_word = row.word_token
117
118         return ICUToken(penalty=penalty, token=row.word_id, count=count,
119                         lookup_word=lookup_word, is_indexed=True,
120                         word_token=row.word_token, info=row.info)
121
122
123
124 class ICUQueryAnalyzer(AbstractQueryAnalyzer):
125     """ Converter for query strings into a tokenized query
126         using the tokens created by a ICU tokenizer.
127     """
128
129     def __init__(self, conn: SearchConnection) -> None:
130         self.conn = conn
131
132
133     async def setup(self) -> None:
134         """ Set up static data structures needed for the analysis.
135         """
136         rules = await self.conn.get_property('tokenizer_import_normalisation')
137         self.normalizer = Transliterator.createFromRules("normalization", rules)
138         rules = await self.conn.get_property('tokenizer_import_transliteration')
139         self.transliterator = Transliterator.createFromRules("transliteration", rules)
140
141         if 'word' not in self.conn.t.meta.tables:
142             sa.Table('word', self.conn.t.meta,
143                      sa.Column('word_id', sa.Integer),
144                      sa.Column('word_token', sa.Text, nullable=False),
145                      sa.Column('type', sa.Text, nullable=False),
146                      sa.Column('word', sa.Text),
147                      sa.Column('info', self.conn.t.types.Json))
148
149
150     async def analyze_query(self, phrases: List[qmod.Phrase]) -> qmod.QueryStruct:
151         """ Analyze the given list of phrases and return the
152             tokenized query.
153         """
154         log().section('Analyze query (using ICU tokenizer)')
155         normalized = list(filter(lambda p: p.text,
156                                  (qmod.Phrase(p.ptype, self.normalize_text(p.text))
157                                   for p in phrases)))
158         query = qmod.QueryStruct(normalized)
159         log().var_dump('Normalized query', query.source)
160         if not query.source:
161             return query
162
163         parts, words = self.split_query(query)
164         log().var_dump('Transliterated query', lambda: _dump_transliterated(query, parts))
165
166         for row in await self.lookup_in_db(list(words.keys())):
167             for trange in words[row.word_token]:
168                 token = ICUToken.from_db_row(row)
169                 if row.type == 'S':
170                     if row.info['op'] in ('in', 'near'):
171                         if trange.start == 0:
172                             query.add_token(trange, qmod.TokenType.CATEGORY, token)
173                     else:
174                         query.add_token(trange, qmod.TokenType.QUALIFIER, token)
175                         if trange.start == 0 or trange.end == query.num_token_slots():
176                             token = copy(token)
177                             token.penalty += 0.1 * (query.num_token_slots())
178                             query.add_token(trange, qmod.TokenType.CATEGORY, token)
179                 else:
180                     query.add_token(trange, DB_TO_TOKEN_TYPE[row.type], token)
181
182         self.add_extra_tokens(query, parts)
183         self.rerank_tokens(query, parts)
184
185         log().table_dump('Word tokens', _dump_word_tokens(query))
186
187         return query
188
189
190     def normalize_text(self, text: str) -> str:
191         """ Bring the given text into a normalized form. That is the
192             standardized form search will work with. All information removed
193             at this stage is inevitably lost.
194         """
195         return cast(str, self.normalizer.transliterate(text))
196
197
198     def split_query(self, query: qmod.QueryStruct) -> Tuple[QueryParts, WordDict]:
199         """ Transliterate the phrases and split them into tokens.
200
201             Returns the list of transliterated tokens together with their
202             normalized form and a dictionary of words for lookup together
203             with their position.
204         """
205         parts: QueryParts = []
206         phrase_start = 0
207         words = defaultdict(list)
208         wordnr = 0
209         for phrase in query.source:
210             query.nodes[-1].ptype = phrase.ptype
211             for word in phrase.text.split(' '):
212                 trans = self.transliterator.transliterate(word)
213                 if trans:
214                     for term in trans.split(' '):
215                         if term:
216                             parts.append(QueryPart(term, word, wordnr))
217                             query.add_node(qmod.BreakType.TOKEN, phrase.ptype)
218                     query.nodes[-1].btype = qmod.BreakType.WORD
219                 wordnr += 1
220             query.nodes[-1].btype = qmod.BreakType.PHRASE
221
222             for word, wrange in yield_words(parts, phrase_start):
223                 words[word].append(wrange)
224
225             phrase_start = len(parts)
226         query.nodes[-1].btype = qmod.BreakType.END
227
228         return parts, words
229
230
231     async def lookup_in_db(self, words: List[str]) -> 'sa.Result[Any]':
232         """ Return the token information from the database for the
233             given word tokens.
234         """
235         t = self.conn.t.meta.tables['word']
236         return await self.conn.execute(t.select().where(t.c.word_token.in_(words)))
237
238
239     def add_extra_tokens(self, query: qmod.QueryStruct, parts: QueryParts) -> None:
240         """ Add tokens to query that are not saved in the database.
241         """
242         for part, node, i in zip(parts, query.nodes, range(1000)):
243             if len(part.token) <= 4 and part[0].isdigit()\
244                and not node.has_tokens(i+1, qmod.TokenType.HOUSENUMBER):
245                 query.add_token(qmod.TokenRange(i, i+1), qmod.TokenType.HOUSENUMBER,
246                                 ICUToken(0.5, 0, 1, part.token, True, part.token, None))
247
248
249     def rerank_tokens(self, query: qmod.QueryStruct, parts: QueryParts) -> None:
250         """ Add penalties to tokens that depend on presence of other token.
251         """
252         for i, node, tlist in query.iter_token_lists():
253             if tlist.ttype == qmod.TokenType.POSTCODE:
254                 for repl in node.starting:
255                     if repl.end == tlist.end and repl.ttype != qmod.TokenType.POSTCODE \
256                        and (repl.ttype != qmod.TokenType.HOUSENUMBER
257                             or len(tlist.tokens[0].lookup_word) > 4):
258                         repl.add_penalty(0.39)
259             elif tlist.ttype == qmod.TokenType.HOUSENUMBER \
260                  and len(tlist.tokens[0].lookup_word) <= 3:
261                 if any(c.isdigit() for c in tlist.tokens[0].lookup_word):
262                     for repl in node.starting:
263                         if repl.end == tlist.end and repl.ttype != qmod.TokenType.HOUSENUMBER:
264                             repl.add_penalty(0.5 - tlist.tokens[0].penalty)
265             elif tlist.ttype not in (qmod.TokenType.COUNTRY, qmod.TokenType.PARTIAL):
266                 norm = parts[i].normalized
267                 for j in range(i + 1, tlist.end):
268                     if parts[j - 1].word_number != parts[j].word_number:
269                         norm += '  ' + parts[j].normalized
270                 for token in tlist.tokens:
271                     cast(ICUToken, token).rematch(norm)
272
273
274 def _dump_transliterated(query: qmod.QueryStruct, parts: QueryParts) -> str:
275     out = query.nodes[0].btype.value
276     for node, part in zip(query.nodes[1:], parts):
277         out += part.token + node.btype.value
278     return out
279
280
281 def _dump_word_tokens(query: qmod.QueryStruct) -> Iterator[List[Any]]:
282     yield ['type', 'token', 'word_token', 'lookup_word', 'penalty', 'count', 'info']
283     for node in query.nodes:
284         for tlist in node.starting:
285             for token in tlist.tokens:
286                 t = cast(ICUToken, token)
287                 yield [tlist.ttype.name, t.token, t.word_token or '',
288                        t.lookup_word or '', t.penalty, t.count, t.info]
289
290
291 async def create_query_analyzer(conn: SearchConnection) -> AbstractQueryAnalyzer:
292     """ Create and set up a new query analyzer for a database based
293         on the ICU tokenizer.
294     """
295     out = ICUQueryAnalyzer(conn)
296     await out.setup()
297
298     return out