From 115a0b9a078134c64165692b2a5fdcdc218accaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hora=C8=9Biu=20Mlendea?= Date: Thu, 23 Nov 2023 14:36:11 +0200 Subject: [PATCH] berber: Created a Berber transliterator --- .../BerberTransliteratorTests.cs | 25 ++++++ .../Service/Entities/Language.cs | 79 ++++++++++--------- .../Transliterators/BerberTransliterator.cs | 45 +++++++++++ 3 files changed, 110 insertions(+), 39 deletions(-) create mode 100644 TransliterationAPI.UnitTests/Service/Transliterators/BerberTransliteratorTests.cs create mode 100644 TransliterationAPI/Service/Transliterators/BerberTransliterator.cs diff --git a/TransliterationAPI.UnitTests/Service/Transliterators/BerberTransliteratorTests.cs b/TransliterationAPI.UnitTests/Service/Transliterators/BerberTransliteratorTests.cs new file mode 100644 index 0000000..bdb23ef --- /dev/null +++ b/TransliterationAPI.UnitTests/Service/Transliterators/BerberTransliteratorTests.cs @@ -0,0 +1,25 @@ +using NUnit.Framework; + +using TransliterationAPI.Service.Entities; +using TransliterationAPI.Service.Transliterators; + +namespace TransliterationAPI.UnitTests.Service.Transliterators +{ + public class BerberTransliteratorTests + { + private ITransliterator transliterator; + + [SetUp] + public void SetUp() + { + this.transliterator = new BerberTransliterator(); + } + + [Test] + [TestCase("ⴳⵓⵍⵎⵉⵎ ⴰⵙⵉⴼ ⵏⵓⵏ", "Gulmim Asif Nun")] + public void GivenATextInBerberScript_WhenTransliteratingIntoLatin_ThenTheCorrectTextIsReturned( + string arabicText, + string expectedTransliteratedText) + => Assert.That(transliterator.Transliterate(arabicText, Language.Berber), Is.EqualTo(expectedTransliteratedText)); + } +} diff --git a/TransliterationAPI/Service/Entities/Language.cs b/TransliterationAPI/Service/Entities/Language.cs index 9480237..9dd6ad5 100644 --- a/TransliterationAPI/Service/Entities/Language.cs +++ b/TransliterationAPI/Service/Entities/Language.cs @@ -11,49 +11,50 @@ public sealed class Language : IEquatable { static IDictionary entries; - public static Language Abkhaz => new Language("ab", "Abkhaz", nameof(TranslitterationDotComTransliterator)); - public static Language Adyghe => new Language("ady", "Adyghe", nameof(TranslitterationDotComTransliterator)); - public static Language AncientGreek => new Language("grc", "Ancient Greek", nameof(GreekTransliterator)); - public static Language AncientGreekDoric => new Language("grc-dor", "Ancient Doric Greek", nameof(GreekTransliterator)); - public static Language Arabic => new Language("ar", "Arabic", nameof(ArabicTransliterator)); - public static Language Armenian => new Language("hy", "Armenian", nameof(TranslitterationDotComTransliterator)); - public static Language Bashkir => new Language("ba", "Bashkir", nameof(TranslitterationDotComTransliterator)); - public static Language Belarussian => new Language("be", "Belarussian", nameof(CyrillicTransliterator)); - public static Language Bengali => new Language("bn", "Bengali", nameof(UshuaiaTransliterator)); - public static Language Bulgarian => new Language("bg", "Bulgarian", nameof(CyrillicTransliterator)); - public static Language Chinese => new Language("zh", "Chinese", nameof(PinyinTransliterator)); - public static Language Chuvash => new Language("cv", "Chuvash", nameof(CyrillicTransliterator)); - public static Language Coptic => new Language("cop", "Coptic", nameof(CopticTransliterator)); - public static Language EgyptianArabic => new Language("arz", "Egyptian Arabic", nameof(ArabicTransliterator)); - public static Language Georgian => new Language("ka", "Georgian", nameof(TranslitterationDotComTransliterator)); - public static Language Greek => new Language("el", "Greek", nameof(GreekTransliterator)); - public static Language Gujarati => new Language("gy", "Gujarati", nameof(GujaratiTransliterator)); - public static Language Hebrew => new Language("he", "Hebrew", nameof(HebrewTransliterator)); - public static Language Hindi => new Language("hi", "Hindi", nameof(UshuaiaTransliterator)); - public static Language Inuttitut => new Language("iu", "Inuttitut", nameof(TranslitterationDotComTransliterator)); - public static Language Japanese => new Language("ja", "Japanese", nameof(JapaneseTransliterator)); - public static Language Kannada => new Language("kn", "Kannada", nameof(UshuaiaTransliterator)); - public static Language Kazakh => new Language("kk", "Kazakh", nameof(CyrillicTransliterator)); - public static Language Korean => new Language("ko", "Korean", nameof(KoreanTransliterator)); - public static Language Kyrgyz => new Language("ky", "Kyrgyz", nameof(TranslitterationDotComTransliterator)); + public static Language Abkhaz => new Language("ab", nameof(Abkhaz), nameof(TranslitterationDotComTransliterator)); + public static Language Adyghe => new Language("ady", nameof(Adyghe), nameof(TranslitterationDotComTransliterator)); + public static Language AncientGreek => new Language("grc", $"Ancient {nameof(Greek)}", nameof(GreekTransliterator)); + public static Language AncientGreekDoric => new Language("grc-dor", $"Ancient Doric {nameof(Greek)}", nameof(GreekTransliterator)); + public static Language Arabic => new Language("ar", nameof(Arabic), nameof(ArabicTransliterator)); + public static Language Armenian => new Language("hy", nameof(Armenian), nameof(TranslitterationDotComTransliterator)); + public static Language Bashkir => new Language("ba", nameof(Bashkir), nameof(TranslitterationDotComTransliterator)); + public static Language Belarussian => new Language("be", nameof(Belarussian), nameof(CyrillicTransliterator)); + public static Language Bengali => new Language("bn", nameof(Bengali), nameof(UshuaiaTransliterator)); + public static Language Berber => new Language("ber", nameof(Berber), nameof(TranslitterationDotComTransliterator)); + public static Language Bulgarian => new Language("bg", nameof(Bulgarian), nameof(CyrillicTransliterator)); + public static Language Chinese => new Language("zh", nameof(Chinese), nameof(PinyinTransliterator)); + public static Language Chuvash => new Language("cv", nameof(Chuvash), nameof(CyrillicTransliterator)); + public static Language Coptic => new Language("cop", nameof(Coptic), nameof(CopticTransliterator)); + public static Language EgyptianArabic => new Language("arz", $"Egyptian {nameof(Arabic)}", nameof(ArabicTransliterator)); + public static Language Georgian => new Language("ka", nameof(Georgian), nameof(TranslitterationDotComTransliterator)); + public static Language Greek => new Language("el", nameof(Greek), nameof(GreekTransliterator)); + public static Language Gujarati => new Language("gy", nameof(Gujarati), nameof(GujaratiTransliterator)); + public static Language Hebrew => new Language("he", nameof(Hebrew), nameof(HebrewTransliterator)); + public static Language Hindi => new Language("hi", nameof(Hindi), nameof(UshuaiaTransliterator)); + public static Language Inuttitut => new Language("iu", nameof(Inuttitut), nameof(TranslitterationDotComTransliterator)); + public static Language Japanese => new Language("ja", nameof(Japanese), nameof(JapaneseTransliterator)); + public static Language Kannada => new Language("kn", nameof(Kannada), nameof(UshuaiaTransliterator)); + public static Language Kazakh => new Language("kk", nameof(Kazakh), nameof(CyrillicTransliterator)); + public static Language Korean => new Language("ko", nameof(Korean), nameof(KoreanTransliterator)); + public static Language Kyrgyz => new Language("ky", nameof(Kyrgyz), nameof(TranslitterationDotComTransliterator)); public static Language MacedonianSlavic => new Language("mk", "Macedonian Slavic", nameof(CyrillicTransliterator)); - public static Language MaghrebiArabic => new Language("ary", "Maghrebi Arabic", nameof(ArabicTransliterator)); - public static Language Malayalam => new Language("ml", "Malayalam", nameof(UshuaiaTransliterator)); - public static Language Marathi => new Language("mr", "Marathi", nameof(MarathiTransliterator)); - public static Language Mongol => new Language("mn", "Mongol", nameof(UshuaiaTransliterator)); + public static Language MaghrebiArabic => new Language("ary", $"Maghrebi {nameof(Arabic)}", nameof(ArabicTransliterator)); + public static Language Malayalam => new Language("ml", nameof(Malayalam), nameof(UshuaiaTransliterator)); + public static Language Marathi => new Language("mr", nameof(Marathi), nameof(MarathiTransliterator)); + public static Language Mongol => new Language("mn", nameof(Mongol), nameof(UshuaiaTransliterator)); public static Language OldChurchSlavonic => new Language("cu", "Old Church Slavonic", nameof(PodolakTransliterator)); - public static Language Ossetic => new Language("os", "Ossetic", nameof(TranslitterationDotComTransliterator)); - public static Language Russian => new Language("ru", "Russian", nameof(CyrillicTransliterator)); - public static Language Sanskrit => new Language("sa", "Sanskrit", nameof(UshuaiaTransliterator)); - public static Language Serbian => new Language("sr", "Serbian", nameof(CyrillicTransliterator)); - public static Language SerbianCyrillic => new Language("sr-ec", "Serbian", nameof(CyrillicTransliterator)); + public static Language Ossetic => new Language("os", nameof(Ossetic), nameof(TranslitterationDotComTransliterator)); + public static Language Russian => new Language("ru", nameof(Russian), nameof(CyrillicTransliterator)); + public static Language Sanskrit => new Language("sa", nameof(Sanskrit), nameof(UshuaiaTransliterator)); + public static Language Serbian => new Language("sr", nameof(Serbian), nameof(CyrillicTransliterator)); + public static Language SerbianCyrillic => new Language("sr-ec", nameof(Serbian), nameof(CyrillicTransliterator)); public static Language SerboCroatian => new Language("sh", "Serbo-Croatian", nameof(CyrillicTransliterator)); public static Language SimplifiedChinese => new Language("zh-hans", "Simplified Chinese", nameof(PinyinTransliterator)); - public static Language Sinhala => new Language("si", "Sinhala", nameof(UshuaiaTransliterator)); - public static Language Tamil => new Language("ta", "Tamil", nameof(UshuaiaTransliterator)); - public static Language Telugu => new Language("te", "Telugu", nameof(UshuaiaTransliterator)); - public static Language Udmurt => new Language("udm", "Udmurt", nameof(TranslitterationDotComTransliterator)); - public static Language Ukrainian => new Language("uk", "Ukrainian", nameof(CyrillicTransliterator)); + public static Language Sinhala => new Language("si", nameof(Sinhala), nameof(UshuaiaTransliterator)); + public static Language Tamil => new Language("ta", nameof(Tamil), nameof(UshuaiaTransliterator)); + public static Language Telugu => new Language("te", nameof(Telugu), nameof(UshuaiaTransliterator)); + public static Language Udmurt => new Language("udm", nameof(Udmurt), nameof(TranslitterationDotComTransliterator)); + public static Language Ukrainian => new Language("uk", nameof(Ukrainian), nameof(CyrillicTransliterator)); public static Language WesternArmenian => new Language("hyw", "Western Armenian", nameof(TranslitterationDotComTransliterator)); static Language() diff --git a/TransliterationAPI/Service/Transliterators/BerberTransliterator.cs b/TransliterationAPI/Service/Transliterators/BerberTransliterator.cs new file mode 100644 index 0000000..c9897b5 --- /dev/null +++ b/TransliterationAPI/Service/Transliterators/BerberTransliterator.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using NuciExtensions; +using TransliterationAPI.Service.Entities; + +namespace TransliterationAPI.Service.Transliterators +{ + public class BerberTransliterator : ITransliterator + { + Dictionary transliterationTable; + public BerberTransliterator() + { + transliterationTable = new Dictionary + { + { "ⴰ", "a" }, + { "ⴳ", "g" }, + { "ⴼ", "f" }, + { "ⵉ", "i" }, + { "ⵍ", "l" }, + { "ⵎ", "m" }, + { "ⵏ", "n" }, + { "ⵓ", "u" }, + { "ⵙ", "s" }, + }; + } + + public string Transliterate(string text, Language language) + { + if (!language.Equals(Language.Berber)) + { + throw new ArgumentException($"The {language.Name} language is not supported by the {nameof(BerberTransliterator)}"); + } + + string transliteratedText = text; + + foreach (string character in transliterationTable.Keys) + { + transliteratedText = Regex.Replace(transliteratedText, character, transliterationTable[character]); + } + + return transliteratedText.ToTitleCase(); + } + } +}