diff --git a/search/geocoder_context.cpp b/search/geocoder_context.cpp index cd2aaec133..a66684b4bf 100644 --- a/search/geocoder_context.cpp +++ b/search/geocoder_context.cpp @@ -84,7 +84,7 @@ size_t BaseContext::NumUnusedTokenGroups() const return numGroups; } -string DebugPrint(BaseContext::TokenType type) +string ToString(BaseContext::TokenType type) { switch (type) { @@ -101,4 +101,9 @@ string DebugPrint(BaseContext::TokenType type) } UNREACHABLE(); } + +string DebugPrint(BaseContext::TokenType type) +{ + return ToString(type); +} } // namespace search diff --git a/search/geocoder_context.hpp b/search/geocoder_context.hpp index f63ea8365a..a72d896ece 100644 --- a/search/geocoder_context.hpp +++ b/search/geocoder_context.hpp @@ -83,4 +83,5 @@ struct BaseContext }; std::string DebugPrint(BaseContext::TokenType type); +std::string ToString(BaseContext::TokenType type); } // namespace search diff --git a/search/pysearch/CMakeLists.txt b/search/pysearch/CMakeLists.txt index c55701b9e5..df6a9d0863 100644 --- a/search/pysearch/CMakeLists.txt +++ b/search/pysearch/CMakeLists.txt @@ -11,6 +11,7 @@ omim_add_library(${PROJECT_NAME} MODULE ${SRC}) omim_link_libraries( ${PROJECT_NAME} + search_quality search_tests_support search storage diff --git a/search/pysearch/README.txt b/search/pysearch/README.txt index 0d6b08e13f..f2f6fda077 100644 --- a/search/pysearch/README.txt +++ b/search/pysearch/README.txt @@ -2,14 +2,13 @@ This document describes how to use this module. 1. How to build? - To build the module you need Python2.7 and Boost Python. Also, you - need Qt5.5 (or higher), but you need it in any case, if you're - planning to build the project. On MacOS, Python2.7 should be + To build the module you need Python3.7 (or higher) and Boost Python. Also, you + need Qt5.5 (or higher), but you need it in any case if you're + planning to build the project. On MacOS, Python3.7 should be installed by default, to get Boost via Brew just type in the shell: brew update - brew install boost --with-python - brew install boost-python + brew install boost-python3 On Debian, type in the shell: @@ -24,9 +23,21 @@ This document describes how to use this module. Then, invoke cmake from the shell, for example: - cmake path-to-omim-directory -DPYBINDINGS=ON -DCMAKE_PREFIX_PATH=path-to-qt5.5 + cmake \ + -DPYBINDINGS=ON\ + -DPYTHON_VERSION=3.7\ + -DPYBINDINGS_VERSION=3.7\ + -DPYTHON_LIBRARIES=\ + -DPYTHON_INCLUDE_DIRS=\ + -DCMAKE_PREFIX_PATH= + make -k -j8 pysearch + To set the python paths correctly, refer to https://cmake.org/cmake/help/v3.0/module/FindPythonLibs.html + and + + python-config --includes + 2. How to use? As pysearch is a custom Python module, all that you need diff --git a/search/pysearch/bindings.cpp b/search/pysearch/bindings.cpp index 1fd5b89c1e..f7f1b65c20 100644 --- a/search/pysearch/bindings.cpp +++ b/search/pysearch/bindings.cpp @@ -1,4 +1,5 @@ #include "search/engine.hpp" +#include "search/search_quality/helpers.hpp" #include "search/search_tests_support/test_search_engine.hpp" #include "search/search_tests_support/test_search_request.hpp" #include "search/tracer.hpp" @@ -6,8 +7,7 @@ #include "indexer/classificator_loader.hpp" #include "indexer/data_source.hpp" -#include "storage/country.hpp" -#include "storage/country_info_getter.hpp" +#include "storage/storage_defines.hpp" #include "platform/local_country_file.hpp" #include "platform/local_country_file_utils.hpp" @@ -38,50 +38,6 @@ using namespace std; namespace { -unique_ptr g_affiliations; - -string ToString(search::Tracer::Parse::TokenType type) -{ - using TokenType = search::Tracer::Parse::TokenType; - - switch (type) - { - case TokenType::TOKEN_TYPE_POI: return "poi"; - case TokenType::TOKEN_TYPE_BUILDING: return "building"; - case TokenType::TOKEN_TYPE_STREET: return "street"; - case TokenType::TOKEN_TYPE_UNCLASSIFIED: return "unclassified"; - case TokenType::TOKEN_TYPE_VILLAGE: return "village"; - case TokenType::TOKEN_TYPE_CITY: return "city"; - case TokenType::TOKEN_TYPE_STATE: return "state"; - case TokenType::TOKEN_TYPE_COUNTRY: return "country"; - case TokenType::TOKEN_TYPE_POSTCODE: return "postcode"; - case TokenType::TOKEN_TYPE_COUNT: return "count"; - } -} - -void Init(string const & resource_path, string const & mwm_path) -{ - auto & platform = GetPlatform(); - - string countriesFile = COUNTRIES_FILE; - - if (!resource_path.empty()) - { - platform.SetResourceDir(resource_path); - countriesFile = base::JoinPath(resource_path, COUNTRIES_FILE); - } - - if (!mwm_path.empty()) - platform.SetWritableDirForTests(mwm_path); - - classificator::Load(); - - g_affiliations = make_unique(); - storage::CountryTree countries; - auto const rv = storage::LoadCountriesFromFile(countriesFile, countries, *g_affiliations); - CHECK(rv != -1, ("Can't load countries from:", countriesFile)); -} - struct Mercator { Mercator() = default; @@ -174,35 +130,14 @@ struct TraceResult bool m_isCategory = false; }; -unique_ptr CreateCountryInfoGetter() -{ - CHECK(g_affiliations.get(), ("init() was not called.")); - auto & platform = GetPlatform(); - auto infoGetter = storage::CountryInfoReader::CreateCountryInfoReader(platform); - infoGetter->SetAffiliations(&*g_affiliations); - return infoGetter; -} - -struct Context -{ - Context() : m_engine(m_dataSource, CreateCountryInfoGetter(), search::Engine::Params{}) {} - // todo(@pimenov) Choose right type for 'm_dataSource'. - FrozenDataSource m_dataSource; - search::tests_support::TestSearchEngine m_engine; -}; - struct SearchEngineProxy { - SearchEngineProxy() : m_context(make_shared()) + SearchEngineProxy() { - vector mwms; - platform::FindAllLocalMapsAndCleanup(numeric_limits::max() /* the latest version */, - mwms); - for (auto & mwm : mwms) - { - mwm.SyncWithDisk(); - m_context->m_dataSource.RegisterMap(mwm); - } + search::search_quality::InitDataSource(m_dataSource, "" /* mwmListPath */); + search::search_quality::InitAffiliations(m_affiliations); + m_engine = search::search_quality::InitSearchEngine(m_dataSource, m_affiliations, + "en" /* locale */, 1 /* numThreads */); } search::SearchParams MakeSearchParams(Params const & params) const @@ -223,8 +158,8 @@ struct SearchEngineProxy boost::python::list Query(Params const & params) const { - m_context->m_engine.SetLocale(params.m_locale); - search::tests_support::TestSearchRequest request(m_context->m_engine, MakeSearchParams(params)); + m_engine->SetLocale(params.m_locale); + search::tests_support::TestSearchRequest request(*m_engine, MakeSearchParams(params)); request.Run(); boost::python::list results; @@ -235,13 +170,13 @@ struct SearchEngineProxy boost::python::list Trace(Params const ¶ms) const { - m_context->m_engine.SetLocale(params.m_locale); + m_engine->SetLocale(params.m_locale); auto sp = MakeSearchParams(params); auto tracer = make_shared(); sp.m_tracer = tracer; - search::tests_support::TestSearchRequest request(m_context->m_engine, sp); + search::tests_support::TestSearchRequest request(*m_engine, sp); request.Run(); boost::python::list trs; @@ -261,8 +196,16 @@ struct SearchEngineProxy return trs; } - shared_ptr m_context; + storage::Affiliations m_affiliations; + FrozenDataSource m_dataSource; + unique_ptr m_engine; }; + +void Init(string const & dataPath, string const & mwmPath) +{ + classificator::Load(); + search::search_quality::SetPlatformDirs(dataPath, mwmPath); +} } // namespace BOOST_PYTHON_MODULE(pysearch) @@ -303,7 +246,7 @@ BOOST_PYTHON_MODULE(pysearch) .def_readwrite("is_category", &TraceResult::m_isCategory) .def("__repr__", &TraceResult::ToString); - class_("SearchEngine") + class_("SearchEngine") .def("query", &SearchEngineProxy::Query) .def("trace", &SearchEngineProxy::Trace); } diff --git a/search/pysearch/run_search_engine.py b/search/pysearch/run_search_engine.py index 6654fed227..e2df7d1851 100755 --- a/search/pysearch/run_search_engine.py +++ b/search/pysearch/run_search_engine.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2.7 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import print_function diff --git a/search/pysearch/run_search_server.py b/search/pysearch/run_search_server.py index f878322487..69c9ae1013 100755 --- a/search/pysearch/run_search_server.py +++ b/search/pysearch/run_search_server.py @@ -1,13 +1,13 @@ -#!/usr/bin/env python2.7 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import print_function -from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer +from http.server import BaseHTTPRequestHandler,HTTPServer import argparse import json import os import pysearch -import urlparse +import urllib.parse DIR = os.path.dirname(__file__) RESOURCE_PATH = os.path.realpath(os.path.join(DIR, '..', '..', 'data')) @@ -17,8 +17,8 @@ PORT=8080 class HTTPHandler(BaseHTTPRequestHandler): def do_GET(self): - result = urlparse.urlparse(self.path) - query = urlparse.parse_qs(result.query) + result = urllib.parse.urlparse(self.path) + query = urllib.parse.parse_qs(result.query) def sparam(name): return query[name][-1] @@ -52,7 +52,7 @@ class HTTPHandler(BaseHTTPRequestHandler): self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() - json.dump(responses, self.wfile) + self.wfile.write(json.dumps(responses).encode()) def main(args):