Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/search
diff options
context:
space:
mode:
authortatiana-yan <tatiana.kondakova@gmail.com>2019-03-18 17:37:09 +0300
committermpimenov <mpimenov@users.noreply.github.com>2019-03-19 18:31:55 +0300
commit85faa2de2fa22a53cf99154e3333c4c194ceaf1c (patch)
treec8f88f14245f32c3382036d8669efe4f7bd09ebd /search
parent750aaacada4d3b6373e67f98418adf1f2b22d6a8 (diff)
[search] Add IsCategorialRequestFuzzy method.
Diffstat (limited to 'search')
-rw-r--r--search/search_integration_tests/utils_test.cpp16
-rw-r--r--search/utils.cpp49
-rw-r--r--search/utils.hpp3
3 files changed, 66 insertions, 2 deletions
diff --git a/search/search_integration_tests/utils_test.cpp b/search/search_integration_tests/utils_test.cpp
index 15fb0e68a2..16a5fe8cc0 100644
--- a/search/search_integration_tests/utils_test.cpp
+++ b/search/search_integration_tests/utils_test.cpp
@@ -95,5 +95,21 @@ UNIT_CLASS_TEST(SearchUtilsTest, Utils)
// All.
testTypes(typesFood, 3);
}
+
+UNIT_TEST(IsCategorialRequestFuzzy)
+{
+ auto const isHotelRequest = [](string const & q) { return IsCategorialRequestFuzzy(q, "hotel"); };
+
+ TEST(isHotelRequest("hotel"), ());
+ TEST(isHotelRequest("Hotel"), ());
+ TEST(isHotelRequest("motel"), ());
+ TEST(isHotelRequest("отель"), ());
+ TEST(isHotelRequest("гостиница"), ());
+ TEST(isHotelRequest("гостиница москва"), ());
+ TEST(isHotelRequest("new york hotel"), ());
+ TEST(!isHotelRequest("new york where to stay"), ());
+ TEST(!isHotelRequest("where to stay"), ());
+ TEST(!isHotelRequest("random request"), ());
+}
} // namespace
} // namespace search
diff --git a/search/utils.cpp b/search/utils.cpp
index 7f570eba75..bcf8e981e4 100644
--- a/search/utils.cpp
+++ b/search/utils.cpp
@@ -60,8 +60,8 @@ vector<uint32_t> GetCategoryTypes(string const & name, string const & locale,
locales.Insert(static_cast<uint64_t>(code));
vector<strings::UniString> tokens;
- SplitUniString(search::NormalizeAndSimplifyString(name), base::MakeBackInsertFunctor(tokens),
- search::Delimiters());
+ SplitUniString(NormalizeAndSimplifyString(name), base::MakeBackInsertFunctor(tokens),
+ Delimiters());
FillCategories(QuerySliceOnRawStrings<vector<strings::UniString>>(tokens, {} /* prefix */),
locales, categories, types);
@@ -125,4 +125,49 @@ void ForEachOfTypesInRect(DataSource const & dataSource, vector<uint32_t> const
});
}
}
+
+bool IsCategorialRequestFuzzy(string const & query, string const & categoryEn)
+{
+ auto const & catHolder = GetDefaultCategories();
+ auto const types = GetCategoryTypes(categoryEn, "en", catHolder);
+
+ vector<QueryParams::String> queryTokens;
+ SplitUniString(NormalizeAndSimplifyString(query), base::MakeBackInsertFunctor(queryTokens),
+ Delimiters());
+
+ bool isCategorialRequest = false;
+ for (auto const type : types)
+ {
+ if (isCategorialRequest)
+ return true;
+
+ catHolder.ForEachNameByType(
+ type, [&](CategoriesHolder::Category::Name const & categorySynonym) {
+ if (isCategorialRequest)
+ return;
+ vector<QueryParams::String> categoryTokens;
+ SplitUniString(NormalizeAndSimplifyString(categorySynonym.m_name),
+ base::MakeBackInsertFunctor(categoryTokens), Delimiters());
+ for (size_t start = 0; start < queryTokens.size(); ++start)
+ {
+ bool found = true;
+ for (size_t i = 0; i < categoryTokens.size() && start + i < queryTokens.size(); ++i)
+ {
+ if (queryTokens[start + i] != categoryTokens[i])
+ {
+ found = false;
+ break;
+ }
+ }
+ if (found)
+ {
+ isCategorialRequest = true;
+ break;
+ }
+ }
+ });
+ }
+
+ return isCategorialRequest;
+}
} // namespace search
diff --git a/search/utils.hpp b/search/utils.hpp
index 2e225181ec..584c18bffd 100644
--- a/search/utils.hpp
+++ b/search/utils.hpp
@@ -130,4 +130,7 @@ using FeatureIndexCallback = std::function<void(FeatureID const &)>;
// Applies |fn| to each feature index of type from |types| in |rect|.
void ForEachOfTypesInRect(DataSource const & dataSource, std::vector<uint32_t> const & types,
m2::RectD const & rect, FeatureIndexCallback const & fn);
+
+// Returns true iff |query| contains |categoryEn| synonym.
+bool IsCategorialRequestFuzzy(std::string const & query, std::string const & categoryEn);
} // namespace search