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 ...config import Configuration
15 from ...utils.url_utils import get_url
16 from .special_phrase import SpecialPhrase
18 LOG = logging.getLogger()
21 def _get_wiki_content(lang: str) -> str:
23 Request and return the wiki page's content
24 corresponding to special phrases for a given lang.
25 Requested URL Example :
26 https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/EN
28 url = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/' \
35 Handles loading of special phrases from the wiki.
37 def __init__(self, config: Configuration) -> None:
39 # Compile the regex here to increase performances.
40 self.occurence_pattern = re.compile(
41 r'\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([\-YN])'
43 # Hack around a bug where building=yes was imported with quotes into the wiki
44 self.type_fix_pattern = re.compile(r'\"|"')
46 self.languages = self.config.get_str_list('LANGUAGES') or \
47 ['af', 'ar', 'br', 'ca', 'cs', 'de', 'en', 'es',
48 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'hr', 'hu',
49 'ia', 'is', 'it', 'ja', 'mk', 'nl', 'no', 'pl',
50 '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]),