Skip to content

Instantly share code, notes, and snippets.

@0x49D1
Last active October 10, 2016 07:26
Show Gist options
  • Save 0x49D1/82bbbc97eb7924f93b642ce94c606b60 to your computer and use it in GitHub Desktop.
Save 0x49D1/82bbbc97eb7924f93b642ce94c606b60 to your computer and use it in GitHub Desktop.
Snippet to help make Eng -> Geo and Geo -> Eng transliteration in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public static class GeoEngTranslit
{
static readonly Dictionary<char, string> EngGeoDictionary = new Dictionary<char, string>()
{
{'a',"ა"},
{'A',"ა"},
{'B',"ბ"},
{'b',"ბ"},
{'g',"გ"},
{'G',"(გ|ღ)"},
{'d',"დ"},
{'D',"დ"},
{'e',"ე"},
{'E',"ე"},
{'v',"ვ"},
{'V',"ვ"},
{'i',"ი"},
{'I',"ი"},
{'k',"(კ|ქ)"},
{'K',"(კ|ქ)"},
{'l',"ლ"},
{'L',"ლ"},
{'m',"მ"},
{'M',"მ"},
{'n',"ნ"},
{'N',"ნ"},
{'o',"ო"},
{'O',"ო"},
{'p',"(პ|ფ)"},
{'P',"(პ|ფ)"},
{'J',"(ჟ|ჯ)"},
{'j',"ჯ"},
{'s',"ს"},
{'t',"(ტ|თ)"},
{'T',"(თ|ტ)"},
{'u',"უ"},
{'U',"უ"},
{'f',"ფ"},
{'F',"ფ"},
{'q',"(ქ|კ)"},
{'Q',"(ქ|კ)"},
{'R',"(ღ|რ)"},
{'r',"რ"},
{'y',"ყ"},
{'S',"(შ|ს)"},
{'C',"ჩ"},
{'c',"(ც|ჩ)"},
{'Z',"(ძ|ზ)"},
{'z',"ზ"},
{'w',"წ"},
{'W',"(ჭ|წ)"},
{'x',"ხ"},
{'X',"ხ"},
{'h',"ჰ"},
{'H',"ჰ"}
};
/// <summary>
/// Method to translit entered string from Geo -> Eng
/// </summary>
/// <param name="text">String to translit</param>
/// <returns>Transliterated string</returns>
public static string TranslitGeorgianToEnglish(string text)
{
StringBuilder engResult = new StringBuilder();
foreach (var t in text)
{
char c = EngGeoDictionary.FirstOrDefault(a => a.Value.First() == t || a.Value.StartsWith("(" + t)).Key;
engResult.Append(c != default(Char) ? c.ToString() : t.ToString());
}
return engResult.ToString();
}
/// <summary>
/// Method to translit entered string from Eng -> Geo.
/// </summary>
/// <param name="text">String to translit</param>
/// <returns>Transliterated string</returns>
public static string TranslitEnglishToGeorgian(string text)
{
StringBuilder geoResult = new StringBuilder();
foreach (var t in text)
{
char characterToAppend = t;
string c = EngGeoDictionary.FirstOrDefault(a => a.Key == t).Value;
if (!string.IsNullOrEmpty(c))
characterToAppend = c.Contains('(') ? c[1] : c[0];
geoResult.Append(characterToAppend);
}
return geoResult.ToString();
}
/// <summary>
/// Method to translit entered string from Eng -> Geo. Can be used in regexes
/// </summary>
/// <param name="text">String to translit</param>
/// <returns>Transliterated string</returns>
public static string TranslitEnglishToGeorgianForRegex(string text)
{
StringBuilder geoResult = new StringBuilder();
bool tryToTranslit = false; // This flag shows if there were translited characters or no. If there were no characters for translit- lets do not translit
for (int i = 0; i < text.Length; i++)
{
char letter = text[i];
if (EngGeoDictionary.ContainsKey(letter))
{
if (i + 1 < text.Length)
{
char nextLetter = text[i + 1];
if ((letter == 'S' || letter == 's')
&& (nextLetter == 'H' || nextLetter == 'h'))
{
geoResult.Append("(შ|სშ|სხ|შჰ|შხ)");
i++;
}
else if ((letter == 'G' || letter == 'g')
&& (nextLetter == 'H' || nextLetter == 'h'))
{
geoResult.Append("(ღ|ღხ|ღჰ|გხ|გჰ)");
i++;
}
else if ((letter == 'K' || letter == 'k')
&& (nextLetter == 'H' || nextLetter == 'h'))
{
geoResult.Append("(ხ|კხ|კჰ)");
i++;
}
else if ((letter == 'Z' || letter == 'z')
&& (nextLetter == 'H' || nextLetter == 'h'))
{
geoResult.Append("(ჟ|ზხ|ზჰ|ძხ)");
i++;
}
else if ((letter == 'D' || letter == 'd')
&& (nextLetter == 'z' || nextLetter == 'Z'))
{
geoResult.Append("(ძ|დზ|დძ)");
i++;
}
else if ((letter == 'c' || letter == 'C')
&& (nextLetter == 'h' || nextLetter == 'H'))
{
geoResult.Append("(ჩ|ჭ|ცხ|ცჰ)");
i++;
}
else if ((letter == 't' || letter == 'T')
&& (nextLetter == 's' || nextLetter == 'S'))
{
geoResult.Append("(ც|ტს|თს|თშ|ტშ|წ)");
i++;
}
else
{
geoResult.Append(EngGeoDictionary[letter]);
}
}
else
{
geoResult.Append(EngGeoDictionary[letter]);
}
tryToTranslit = true;
}
else
{
geoResult.Append(letter);
}
}
if (!tryToTranslit)
return "";
return geoResult.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment