]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/special_phrases.py
626c0053cb4f7793328595fed44c928459e1e926
[nominatim.git] / nominatim / clicmd / special_phrases.py
1 """
2     Implementation of the 'special-phrases' command.
3 """
4 import logging
5 from pathlib import Path
6 from nominatim.errors import UsageError
7 from nominatim.db.connection import connect
8 from nominatim.tools.special_phrases.sp_importer import SPImporter
9 from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
10 from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
11
12 LOG = logging.getLogger()
13
14 # Do not repeat documentation of subcommand classes.
15 # pylint: disable=C0111
16 # Using non-top-level imports to avoid eventually unused imports.
17 # pylint: disable=E0012,C0415
18
19 class ImportSpecialPhrases:
20     """\
21     Import special phrases.
22
23     Special phrases are search terms that narrow down the type of object
24     that should be searched. For example, you might want to search for
25     'Hotels in Barcelona'. The OSM wiki has a selection of special phrases
26     in many languages, which can be imported with this command.
27
28     You can also provide your own phrases in a CSV file. The file needs to have
29     the following five columns:
30      * phrase - the term expected for searching
31      * class - the OSM tag key of the object type
32      * type - the OSM tag value of the object type
33      * operator - the kind of search to be done (one of: in, near, name, -)
34      * plural - whether the term is a plural or not (Y/N)
35
36     An example file can be found in the Nominatim sources at
37     'test/testdb/full_en_phrases_test.csv'.
38     """
39     @staticmethod
40     def add_args(parser):
41         group = parser.add_argument_group('Input arguments')
42         group.add_argument('--import-from-wiki', action='store_true',
43                            help='Import special phrases from the OSM wiki to the database')
44         group.add_argument('--import-from-csv', metavar='FILE',
45                            help='Import special phrases from a CSV file')
46         group.add_argument('--no-replace', action='store_true',
47                            help='Keep the old phrases and only add the new ones')
48
49     @staticmethod
50     def run(args):
51         if args.import_from_wiki:
52             ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
53
54         if args.import_from_csv:
55             if not Path(args.import_from_csv).is_file():
56                 LOG.fatal("CSV file '%s' does not exist.", args.import_from_csv)
57                 raise UsageError('Cannot access file.')
58
59             ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
60
61         return 0
62
63     @staticmethod
64     def start_import(args, loader):
65         """
66             Create the SPImporter object containing the right
67             sp loader and then start the import of special phrases.
68         """
69         from ..tokenizer import factory as tokenizer_factory
70
71         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
72         should_replace = not args.no_replace
73         with connect(args.config.get_libpq_dsn()) as db_connection:
74             SPImporter(
75                 args.config, args.phplib_dir, db_connection, loader
76             ).import_phrases(tokenizer, should_replace)