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'.
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')
51 if args.import_from_wiki:
52 ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
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.')
59 ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
64 def start_import(args, loader):
66 Create the SPImporter object containing the right
67 sp loader and then start the import of special phrases.
69 from ..tokenizer import factory as tokenizer_factory
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:
75 args.config, args.phplib_dir, db_connection, loader
76 ).import_phrases(tokenizer, should_replace)