Skip to content

Commit

Permalink
Merge pull request #65 from hmlendea/berber
Browse files Browse the repository at this point in the history
Created a transliterator for `Berber`
  • Loading branch information
hmlendea authored Dec 6, 2023
2 parents 4cbedd8 + 115a0b9 commit 4171b1e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
79 changes: 40 additions & 39 deletions TransliterationAPI/Service/Entities/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,50 @@ public sealed class Language : IEquatable<Language>
{
static IDictionary<string, Language> 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()
Expand Down
45 changes: 45 additions & 0 deletions TransliterationAPI/Service/Transliterators/BerberTransliterator.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> transliterationTable;
public BerberTransliterator()
{
transliterationTable = new Dictionary<string, string>
{
{ "", "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();
}
}
}

0 comments on commit 4171b1e

Please sign in to comment.