2 Module containing the SPWikiLoader class.
6 from collections.abc import Iterator
7 from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
8 from nominatim.tools.exec_utils import get_url
10 LOG = logging.getLogger()
11 class SPWikiLoader(Iterator):
13 Handles loading of special phrases from the wiki.
15 def __init__(self, config, languages=None):
18 #Compile the regex here to increase performances.
19 self.occurence_pattern = re.compile(
20 r'\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([\-YN])'
22 self.languages = self._load_languages() if not languages else list(languages)
25 if not self.languages:
28 lang = self.languages.pop(0)
29 loaded_xml = self._get_wiki_content(lang)
30 LOG.warning('Importing phrases for lang: %s...', lang)
31 return self.parse_xml(loaded_xml)
33 def parse_xml(self, xml):
35 Parses XML content and extracts special phrases from it.
36 Return a list of SpecialPhrase.
38 #One match will be of format [label, class, type, operator, plural]
39 matches = self.occurence_pattern.findall(xml)
40 returned_phrases = set()
43 SpecialPhrase(match[0], match[1], match[2], match[3])
45 return returned_phrases
47 def _load_languages(self):
49 Get list of all languages from env config file
50 or default if there is no languages configured.
51 The system will extract special phrases only from all specified languages.
54 'af', 'ar', 'br', 'ca', 'cs', 'de', 'en', 'es',
55 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'hr', 'hu',
56 'ia', 'is', 'it', 'ja', 'mk', 'nl', 'no', 'pl',
57 'ps', 'pt', 'ru', 'sk', 'sl', 'sv', 'uk', 'vi']
58 return self.config.LANGUAGES.split(',') if self.config.LANGUAGES else default_languages
61 def _get_wiki_content(lang):
63 Request and return the wiki page's content
64 corresponding to special phrases for a given lang.
65 Requested URL Example :
66 https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/EN
68 url = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/' + lang.upper() # pylint: disable=line-too-long