]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/special_phrases/sp_wiki_loader.py
914e15391123cf2571c99924a17d446c7bb8415c
[nominatim.git] / nominatim / tools / special_phrases / sp_wiki_loader.py
1 """
2     Module containing the SPWikiLoader class.
3 """
4 import re
5 import logging
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
9
10 LOG = logging.getLogger()
11 class SPWikiLoader(Iterator):
12     """
13         Handles loading of special phrases from the wiki.
14     """
15     def __init__(self, config, languages=None):
16         super().__init__()
17         self.config = config
18         #Compile the regex here to increase performances.
19         self.occurence_pattern = re.compile(
20             r'\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([\-YN])'
21         )
22         self.languages = self._load_languages() if not languages else list(languages)
23
24     def __next__(self):
25         if not self.languages:
26             raise StopIteration
27
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)
32
33     def parse_xml(self, xml):
34         """
35             Parses XML content and extracts special phrases from it.
36             Return a list of SpecialPhrase.
37         """
38         #One match will be of format [label, class, type, operator, plural]
39         matches = self.occurence_pattern.findall(xml)
40         returned_phrases = set()
41         for match in matches:
42             returned_phrases.add(
43                 SpecialPhrase(match[0], match[1], match[2], match[3])
44             )
45         return returned_phrases
46
47     def _load_languages(self):
48         """
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.
52         """
53         default_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
59
60     @staticmethod
61     def _get_wiki_content(lang):
62         """
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
67         """
68         url = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/' + lang.upper() # pylint: disable=line-too-long
69         return get_url(url)