Last active
May 9, 2024 12:34
-
-
Save TheVeryStarlk/fdd5a592c7e515d1802184a335950bfa to your computer and use it in GitHub Desktop.
An extension method that shapes separated Arabic letters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
const string original = "سماء عماد لها وارض تسير وفوقها قمم الروابي فضاء لا انتهاء له وشمس تضيء بحسبة بين السحاب فشد كيانك الادنى برب تنال بقربه شرف الجناب!"; | |
Console.OutputEncoding = Encoding.UTF8; | |
Console.WriteLine($"Before: \"{original}\", \nafter: \"{original.ShapeArabic()}\"."); | |
internal static class StringExtensions | |
{ | |
// Taken from https://en.wikipedia.org/wiki/Arabic_script_in_Unicode#Contextual_forms. | |
private static readonly Dictionary<char, (char Final, char Middle, char Initial)> Letters = new() | |
{ | |
{ 'ا', ('ﺎ', '\0', '\0') }, | |
{ 'ب', ('ﺐ', 'ﺒ', 'ﺑ') }, | |
{ 'ت', ('ﺖ', 'ﺘ', 'ﺗ') }, | |
{ 'ث', ('ﺚ', 'ﺜ', 'ﺛ') }, | |
{ 'ج', ('ﺞ', 'ﺠ', 'ﺟ') }, | |
{ 'ح', ('ﺢ', 'ﺤ', 'ﺣ') }, | |
{ 'خ', ('ﺦ', 'ﺨ', 'ﺧ') }, | |
{ 'د', ('ﺪ', '\0', '\0') }, | |
{ 'ذ', ('ﺬ', '\0', '\0') }, | |
{ 'ر', ('ﺮ', '\0', '\0') }, | |
{ 'ز', ('ﺰ', '\0', '\0') }, | |
{ 'س', ('ﺲ', 'ﺴ', 'ﺳ') }, | |
{ 'ش', ('ﺶ', 'ﺸ', 'ﺷ') }, | |
{ 'ص', ('ﺺ', 'ﺼ', 'ﺻ') }, | |
{ 'ض', ('ﺾ', 'ﻀ', 'ﺿ') }, | |
{ 'ط', ('ﻂ', 'ﻄ', 'ﻃ') }, | |
{ 'ظ', ('ﻆ', 'ﻈ', 'ﻇ') }, | |
{ 'ع', ('ﻊ', 'ﻌ', 'ﻋ') }, | |
{ 'غ', ('ﻎ', 'ﻐ', 'ﻏ') }, | |
{ 'ف', ('ﻒ', 'ﻔ', 'ﻓ') }, | |
{ 'ق', ('ﻖ', 'ﻘ', 'ﻗ') }, | |
{ 'ك', ('ﻚ', 'ﻜ', 'ﻛ') }, | |
{ 'ل', ('ﻞ', 'ﻠ', 'ﻟ') }, | |
{ 'م', ('ﻢ', 'ﻤ', 'ﻣ') }, | |
{ 'ن', ('ﻦ', 'ﻨ', 'ﻧ') }, | |
{ 'ه', ('ﻪ', 'ﻬ', 'ﻫ') }, | |
{ 'و', ('ﻮ', '\0', '\0') }, | |
{ 'ي', ('ﻲ', 'ﻴ', 'ﻳ') }, | |
{ 'ﺁ', ('ﺂ', '\0', '\0') }, | |
{ 'ة', ('ﺔ', '\0', '\0') }, | |
{ 'ى', ('ﻰ', '\0', '\0') } | |
}; | |
/// <summary> | |
/// Shapes separated Arabic letters into words. | |
/// </summary> | |
/// <example> | |
/// Shapes text like: "ﻝﻩﺱ" to: "سهل". | |
/// </example> | |
/// <remarks> | |
/// Letters that have vocalization (تشكيل) will not be shaped correctly. | |
/// </remarks> | |
/// <param name="original">The original, unshaped Arabic <see cref="string"/>.</param> | |
/// <returns>The fully shaped Arabic <see cref="string"/>.</returns> | |
public static string ShapeArabic(this string original) | |
{ | |
if (original.Length < 2) | |
{ | |
return original; | |
} | |
return string.Create( | |
original.Length, | |
original, | |
(span, @string) => | |
{ | |
var reversed = original.Length - 1; | |
for (var index = 0; index < @string.Length; index++) | |
{ | |
var current = @string[reversed]; | |
if (!Letters.TryGetValue(current, out var value)) | |
{ | |
span[index] = current; | |
reversed--; | |
continue; | |
} | |
// Try to find if neighbouring letters have initial/final shapes. | |
// We use that to construct the final shape of the letter in the "middle". | |
var initial = index - 1 >= 0 | |
&& Letters.TryGetValue(@string[reversed + 1], out var right) | |
&& right.Final is not '\0'; | |
var final = @string.Length > index + 1 | |
&& Letters.TryGetValue(@string[reversed - 1], out var left) | |
&& left.Initial is not '\0'; | |
// Try to construct the shape of the letter and count for all shape possibilities. | |
// And in case of failure, always fallback to the nearest "shape". | |
span[index] = (initial, final) switch | |
{ | |
(true, true) => value.Middle is '\0' ? value.Final is '\0' ? value.Initial : value.Final : value.Middle, | |
(true, false) => value.Initial is '\0' ? current : value.Initial, | |
(false, true) => value.Final is '\0' ? current : value.Final, | |
_ => current | |
}; | |
reversed--; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated benchmark with 5K characters.