Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[search] Search in downloader by country names. #14081

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/assets/countries_names.txt
1 change: 1 addition & 0 deletions android/script/replace_links.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cp ../data/classificator.txt assets/
cp ../data/colors.txt assets/
cp ../data/copyright.html assets/
cp ../data/countries.txt assets/
cp ../data/countries_names.txt assets/
cp ../data/drules_proto_dark.bin assets/
cp ../data/drules_proto_clear.bin assets/
cp ../data/drules_proto_vehicle_dark.bin assets/
Expand Down
1 change: 1 addition & 0 deletions defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

#define COUNTRIES_FILE "countries.txt"
#define COUNTRIES_META_FILE "countries_meta.txt"
#define COUNTRIES_NAMES_FILE "countries_names.txt"
#define LEAP_SPEEDS_FILE "leap_speeds.json"

#define WORLD_FILE_NAME "World"
Expand Down
4 changes: 4 additions & 0 deletions iphone/Maps/Maps.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
34F73FA31E08300E00AC1FD6 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 34F73FA11E08300E00AC1FD6 /* Images.xcassets */; };
34F742321E0834F400AC1FD6 /* UIViewController+Navigation.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F742301E0834F400AC1FD6 /* UIViewController+Navigation.m */; };
34FE5A6F1F18F30F00BCA729 /* TrafficButtonArea.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34FE5A6D1F18F30F00BCA729 /* TrafficButtonArea.swift */; };
3970A6A825B64EE400CF5828 /* countries_names.txt in Resources */ = {isa = PBXBuildFile; fileRef = 3970A6A725B64EE300CF5828 /* countries_names.txt */; };
39CDE69123E1B6C8007CDA58 /* libge0.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 39CDE69023E1B6C8007CDA58 /* libge0.a */; };
3D0D2F7623D858BF00945C8D /* IsolinesTutorialBlur.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3D0D2F7523D858BF00945C8D /* IsolinesTutorialBlur.xib */; };
3D15ACEE2155117000F725D5 /* MWMObjectsCategorySelectorDataSource.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3D15ACED2155117000F725D5 /* MWMObjectsCategorySelectorDataSource.mm */; };
Expand Down Expand Up @@ -1416,6 +1417,7 @@
34FE4C431BCC013500066718 /* MWMMapWidgets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MWMMapWidgets.h; sourceTree = "<group>"; };
34FE4C441BCC013500066718 /* MWMMapWidgets.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MWMMapWidgets.mm; sourceTree = "<group>"; };
34FE5A6D1F18F30F00BCA729 /* TrafficButtonArea.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrafficButtonArea.swift; sourceTree = "<group>"; };
3970A6A725B64EE300CF5828 /* countries_names.txt */ = {isa = PBXFileReference; lastKnownFileType = text; name = countries_names.txt; path = ../../data/countries_names.txt; sourceTree = "<group>"; };
39CDE69023E1B6C8007CDA58 /* libge0.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libge0.a; sourceTree = BUILT_PRODUCTS_DIR; };
3D0D2F7523D858BF00945C8D /* IsolinesTutorialBlur.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = IsolinesTutorialBlur.xib; sourceTree = "<group>"; };
3D15ACED2155117000F725D5 /* MWMObjectsCategorySelectorDataSource.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MWMObjectsCategorySelectorDataSource.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2360,6 +2362,7 @@
29B97314FDCFA39411CA2CEA /* Maps */ = {
isa = PBXGroup;
children = (
3970A6A725B64EE300CF5828 /* countries_names.txt */,
47AEF83F2231249E00D20538 /* categories_brands.txt */,
471BBD92213038E000EB17C9 /* TipsAndTricks */,
FA36B8011540388B004560CC /* Bookmarks */,
Expand Down Expand Up @@ -5187,6 +5190,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3970A6A825B64EE400CF5828 /* countries_names.txt in Resources */,
47AEF8402231249E00D20538 /* categories_brands.txt in Resources */,
F6C3A1B221AC22810060EEC8 /* Alert 5.m4a in Resources */,
4560F585213D53C100CC736C /* shaders_metal.metallib in Resources */,
Expand Down
4 changes: 4 additions & 0 deletions map/framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,10 @@ void Framework::SelectSearchResult(search::Result const & result, bool animation
m_currentPlacePageInfo = {};
ASSERT(false, ("Suggests should not be here."));
return;
case Result::Type::DownloaderEntry:
m_currentPlacePageInfo = {};
ASSERT(false, ("Downloader entries should not be here."));
return;
}

m_currentPlacePageInfo = BuildPlacePageInfo(info);
Expand Down
8 changes: 6 additions & 2 deletions map/search_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
#include "map/discovery/discovery_search_params.hpp"
#include "map/everywhere_search_params.hpp"

#include "partners_api/booking_api.hpp"

#include "search/bookmarks/processor.hpp"
#include "search/geometry_utils.hpp"
#include "search/hotels_filter.hpp"
#include "search/tracer.hpp"
#include "search/utils.hpp"

#include "partners_api/booking_api.hpp"

#include "storage/downloader_search_params.hpp"

#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"
#include "platform/safe_callback.hpp"

#include "geometry/mercator.hpp"

#include "base/checked_cast.hpp"
#include "base/file_name_utils.hpp"
#include "base/string_utils.hpp"

#include "defines.hpp"

#include <algorithm>
#include <cmath>
#include <iterator>
Expand Down
2 changes: 2 additions & 0 deletions search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ set(
city_finder.cpp
city_finder.hpp
common.hpp
countries_names_index.cpp
countries_names_index.hpp
cuisine_filter.cpp
cuisine_filter.hpp
displayed_categories.cpp
Expand Down
89 changes: 89 additions & 0 deletions search/countries_names_index.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "search/countries_names_index.hpp"

#include "platform/platform.hpp"

#include "coding/file_reader.hpp"

#include "base/assert.hpp"

#include <fstream>
#include <set>
#include <sstream>

using namespace std;

namespace search
{
CountriesNamesIndex::CountriesNamesIndex()
{
ReadCountryNamesFromFile(m_countries);
BuildIndexFromTranslations();
}

void CountriesNamesIndex::CollectMatchingCountries(string const & query,
vector<storage::CountryId> & results)
{
set<size_t> ids;
auto insertId = [&ids](size_t id, bool /* exactMatch */) { ids.insert(id); };

vector<strings::UniString> tokens;
search::NormalizeAndTokenizeString(query, tokens);
search::Delimiters delims;
bool const lastTokenIsPrefix = !query.empty() && !delims(strings::LastUniChar(query));
for (size_t i = 0; i < tokens.size(); ++i)
{
auto const & token = tokens[i];
if (i + 1 == tokens.size() && lastTokenIsPrefix)
Retrieve<strings::PrefixDFAModifier<strings::LevenshteinDFA>>(token, insertId);
else
Retrieve<strings::LevenshteinDFA>(token, insertId);
}

// todo(@m) Do not bother with tf/idf for now.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно без tf/idf но хотя бы кол-во заматченых токенов учитывать

например:

cat data/countries_names.txt |grep Северная
  ru = Северная территория
  ru = Северная Ютландия
  ru = Северная Финляндия
  ru = Северная Голландия — Алкмар
  ru = Северная Голландия — Амстердам
  ru = Северная Голландия — Занстад
  ru = Северная Каролина
  ru = Северная Дакота
  ru = Северная Осетия
  ru = Северная Швеция
  ru = Северная Ирландия

если я правильно поняла, сейчас можно набрать в поиске "северная осетия", а в результатах ее не увидеть

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

но независимо от реализации я за то чтобы это было не в этом реквесте

results.clear();
for (auto id : ids)
{
CHECK_LESS(id, m_countries.size(), ());
results.emplace_back(m_countries[id].m_countryId);
}
}

void CountriesNamesIndex::ReadCountryNamesFromFile(vector<Country> & countries)
{
string contents;

GetPlatform().GetReader(COUNTRIES_NAMES_FILE)->ReadAsString(contents);
istringstream ifs(contents);

string line;
countries.clear();
while (getline(ifs, line))
{
if (line.empty())
continue;
strings::Trim(line);
if (line[0] == '[')
{
CHECK_EQUAL(line[line.size() - 1], ']', ());
countries.push_back({});
countries.back().m_countryId = line.substr(1, line.size() - 2);
continue;
}
auto pos = line.find('=');
if (pos == string::npos)
continue;
// Ignore the language code: the language sets differ for StringUtf8Multilang
// and for the translations used by this class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

после добавления в StringUtf8Multilang норвежского и индонезийского языки на которые переведны страны стали подмножеством языков в StringUtf8Multilang, но я не против игнорить, по крайней мере пока нет жалоб что работает не так ха-ха))

auto t = line.substr(pos + 1);
strings::Trim(t);
if (!countries.empty())
countries.back().m_doc.m_translations.push_back(t);
}
}

void CountriesNamesIndex::BuildIndexFromTranslations()
{
for (size_t i = 0; i < m_countries.size(); ++i)
m_index.Add(i, m_countries[i].m_doc);
}
} // namespace search
65 changes: 65 additions & 0 deletions search/countries_names_index.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include "search/base/mem_search_index.hpp"
#include "search/feature_offset_match.hpp"

#include "storage/storage_defines.hpp"

#include "indexer/search_string_utils.hpp"

#include "base/string_utils.hpp"

#include <cstddef>
#include <string>
#include <utility>
#include <vector>

namespace search
{
class CountriesNamesIndex
{
public:
struct Doc
{
template <typename Fn>
void ForEachToken(Fn && fn) const
{
for (auto const & s : m_translations)
fn(StringUtf8Multilang::kDefaultCode, NormalizeAndSimplifyString(s));
}

std::vector<std::string> m_translations;
};

CountriesNamesIndex();

void CollectMatchingCountries(std::string const & query,
std::vector<storage::CountryId> & results);

private:
struct Country
{
storage::CountryId m_countryId;
Doc m_doc;
};

// todo(@m) Almost the same as in bookmarks/processor.hpp.
template <typename DFA, typename Fn>
void Retrieve(strings::UniString const & s, Fn && fn) const
{
SearchTrieRequest<DFA> request;
request.m_names.emplace_back(BuildLevenshteinDFA(s));
request.m_langs.insert(StringUtf8Multilang::kDefaultCode);

MatchFeaturesInTrie(
request, m_index.GetRootIterator(), [](size_t id) { return true; } /* filter */,
std::forward<Fn>(fn));
}

void ReadCountryNamesFromFile(std::vector<Country> & countries);
void BuildIndexFromTranslations();

std::vector<Country> m_countries;
search_base::MemSearchIndex<size_t> m_index;
};
} // namespace search
73 changes: 50 additions & 23 deletions search/downloader_search_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@

#include "search/result.hpp"

#include "storage/country_info_getter.hpp"
#include "storage/storage.hpp"

#include "editor/editable_data_source.hpp"

#include "indexer/data_source.hpp"

#include "storage/country_info_getter.hpp"
#include "storage/storage.hpp"

#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include <set>
#include <string>
#include <utility>

namespace
{
bool GetGroupCountryId(storage::Storage const & storage, std::string & name)
{
auto const & synonyms = storage.GetCountryNameSynonyms();

if (storage.IsInnerNode(name))
return true;
auto const it = synonyms.find(name);
if (it == synonyms.end())
return false;
if (!storage.IsInnerNode(it->second))
return false;
name = it->second;
return true;
}

bool GetGroupCountryIdFromFeature(storage::Storage const & storage, FeatureType & ft,
std::string & name)
{
auto const & synonyms = storage.GetCountryNameSynonyms();
int8_t const langIndices[] = {StringUtf8Multilang::kEnglishCode,
StringUtf8Multilang::kDefaultCode,
StringUtf8Multilang::kInternationalCode};
Expand All @@ -30,15 +44,8 @@ bool GetGroupCountryIdFromFeature(storage::Storage const & storage, FeatureType
{
if (!ft.GetName(langIndex, name))
continue;
if (storage.IsInnerNode(name))
if (GetGroupCountryId(storage, name))
return true;
auto const it = synonyms.find(name);
if (it == synonyms.end())
continue;
if (!storage.IsInnerNode(it->second))
continue;
name = it->second;
return true;
}
return false;
}
Expand Down Expand Up @@ -66,6 +73,22 @@ void DownloaderSearchCallback::operator()(search::Results const & results)

for (auto const & result : results)
{
if (result.GetResultType() == search::Result::Type::DownloaderEntry)
{
std::string groupFeatureName = result.GetCountryId();
if (!GetGroupCountryId(m_storage, groupFeatureName))
continue;

storage::DownloaderSearchResult downloaderResult(groupFeatureName,
result.GetString() /* m_matchedName */);
if (uniqueResults.find(downloaderResult) == uniqueResults.end())
{
uniqueResults.insert(downloaderResult);
downloaderSearchResults.m_results.push_back(downloaderResult);
}
continue;
}

if (!result.HasPoint())
continue;

Expand Down Expand Up @@ -98,21 +121,25 @@ void DownloaderSearchCallback::operator()(search::Results const & results)
}
}
}
auto const & mercator = result.GetFeatureCenter();
storage::CountryId const & countryId = m_infoGetter.GetRegionCountryId(mercator);
if (countryId == storage::kInvalidCountryId)
continue;

storage::DownloaderSearchResult downloaderResult(countryId,
result.GetString() /* m_matchedName */);
if (uniqueResults.find(downloaderResult) == uniqueResults.end())
if (result.GetResultType() == search::Result::Type::LatLon)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else вместо result.GetResultType() == search::Result::Type::LatLon ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

или наоборот result.GetResultType() == search::Result::Type::LatLon и остальное в else?

{
uniqueResults.insert(downloaderResult);
downloaderSearchResults.m_results.push_back(downloaderResult);
auto const & mercator = result.GetFeatureCenter();
storage::CountryId const & countryId = m_infoGetter.GetRegionCountryId(mercator);
if (countryId == storage::kInvalidCountryId)
continue;

storage::DownloaderSearchResult downloaderResult(countryId,
result.GetString() /* m_matchedName */);
if (uniqueResults.find(downloaderResult) == uniqueResults.end())
{
uniqueResults.insert(downloaderResult);
downloaderSearchResults.m_results.push_back(downloaderResult);
}
continue;
}
}

downloaderSearchResults.m_query = m_params.m_query;
downloaderSearchResults.m_endMarker = results.IsEndMarker();

if (m_params.m_onResults)
Expand Down
Loading