1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Module containing the SPWikiLoader class.
10 from typing import Iterable
14 from nominatim_core.config import Configuration
15 from nominatim_core.utils.url_utils import get_url
16 from .special_phrase import SpecialPhrase
18 LOG = logging.getLogger()
20 def _get_wiki_content(lang: str) -> str:
22 Request and return the wiki page's content
23 corresponding to special phrases for a given lang.
24 Requested URL Example :
25 https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/EN
27 url = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/' \
34 Handles loading of special phrases from the wiki.
36 def __init__(self, config: Configuration) -> None:
38 # Compile the regex here to increase performances.
39 self.occurence_pattern = re.compile(
40 r'\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([\-YN])'
42 # Hack around a bug where building=yes was imported with quotes into the wiki
43 self.type_fix_pattern = re.compile(r'\"|"')
45 self.languages = self.config.get_str_list('LANGUAGES') or \
46 ['af', 'ar', 'br', 'ca', 'cs', 'de', 'en', 'es',
47 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'hr', 'hu',
48 'ia', 'is', 'it', 'ja', 'mk', 'nl', 'no', 'pl',
49 'ps', 'pt', 'ru', 'sk', 'sl', 'sv', 'uk', 'vi',
53 def generate_phrases(self) -> Iterable[SpecialPhrase]:
54 """ Download the wiki pages for the configured languages
55 and extract the phrases from the page.
57 for lang in self.languages:
58 LOG.warning('Importing phrases for lang: %s...', lang)
59 loaded_xml = _get_wiki_content(lang)
61 # One match will be of format [label, class, type, operator, plural]
62 matches = self.occurence_pattern.findall(loaded_xml)
65 yield SpecialPhrase(match[0],
67 self.type_fix_pattern.sub('', match[2]),