您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

334 行
15 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CIG.Translation.ArabicSupport
  4. {
  5. public class ArabicFixerTool
  6. {
  7. public static bool showTashkeel = true;
  8. public static bool useHinduNumbers;
  9. private static bool isParsingColor;
  10. private static string RemoveTashkeel(string str, out List<TashkeelLocation> tashkeelLocation)
  11. {
  12. tashkeelLocation = new List<TashkeelLocation>();
  13. char[] array = str.ToCharArray();
  14. for (int i = 0; i < array.Length; i++)
  15. {
  16. if (array[i] == '\u064b')
  17. {
  18. tashkeelLocation.Add(new TashkeelLocation('\u064b', i));
  19. }
  20. else if (array[i] == '\u064c')
  21. {
  22. tashkeelLocation.Add(new TashkeelLocation('\u064c', i));
  23. }
  24. else if (array[i] == '\u064d')
  25. {
  26. tashkeelLocation.Add(new TashkeelLocation('\u064d', i));
  27. }
  28. else if (array[i] == '\u064e')
  29. {
  30. tashkeelLocation.Add(new TashkeelLocation('\u064e', i));
  31. }
  32. else if (array[i] == '\u064f')
  33. {
  34. tashkeelLocation.Add(new TashkeelLocation('\u064f', i));
  35. }
  36. else if (array[i] == '\u0650')
  37. {
  38. tashkeelLocation.Add(new TashkeelLocation('\u0650', i));
  39. }
  40. else if (array[i] == '\u0651')
  41. {
  42. tashkeelLocation.Add(new TashkeelLocation('\u0651', i));
  43. }
  44. else if (array[i] == '\u0652')
  45. {
  46. tashkeelLocation.Add(new TashkeelLocation('\u0652', i));
  47. }
  48. else if (array[i] == '\u0653')
  49. {
  50. tashkeelLocation.Add(new TashkeelLocation('\u0653', i));
  51. }
  52. }
  53. string[] array2 = str.Split('\u064b', '\u064c', '\u064d', '\u064e', '\u064f', '\u0650', '\u0651', '\u0652', '\u0653');
  54. str = string.Empty;
  55. string[] array3 = array2;
  56. foreach (string str2 in array3)
  57. {
  58. str += str2;
  59. }
  60. return str;
  61. }
  62. private static char[] ReturnTashkeel(char[] letters, List<TashkeelLocation> tashkeelLocation)
  63. {
  64. char[] array = new char[letters.Length + tashkeelLocation.Count];
  65. int num = 0;
  66. for (int i = 0; i < letters.Length; i++)
  67. {
  68. array[num] = letters[i];
  69. num++;
  70. foreach (TashkeelLocation item in tashkeelLocation)
  71. {
  72. if (item.position == num)
  73. {
  74. array[num] = item.tashkeel;
  75. num++;
  76. }
  77. }
  78. }
  79. return array;
  80. }
  81. public static string FixLine(string str)
  82. {
  83. string str2 = string.Empty;
  84. List<TashkeelLocation> tashkeelLocation;
  85. string text = RemoveTashkeel(str, out tashkeelLocation);
  86. char[] array = text.ToCharArray();
  87. char[] array2 = text.ToCharArray();
  88. for (int i = 0; i < array.Length; i++)
  89. {
  90. array[i] = (char)ArabicTable.ArabicMapper.Convert(array[i]);
  91. }
  92. for (int j = 0; j < array.Length; j++)
  93. {
  94. if (j + 5 <= str.Length && str.Substring(j, 5) == "color")
  95. {
  96. isParsingColor = true;
  97. }
  98. if (array[j] == '>')
  99. {
  100. isParsingColor = false;
  101. }
  102. bool flag = false;
  103. if (array[j] == 'ﻝ' && j < array.Length - 1)
  104. {
  105. if (array[j + 1] == 'ﺇ')
  106. {
  107. array[j] = 'ﻷ';
  108. array2[j + 1] = '\uffff';
  109. flag = true;
  110. }
  111. else if (array[j + 1] == 'ﺍ')
  112. {
  113. array[j] = 'ﻹ';
  114. array2[j + 1] = '\uffff';
  115. flag = true;
  116. }
  117. else if (array[j + 1] == 'ﺃ')
  118. {
  119. array[j] = 'ﻵ';
  120. array2[j + 1] = '\uffff';
  121. flag = true;
  122. }
  123. else if (array[j + 1] == 'ﺁ')
  124. {
  125. array[j] = 'ﻳ';
  126. array2[j + 1] = '\uffff';
  127. flag = true;
  128. }
  129. }
  130. if (!IsIgnoredCharacter(array[j]) && array[j] != 'A' && array[j] != 'A')
  131. {
  132. if (IsMiddleLetter(array, j))
  133. {
  134. array2[j] = (char)(array[j] + 3);
  135. }
  136. else if (IsFinishingLetter(array, j))
  137. {
  138. array2[j] = (char)(array[j] + 1);
  139. }
  140. else if (IsLeadingLetter(array, j))
  141. {
  142. array2[j] = (char)(array[j] + 2);
  143. }
  144. }
  145. str2 = str2 + Convert.ToString(array[j], 16) + " ";
  146. if (flag)
  147. {
  148. j++;
  149. }
  150. if (useHinduNumbers && !isParsingColor)
  151. {
  152. if (array[j] == '0')
  153. {
  154. array2[j] = '٠';
  155. }
  156. else if (array[j] == '1')
  157. {
  158. array2[j] = '١';
  159. }
  160. else if (array[j] == '2')
  161. {
  162. array2[j] = '٢';
  163. }
  164. else if (array[j] == '3')
  165. {
  166. array2[j] = '٣';
  167. }
  168. else if (array[j] == '4')
  169. {
  170. array2[j] = '٤';
  171. }
  172. else if (array[j] == '5')
  173. {
  174. array2[j] = '٥';
  175. }
  176. else if (array[j] == '6')
  177. {
  178. array2[j] = '٦';
  179. }
  180. else if (array[j] == '7')
  181. {
  182. array2[j] = '٧';
  183. }
  184. else if (array[j] == '8')
  185. {
  186. array2[j] = '٨';
  187. }
  188. else if (array[j] == '9')
  189. {
  190. array2[j] = '٩';
  191. }
  192. }
  193. }
  194. if (showTashkeel)
  195. {
  196. array2 = ReturnTashkeel(array2, tashkeelLocation);
  197. }
  198. List<char> list = new List<char>();
  199. List<char> list2 = new List<char>();
  200. for (int num = array2.Length - 1; num >= 0; num--)
  201. {
  202. if (char.IsPunctuation(array2[num]) && num > 0 && num < array2.Length - 1 && (char.IsPunctuation(array2[num - 1]) || char.IsPunctuation(array2[num + 1])))
  203. {
  204. if (array2[num] == '(')
  205. {
  206. list.Add(')');
  207. }
  208. else if (array2[num] == ')')
  209. {
  210. list.Add('(');
  211. }
  212. else if (array2[num] == '<')
  213. {
  214. list.Add('>');
  215. }
  216. else if (array2[num] == '>')
  217. {
  218. list.Add('<');
  219. }
  220. else if (array2[num] != '\uffff')
  221. {
  222. list.Add(array2[num]);
  223. }
  224. }
  225. else if (array2[num] == ' ' && num > 0 && num < array2.Length - 1 && (char.IsLower(array2[num - 1]) || char.IsUpper(array2[num - 1]) || char.IsNumber(array2[num - 1])) && (char.IsLower(array2[num + 1]) || char.IsUpper(array2[num + 1]) || char.IsNumber(array2[num + 1])))
  226. {
  227. list2.Add(array2[num]);
  228. }
  229. else if (char.IsNumber(array2[num]) || char.IsLower(array2[num]) || char.IsUpper(array2[num]) || char.IsSymbol(array2[num]) || char.IsPunctuation(array2[num]))
  230. {
  231. if (array2[num] == '(')
  232. {
  233. list2.Add(')');
  234. }
  235. else if (array2[num] == ')')
  236. {
  237. list2.Add('(');
  238. }
  239. else if (array2[num] == '<')
  240. {
  241. list2.Add('>');
  242. }
  243. else if (array2[num] == '>')
  244. {
  245. list2.Add('<');
  246. }
  247. else
  248. {
  249. list2.Add(array2[num]);
  250. }
  251. }
  252. else if ((array2[num] >= '\ud800' && array2[num] <= '\udbff') || (array2[num] >= '\udc00' && array2[num] <= '\udfff'))
  253. {
  254. list2.Add(array2[num]);
  255. }
  256. else
  257. {
  258. if (list2.Count > 0)
  259. {
  260. for (int k = 0; k < list2.Count; k++)
  261. {
  262. list.Add(list2[list2.Count - 1 - k]);
  263. }
  264. list2.Clear();
  265. }
  266. if (array2[num] != '\uffff')
  267. {
  268. list.Add(array2[num]);
  269. }
  270. }
  271. }
  272. if (list2.Count > 0)
  273. {
  274. for (int l = 0; l < list2.Count; l++)
  275. {
  276. list.Add(list2[list2.Count - 1 - l]);
  277. }
  278. list2.Clear();
  279. }
  280. char[] array3 = new char[list.Count];
  281. for (int m = 0; m < array3.Length; m++)
  282. {
  283. array3[m] = list[m];
  284. }
  285. str = new string(array3);
  286. return str;
  287. }
  288. private static bool IsIgnoredCharacter(char ch)
  289. {
  290. bool flag = char.IsPunctuation(ch);
  291. bool flag2 = char.IsNumber(ch);
  292. bool flag3 = char.IsLower(ch);
  293. bool flag4 = char.IsUpper(ch);
  294. bool flag5 = char.IsSymbol(ch);
  295. bool flag6 = ch == 'ﭖ' || ch == 'ﭺ' || ch == 'ﮊ' || ch == 'ﮒ';
  296. bool flag7 = (ch <= '\ufeff' && ch >= 'ﹰ') || flag6;
  297. return flag || flag2 || flag3 || flag4 || flag5 || !flag7 || ch == 'a' || ch == '>' || ch == '<' || ch == '؛';
  298. }
  299. private static bool IsLeadingLetter(char[] letters, int index)
  300. {
  301. return (index == 0 || letters[index - 1] == ' ' || letters[index - 1] == '*' || letters[index - 1] == 'A' || char.IsPunctuation(letters[index - 1]) || letters[index - 1] == '>' || letters[index - 1] == '<' || letters[index - 1] == 'ﺍ' || letters[index - 1] == 'ﺩ' || letters[index - 1] == 'ﺫ' || letters[index - 1] == 'ﺭ' || letters[index - 1] == 'ﺯ' || letters[index - 1] == 'ﮊ' || letters[index - 1] == 'ﻯ' || letters[index - 1] == 'ﻭ' || letters[index - 1] == 'ﺁ' || letters[index - 1] == 'ﺃ' || letters[index - 1] == 'ﺇ' || letters[index - 1] == 'ﺅ') && letters[index] != ' ' && letters[index] != 'ﺩ' && letters[index] != 'ﺫ' && letters[index] != 'ﺭ' && letters[index] != 'ﺯ' && letters[index] != 'ﮊ' && letters[index] != 'ﺍ' && letters[index] != 'ﺃ' && letters[index] != 'ﺇ' && letters[index] != 'ﻭ' && letters[index] != 'ﺀ' && index < letters.Length - 1 && letters[index + 1] != ' ' && !char.IsPunctuation(letters[index + 1]) && letters[index + 1] != 'ﺀ';
  302. }
  303. private static bool IsFinishingLetter(char[] letters, int index)
  304. {
  305. return index != 0 && letters[index - 1] != ' ' && letters[index - 1] != '*' && letters[index - 1] != 'A' && letters[index - 1] != 'ﺩ' && letters[index - 1] != 'ﺫ' && letters[index - 1] != 'ﺭ' && letters[index - 1] != 'ﺯ' && letters[index - 1] != 'ﮊ' && letters[index - 1] != 'ﻯ' && letters[index - 1] != 'ﻭ' && letters[index - 1] != 'ﺍ' && letters[index - 1] != 'ﺁ' && letters[index - 1] != 'ﺃ' && letters[index - 1] != 'ﺇ' && letters[index - 1] != 'ﺅ' && letters[index - 1] != 'ﺀ' && !char.IsPunctuation(letters[index - 1]) && letters[index - 1] != '>' && letters[index - 1] != '<' && letters[index] != ' ' && index < letters.Length && letters[index] != 'ﺀ';
  306. }
  307. private static bool IsMiddleLetter(char[] letters, int index)
  308. {
  309. if (index == 0 || letters[index] == ' ' || letters[index] == 'ﺍ' || letters[index] == 'ﺩ' || letters[index] == 'ﺫ' || letters[index] == 'ﺭ' || letters[index] == 'ﺯ' || letters[index] == 'ﮊ' || letters[index] == 'ﻯ' || letters[index] == 'ﻭ' || letters[index] == 'ﺁ' || letters[index] == 'ﺃ' || letters[index] == 'ﺇ' || letters[index] == 'ﺅ' || letters[index] == 'ﺀ' || letters[index - 1] == 'ﺍ' || letters[index - 1] == 'ﺩ' || letters[index - 1] == 'ﺫ' || letters[index - 1] == 'ﺭ' || letters[index - 1] == 'ﺯ' || letters[index - 1] == 'ﮊ' || letters[index - 1] == 'ﻯ' || letters[index - 1] == 'ﻭ' || letters[index - 1] == 'ﺁ' || letters[index - 1] == 'ﺃ' || letters[index - 1] == 'ﺇ' || letters[index - 1] == 'ﺅ' || letters[index - 1] == 'ﺀ' || letters[index - 1] == '>' || letters[index - 1] == '<' || letters[index - 1] == ' ' || letters[index - 1] == '*' || char.IsPunctuation(letters[index - 1]) || index >= letters.Length - 1 || letters[index + 1] == ' ' || letters[index + 1] == '\r' || letters[index + 1] == 'A' || letters[index + 1] == '>' || letters[index + 1] == '>' || letters[index + 1] == 'ﺀ')
  310. {
  311. return false;
  312. }
  313. try
  314. {
  315. return !char.IsPunctuation(letters[index + 1]);
  316. }
  317. catch
  318. {
  319. return false;
  320. }
  321. }
  322. }
  323. }