1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of the 'special-phrases' command.
12 from pathlib import Path
14 from ..errors import UsageError
15 from ..db.connection import connect
16 from ..tools.special_phrases.sp_importer import SPImporter, SpecialPhraseLoader
17 from ..tools.special_phrases.sp_wiki_loader import SPWikiLoader
18 from ..tools.special_phrases.sp_csv_loader import SPCsvLoader
19 from .args import NominatimArgs
22 LOG = logging.getLogger()
25 class ImportSpecialPhrases:
27 Import special phrases.
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.
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)
42 An example file can be found in the Nominatim sources at
43 'test/testdb/full_en_phrases_test.csv'.
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.
53 def add_args(self, parser: argparse.ArgumentParser) -> None:
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')
62 def run(self, args: NominatimArgs) -> int:
64 if args.import_from_wiki:
65 self.start_import(args, SPWikiLoader(args.config))
67 if args.import_from_csv:
68 if not Path(args.import_from_csv).is_file():
69 LOG.fatal("CSV file '%s' does not exist.", args.import_from_csv)
70 raise UsageError('Cannot access file.')
72 self.start_import(args, SPCsvLoader(args.import_from_csv))
76 def start_import(self, args: NominatimArgs, loader: SpecialPhraseLoader) -> None:
78 Create the SPImporter object containing the right
79 sp loader and then start the import of special phrases.
81 from ..tokenizer import factory as tokenizer_factory
83 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
84 should_replace = not args.no_replace
85 with connect(args.config.get_libpq_dsn()) as db_connection:
87 args.config, db_connection, loader
88 ).import_phrases(tokenizer, should_replace)