2 Implementation of the 'special-phrases' command.
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
12 LOG = logging.getLogger()
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
19 class ImportSpecialPhrases:
21 Import special phrases.
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.
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)
36 An example file can be found in the Nominatim sources at
37 'test/testdb/full_en_phrases_test.csv'.
39 The import can be further configured to ignore specific key/value pairs.
40 This is particularly useful when importing phrases from the wiki. The
41 default configuration excludes some very common tags like building=yes.
42 The configuration can be customized by putting a file `phrase-settings.json`
43 with custom rules into the project directory or by using the `--config`
44 option to point to another configuration file.
48 group = parser.add_argument_group('Input arguments')
49 group.add_argument('--import-from-wiki', action='store_true',
50 help='Import special phrases from the OSM wiki to the database')
51 group.add_argument('--import-from-csv', metavar='FILE',
52 help='Import special phrases from a CSV file')
53 group.add_argument('--no-replace', action='store_true',
54 help='Keep the old phrases and only add the new ones')
55 group.add_argument('--config', action='store',
56 help='Configuration file for black/white listing '
57 '(default: phrase-settings.json)')
61 if args.import_from_wiki:
62 ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
64 if args.import_from_csv:
65 if not Path(args.import_from_csv).is_file():
66 LOG.fatal("CSV file '%s' does not exist.", args.import_from_csv)
67 raise UsageError('Cannot access file.')
69 ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
74 def start_import(args, loader):
76 Create the SPImporter object containing the right
77 sp loader and then start the import of special phrases.
79 from ..tokenizer import factory as tokenizer_factory
81 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
82 should_replace = not args.no_replace
83 with connect(args.config.get_libpq_dsn()) as db_connection:
85 args.config, db_connection, loader
86 ).import_phrases(tokenizer, should_replace)