Corrected priority of transliteration.

This commit is contained in:
Daria Volvenkova 2017-03-28 15:16:47 +03:00
parent 2d5a2fd236
commit 8c3d4f78c7
6 changed files with 65 additions and 57 deletions

View file

@ -503,7 +503,8 @@ void FeatureType::GetPreferredNames(string & primary, string & secondary) const
ParseCommon();
auto const deviceLang = StringUtf8Multilang::GetLangIndex(languages::GetCurrentNorm());
::GetPreferredNames(mwmInfo->GetRegionData(), GetNames(), deviceLang, primary, secondary);
::GetPreferredNames(mwmInfo->GetRegionData(), GetNames(), deviceLang, true /* allowTranslit */,
primary, secondary);
}
void FeatureType::GetReadableName(string & name) const
@ -518,7 +519,7 @@ void FeatureType::GetReadableName(string & name) const
ParseCommon();
auto const deviceLang = StringUtf8Multilang::GetLangIndex(languages::GetCurrentNorm());
::GetReadableName(mwmInfo->GetRegionData(), GetNames(), deviceLang, name);
::GetReadableName(mwmInfo->GetRegionData(), GetNames(), deviceLang, true /* allowTranslit */, name);
}
string FeatureType::GetHouseNumber() const

View file

@ -30,7 +30,7 @@ void GetMwmLangName(feature::RegionData const & regionData, StringUtf8Multilang
}
}
void GetTransliteratedName(feature::RegionData const & regionData, StringUtf8Multilang const & src, string & out)
bool GetTransliteratedName(feature::RegionData const & regionData, StringUtf8Multilang const & src, string & out)
{
vector<int8_t> mwmLangCodes;
regionData.GetLanguages(mwmLangCodes);
@ -42,15 +42,17 @@ void GetTransliteratedName(feature::RegionData const & regionData, StringUtf8Mul
{
out = Transliteration::GetInstance().Transliterate(srcName, code);
if (!out.empty())
return;
return true;
}
}
if (!mwmLangCodes.empty() && src.GetString(StringUtf8Multilang::kDefaultCode, srcName))
out = Transliteration::GetInstance().Transliterate(srcName, mwmLangCodes[0]);
return !out.empty();
}
void GetBestName(StringUtf8Multilang const & src, vector<int8_t> const & priorityList, string & out)
bool GetBestName(StringUtf8Multilang const & src, vector<int8_t> const & priorityList, string & out)
{
out.clear();
auto bestIndex = priorityList.size();
auto const findAndSet = [](vector<int8_t> const & langs, int8_t const code, string const & name,
@ -80,6 +82,8 @@ void GetBestName(StringUtf8Multilang const & src, vector<int8_t> const & priorit
{
out = out.substr(0, out.find_first_of(','));
}
return !out.empty();
}
} // namespace
@ -224,7 +228,7 @@ int GetFeatureViewportScale(TypesHolder const & types)
}
void GetPreferredNames(RegionData const & regionData, StringUtf8Multilang const & src,
int8_t const deviceLang, string & primary, string & secondary)
int8_t const deviceLang, bool allowTranslit, string & primary, string & secondary)
{
primary.clear();
secondary.clear();
@ -232,31 +236,31 @@ void GetPreferredNames(RegionData const & regionData, StringUtf8Multilang const
if (src.IsEmpty())
return;
GetBestName(src, {deviceLang, StrUtf8::kInternationalCode}, primary);
if (primary.empty())
if (!GetBestName(src, {deviceLang, StrUtf8::kInternationalCode, StrUtf8::kEnglishCode}, primary) &&
allowTranslit)
{
GetTransliteratedName(regionData, src, primary);
if (primary.empty())
GetBestName(src, {StrUtf8::kEnglishCode}, primary);
}
vector<int8_t> secondaryCodes = {StrUtf8::kDefaultCode,
StrUtf8::kInternationalCode};
vector<int8_t> mwmLangCodes;
regionData.GetLanguages(mwmLangCodes);
vector<int8_t> secondaryCodes = {StrUtf8::kDefaultCode,
StrUtf8::kInternationalCode};
secondaryCodes.insert(secondaryCodes.end(), mwmLangCodes.begin(), mwmLangCodes.end());
secondaryCodes.push_back(StrUtf8::kEnglishCode);
GetBestName(src, secondaryCodes, secondary);
if (primary.empty())
primary.swap(secondary);
primary.swap(secondary);
else if (!secondary.empty() && primary.find(secondary) != string::npos)
secondary.clear();
}
void GetReadableName(RegionData const & regionData, StringUtf8Multilang const & src,
int8_t const deviceLang, string & out)
int8_t const deviceLang, bool allowTranslit, string & out)
{
out.clear();
@ -267,25 +271,24 @@ void GetReadableName(RegionData const & regionData, StringUtf8Multilang const &
// If MWM contains user's language.
bool const preferDefault = regionData.HasLanguage(deviceLang);
if (preferDefault)
codes = {deviceLang, StrUtf8::kDefaultCode, StrUtf8::kInternationalCode};
codes = {deviceLang, StrUtf8::kDefaultCode, StrUtf8::kInternationalCode, StrUtf8::kEnglishCode};
else
codes = {deviceLang, StrUtf8::kInternationalCode};
codes = {deviceLang, StrUtf8::kInternationalCode, StrUtf8::kEnglishCode};
GetBestName(src, codes, out);
if (out.empty())
if (GetBestName(src, codes, out))
return;
if (allowTranslit && GetTransliteratedName(regionData, src, out))
return;
if (!preferDefault)
{
GetTransliteratedName(regionData, src, out);
if (out.empty())
{
if (preferDefault)
codes = {StrUtf8::kEnglishCode};
else
codes = {StrUtf8::kEnglishCode, StrUtf8::kDefaultCode};
GetBestName(src, codes, out);
if (out.empty())
GetMwmLangName(regionData, src, out);
}
codes = {StrUtf8::kDefaultCode};
if (GetBestName(src, codes, out))
return;
}
GetMwmLangName(regionData, src, out);
}
} // namespace feature

View file

@ -19,6 +19,7 @@ namespace feature
/// - device language name;
/// - international name;
/// - english name.
/// - transliterated name.
/// Secondary name using priority:
/// - default name;
/// - international name;
@ -27,20 +28,22 @@ namespace feature
/// In case when primary name is empty it will be propagated from secondary and secondary will be
/// cleared. In case when primary name contains secondary name then secondary will be cleared.
void GetPreferredNames(RegionData const & regionData, StringUtf8Multilang const & src,
int8_t const deviceLang, string & primary, string & secondary);
int8_t const deviceLang, bool allowTranslit, string & primary, string & secondary);
/// When MWM contains user's language, the priority is the following:
/// - device language name;
/// - default name;
/// - international name;
/// - english name;
/// - transliterated name;
/// - country language name.
/// When MWM does not contain user's language, the priority is the following:
/// - device language name;
/// - international name;
/// - english name;
/// - transliterated name;
/// - default name;
/// - country language name.
void GetReadableName(RegionData const & regionData, StringUtf8Multilang const & src,
int8_t const deviceLang, string & out);
int8_t const deviceLang, bool allowTranslit, string & out);
} // namespace feature

View file

@ -21,12 +21,13 @@ UNIT_TEST(GetPrefferedNames)
int8_t deviceLang = StrUtf8::GetLangIndex("ru");
string primary, secondary;
bool const allowTranslit = false;
{
StrUtf8 src;
src.AddString("fr", "fr name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "", ());
TEST_EQUAL(secondary, "", ());
@ -36,7 +37,7 @@ UNIT_TEST(GetPrefferedNames)
StrUtf8 src;
src.AddString("default", "default name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "default name", ());
TEST_EQUAL(secondary, "", ());
@ -47,7 +48,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("default", "default name");
src.AddString("en", "en name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "en name", ());
TEST_EQUAL(secondary, "default name", ());
@ -59,7 +60,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("en", "en name");
src.AddString("ru", "ru name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "ru name", ());
TEST_EQUAL(secondary, "default name", ());
@ -72,7 +73,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("ru", "same name");
src.AddString("int_name", "int name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "same name", ());
TEST_EQUAL(secondary, "", ());
@ -86,7 +87,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("int_name", "int name");
src.AddString("de", "de name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "ru name", ());
TEST_EQUAL(secondary, "default name", ());
@ -101,7 +102,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("de", "de name");
src.AddString("ko", "ko name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "ru name", ());
TEST_EQUAL(secondary, "default name", ());
@ -115,7 +116,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("de", "de name");
src.AddString("ko", "ko name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "int name", ());
TEST_EQUAL(secondary, "default name", ());
@ -128,7 +129,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("de", "de name");
src.AddString("ko", "ko name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "int name", ());
TEST_EQUAL(secondary, "", ());
@ -140,7 +141,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("de", "de name");
src.AddString("ko", "ko name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "en name", ());
TEST_EQUAL(secondary, "de name", ());
@ -151,7 +152,7 @@ UNIT_TEST(GetPrefferedNames)
src.AddString("en", "en name");
src.AddString("ko", "ko name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "en name", ());
TEST_EQUAL(secondary, "ko name", ());
@ -161,7 +162,7 @@ UNIT_TEST(GetPrefferedNames)
StrUtf8 src;
src.AddString("en", "en name");
feature::GetPreferredNames(regionData, src, deviceLang, primary, secondary);
feature::GetPreferredNames(regionData, src, deviceLang, allowTranslit, primary, secondary);
TEST_EQUAL(primary, "en name", ());
TEST_EQUAL(secondary, "", ());
@ -174,13 +175,14 @@ UNIT_TEST(GetReadableName)
regionData.SetLanguages({"de", "ko"});
int8_t deviceLang = StrUtf8::GetLangIndex("ru");
bool const allowTranslit = false;
string name;
{
StrUtf8 src;
src.AddString("fr", "fr name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "", ());
}
@ -189,7 +191,7 @@ UNIT_TEST(GetReadableName)
StrUtf8 src;
src.AddString("ko", "ko name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "ko name", ());
}
@ -199,7 +201,7 @@ UNIT_TEST(GetReadableName)
src.AddString("ko", "ko name");
src.AddString("de", "de name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "de name", ());
}
@ -210,7 +212,7 @@ UNIT_TEST(GetReadableName)
src.AddString("de", "de name");
src.AddString("default", "default name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "default name", ());
}
@ -222,7 +224,7 @@ UNIT_TEST(GetReadableName)
src.AddString("default", "default name");
src.AddString("en", "en name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "en name", ());
}
@ -235,7 +237,7 @@ UNIT_TEST(GetReadableName)
src.AddString("en", "en name");
src.AddString("int_name", "int name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "int name", ());
}
@ -249,7 +251,7 @@ UNIT_TEST(GetReadableName)
src.AddString("int_name", "int name");
src.AddString("ru", "ru name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "ru name", ());
}
@ -264,7 +266,7 @@ UNIT_TEST(GetReadableName)
src.AddString("en", "en name");
src.AddString("int_name", "int name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "de name", ());
}
@ -276,7 +278,7 @@ UNIT_TEST(GetReadableName)
src.AddString("en", "en name");
src.AddString("int_name", "int name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "default name", ());
}
@ -287,7 +289,7 @@ UNIT_TEST(GetReadableName)
src.AddString("en", "en name");
src.AddString("int_name", "int name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "int name", ());
}
@ -297,7 +299,7 @@ UNIT_TEST(GetReadableName)
src.AddString("ko", "ko name");
src.AddString("en", "en name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "en name", ());
}
@ -306,7 +308,7 @@ UNIT_TEST(GetReadableName)
StrUtf8 src;
src.AddString("ko", "ko name");
feature::GetReadableName(regionData, src, deviceLang, name);
feature::GetReadableName(regionData, src, deviceLang, allowTranslit, name);
TEST_EQUAL(name, "ko name", ());
}

View file

@ -11,7 +11,6 @@ UNIT_TEST(Transliteration_CompareSamples)
Transliteration & translit = Transliteration::GetInstance();
translit.Init(GetPlatform().ResourcesDir());
TEST_EQUAL("rì běn yǔ", translit.Transliterate("日本語", StringUtf8Multilang::GetLangIndex("ja")), ());
TEST_EQUAL("ạlʿrbyẗ", translit.Transliterate("العربية", StringUtf8Multilang::GetLangIndex("ar")), ());
TEST_EQUAL("Russkiy", translit.Transliterate("Русский", StringUtf8Multilang::GetLangIndex("ru")), ());
TEST_EQUAL("zhōng wén", translit.Transliterate("中文", StringUtf8Multilang::GetLangIndex("zh")), ());

View file

@ -64,7 +64,7 @@ string Info::GetTitle() const
auto const mwmInfo = GetID().m_mwmId.GetInfo();
if (mwmInfo)
feature::GetReadableName(mwmInfo->GetRegionData(), m_name, deviceLang, name);
feature::GetReadableName(mwmInfo->GetRegionData(), m_name, deviceLang, true /* allowTranslit */, name);
return name;
}