]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/special_phrases/sp_wiki_loader.py
Introduction of SPCsvLoader to load special phrases from a csv file
[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 nominatim.tools.special_phrases.special_phrase import SpecialPhrase
7 from nominatim.tools.special_phrases.sp_loader import SPLoader
8 from nominatim.tools.exec_utils import get_url
9
10 LOG = logging.getLogger()
11 class SPWikiLoader(SPLoader):
12     """
13         Handles loading of special phrases from the wiki.
14     """
15     def __init__(self, config, languages=None):
16         if languages is not None and not isinstance(languages, list):
17             raise TypeError('The \'languages\' parameter should be of type list.')
18         super().__init__()
19         self.config = config
20         #Compile the regex here to increase performances.
21         self.occurence_pattern = re.compile(
22             r'\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([^\|]+) *\|\| *([\-YN])'
23         )
24         self.languages = self._load_languages() if not languages else languages
25
26     def __next__(self):
27         if not self.languages:
28             raise StopIteration
29
30         lang = self.languages.pop(0)
31         loaded_xml = SPWikiLoader._get_wiki_content(lang)
32         LOG.warning('Importing phrases for lang: %s...', lang)
33         return self.parse_xml(loaded_xml)
34
35     def parse_xml(self, xml):
36         """
37             Parses XML content and extracts special phrases from it.
38             Return a list of SpecialPhrase.
39         """
40         #One match will be of format [label, class, type, operator, plural]
41         matches = self.occurence_pattern.findall(xml)
42         returned_phrases = set()
43         for match in matches:
44             returned_phrases.add(
45                 SpecialPhrase(match[0], match[1], match[2], match[3])
46             )
47         return returned_phrases
48
49     def _load_languages(self):
50         """
51             Get list of all languages from env config file
52             or default if there is no languages configured.
53             The system will extract special phrases only from all specified languages.
54         """
55         default_languages = [
56             'af', 'ar', 'br', 'ca', 'cs', 'de', 'en', 'es',
57             'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'hr', 'hu',
58             'ia', 'is', 'it', 'ja', 'mk', 'nl', 'no', 'pl',
59             'ps', 'pt', 'ru', 'sk', 'sl', 'sv', 'uk', 'vi']
60         return self.config.LANGUAGES.split(',') if self.config.LANGUAGES else default_languages
61
62     @staticmethod
63     def _get_wiki_content(lang):
64         """
65             Request and return the wiki page's content
66             corresponding to special phrases for a given lang.
67             Requested URL Example :
68                 https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/EN
69         """
70         url = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/' + lang.upper() # pylint: disable=line-too-long
71         return get_url(url)