]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/special_phrases.py
a2c346deb84edd38ce58011d190ee8ab735f7e9f
[nominatim.git] / nominatim / clicmd / special_phrases.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8     Implementation of the 'special-phrases' command.
9 """
10 import logging
11 from pathlib import Path
12 from nominatim.errors import UsageError
13 from nominatim.db.connection import connect
14 from nominatim.tools.special_phrases.sp_importer import SPImporter
15 from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
16 from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
17
18 LOG = logging.getLogger()
19
20 # Do not repeat documentation of subcommand classes.
21 # pylint: disable=C0111
22 # Using non-top-level imports to avoid eventually unused imports.
23 # pylint: disable=E0012,C0415
24
25 class ImportSpecialPhrases:
26     """\
27     Import special phrases.
28
29     Special phrases are search terms that narrow down the type of object
30     that should be searched. For example, you might want to search for
31     'Hotels in Barcelona'. The OSM wiki has a selection of special phrases
32     in many languages, which can be imported with this command.
33
34     You can also provide your own phrases in a CSV file. The file needs to have
35     the following five columns:
36      * phrase - the term expected for searching
37      * class - the OSM tag key of the object type
38      * type - the OSM tag value of the object type
39      * operator - the kind of search to be done (one of: in, near, name, -)
40      * plural - whether the term is a plural or not (Y/N)
41
42     An example file can be found in the Nominatim sources at
43     'test/testdb/full_en_phrases_test.csv'.
44
45     The import can be further configured to ignore specific key/value pairs.
46     This is particularly useful when importing phrases from the wiki. The
47     default configuration excludes some very common tags like building=yes.
48     The configuration can be customized by putting a file `phrase-settings.json`
49     with custom rules into the project directory or by using the `--config`
50     option to point to another configuration file.
51     """
52     @staticmethod
53     def add_args(parser):
54         group = parser.add_argument_group('Input arguments')
55         group.add_argument('--import-from-wiki', action='store_true',
56                            help='Import special phrases from the OSM wiki to the database')
57         group.add_argument('--import-from-csv', metavar='FILE',
58                            help='Import special phrases from a CSV file')
59         group.add_argument('--no-replace', action='store_true',
60                            help='Keep the old phrases and only add the new ones')
61         group.add_argument('--config', action='store',
62                            help='Configuration file for black/white listing '
63                                 '(default: phrase-settings.json)')
64
65     @staticmethod
66     def run(args):
67         if args.import_from_wiki:
68             ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
69
70         if args.import_from_csv:
71             if not Path(args.import_from_csv).is_file():
72                 LOG.fatal("CSV file '%s' does not exist.", args.import_from_csv)
73                 raise UsageError('Cannot access file.')
74
75             ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
76
77         return 0
78
79     @staticmethod
80     def start_import(args, loader):
81         """
82             Create the SPImporter object containing the right
83             sp loader and then start the import of special phrases.
84         """
85         from ..tokenizer import factory as tokenizer_factory
86
87         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
88         should_replace = not args.no_replace
89         with connect(args.config.get_libpq_dsn()) as db_connection:
90             SPImporter(
91                 args.config, db_connection, loader
92             ).import_phrases(tokenizer, should_replace)