RTLFixerTool.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections.Generic;
  3. namespace I2.Loc
  4. {
  5. internal class RTLFixerTool
  6. {
  7. internal static string RemoveTashkeel(string str, out List<TashkeelLocation> tashkeelLocation)
  8. {
  9. tashkeelLocation = new List<TashkeelLocation>();
  10. char[] array = str.ToCharArray();
  11. int num = 0;
  12. for (int i = 0; i < array.Length; i++)
  13. {
  14. if (array[i] == 'ً')
  15. {
  16. tashkeelLocation.Add(new TashkeelLocation('ً', i));
  17. num++;
  18. }
  19. else if (array[i] == 'ٌ')
  20. {
  21. tashkeelLocation.Add(new TashkeelLocation('ٌ', i));
  22. num++;
  23. }
  24. else if (array[i] == 'ٍ')
  25. {
  26. tashkeelLocation.Add(new TashkeelLocation('ٍ', i));
  27. num++;
  28. }
  29. else if (array[i] == 'َ')
  30. {
  31. if (num > 0 && tashkeelLocation[num - 1].tashkeel == 'ّ')
  32. {
  33. tashkeelLocation[num - 1].tashkeel = 'ﱠ';
  34. }
  35. else
  36. {
  37. tashkeelLocation.Add(new TashkeelLocation('َ', i));
  38. num++;
  39. }
  40. }
  41. else if (array[i] == 'ُ')
  42. {
  43. if (num > 0 && tashkeelLocation[num - 1].tashkeel == 'ّ')
  44. {
  45. tashkeelLocation[num - 1].tashkeel = 'ﱡ';
  46. }
  47. else
  48. {
  49. tashkeelLocation.Add(new TashkeelLocation('ُ', i));
  50. num++;
  51. }
  52. }
  53. else if (array[i] == 'ِ')
  54. {
  55. if (num > 0 && tashkeelLocation[num - 1].tashkeel == 'ّ')
  56. {
  57. tashkeelLocation[num - 1].tashkeel = 'ﱢ';
  58. }
  59. else
  60. {
  61. tashkeelLocation.Add(new TashkeelLocation('ِ', i));
  62. num++;
  63. }
  64. }
  65. else if (array[i] == 'ّ')
  66. {
  67. if (num > 0)
  68. {
  69. if (tashkeelLocation[num - 1].tashkeel == 'َ')
  70. {
  71. tashkeelLocation[num - 1].tashkeel = 'ﱠ';
  72. goto IL_2C2;
  73. }
  74. if (tashkeelLocation[num - 1].tashkeel == 'ُ')
  75. {
  76. tashkeelLocation[num - 1].tashkeel = 'ﱡ';
  77. goto IL_2C2;
  78. }
  79. if (tashkeelLocation[num - 1].tashkeel == 'ِ')
  80. {
  81. tashkeelLocation[num - 1].tashkeel = 'ﱢ';
  82. goto IL_2C2;
  83. }
  84. }
  85. tashkeelLocation.Add(new TashkeelLocation('ّ', i));
  86. num++;
  87. }
  88. else if (array[i] == 'ْ')
  89. {
  90. tashkeelLocation.Add(new TashkeelLocation('ْ', i));
  91. num++;
  92. }
  93. else if (array[i] == 'ٓ')
  94. {
  95. tashkeelLocation.Add(new TashkeelLocation('ٓ', i));
  96. num++;
  97. }
  98. IL_2C2:;
  99. }
  100. string[] array2 = str.Split(new char[]
  101. {
  102. 'ً',
  103. 'ٌ',
  104. 'ٍ',
  105. 'َ',
  106. 'ُ',
  107. 'ِ',
  108. 'ّ',
  109. 'ْ',
  110. 'ٓ',
  111. 'ﱠ',
  112. 'ﱡ',
  113. 'ﱢ'
  114. });
  115. str = string.Empty;
  116. foreach (string str2 in array2)
  117. {
  118. str += str2;
  119. }
  120. return str;
  121. }
  122. internal static char[] ReturnTashkeel(char[] letters, List<TashkeelLocation> tashkeelLocation)
  123. {
  124. char[] array = new char[letters.Length + tashkeelLocation.Count];
  125. int num = 0;
  126. for (int i = 0; i < letters.Length; i++)
  127. {
  128. array[num] = letters[i];
  129. num++;
  130. foreach (TashkeelLocation tashkeelLocation2 in tashkeelLocation)
  131. {
  132. if (tashkeelLocation2.position == num)
  133. {
  134. array[num] = tashkeelLocation2.tashkeel;
  135. num++;
  136. }
  137. }
  138. }
  139. return array;
  140. }
  141. internal static string FixLine(string str)
  142. {
  143. string str2 = string.Empty;
  144. List<TashkeelLocation> tashkeelLocation;
  145. string text = RTLFixerTool.RemoveTashkeel(str, out tashkeelLocation);
  146. char[] array = text.ToCharArray();
  147. char[] array2 = text.ToCharArray();
  148. for (int i = 0; i < array.Length; i++)
  149. {
  150. array[i] = (char)ArabicTable.ArabicMapper.Convert((int)array[i]);
  151. }
  152. for (int j = 0; j < array.Length; j++)
  153. {
  154. bool flag = false;
  155. if (array[j] == 'ﻝ' && j < array.Length - 1)
  156. {
  157. if (array[j + 1] == 'ﺇ')
  158. {
  159. array[j] = 'ﻷ';
  160. array2[j + 1] = char.MaxValue;
  161. flag = true;
  162. }
  163. else if (array[j + 1] == 'ﺍ')
  164. {
  165. array[j] = 'ﻹ';
  166. array2[j + 1] = char.MaxValue;
  167. flag = true;
  168. }
  169. else if (array[j + 1] == 'ﺃ')
  170. {
  171. array[j] = 'ﻵ';
  172. array2[j + 1] = char.MaxValue;
  173. flag = true;
  174. }
  175. else if (array[j + 1] == 'ﺁ')
  176. {
  177. array[j] = 'ﻳ';
  178. array2[j + 1] = char.MaxValue;
  179. flag = true;
  180. }
  181. }
  182. if (!RTLFixerTool.IsIgnoredCharacter(array[j]))
  183. {
  184. if (RTLFixerTool.IsMiddleLetter(array, j))
  185. {
  186. array2[j] = array[j] + '\u0003';
  187. }
  188. else if (RTLFixerTool.IsFinishingLetter(array, j))
  189. {
  190. array2[j] = array[j] + '\u0001';
  191. }
  192. else if (RTLFixerTool.IsLeadingLetter(array, j))
  193. {
  194. array2[j] = array[j] + '\u0002';
  195. }
  196. }
  197. str2 = str2 + Convert.ToString((int)array[j], 16) + " ";
  198. if (flag)
  199. {
  200. j++;
  201. }
  202. if (RTLFixerTool.useHinduNumbers)
  203. {
  204. if (array[j] == '0')
  205. {
  206. array2[j] = '٠';
  207. }
  208. else if (array[j] == '1')
  209. {
  210. array2[j] = '١';
  211. }
  212. else if (array[j] == '2')
  213. {
  214. array2[j] = '٢';
  215. }
  216. else if (array[j] == '3')
  217. {
  218. array2[j] = '٣';
  219. }
  220. else if (array[j] == '4')
  221. {
  222. array2[j] = '٤';
  223. }
  224. else if (array[j] == '5')
  225. {
  226. array2[j] = '٥';
  227. }
  228. else if (array[j] == '6')
  229. {
  230. array2[j] = '٦';
  231. }
  232. else if (array[j] == '7')
  233. {
  234. array2[j] = '٧';
  235. }
  236. else if (array[j] == '8')
  237. {
  238. array2[j] = '٨';
  239. }
  240. else if (array[j] == '9')
  241. {
  242. array2[j] = '٩';
  243. }
  244. }
  245. }
  246. if (RTLFixerTool.showTashkeel)
  247. {
  248. array2 = RTLFixerTool.ReturnTashkeel(array2, tashkeelLocation);
  249. }
  250. List<char> list = new List<char>();
  251. List<char> list2 = new List<char>();
  252. for (int k = array2.Length - 1; k >= 0; k--)
  253. {
  254. if (char.IsPunctuation(array2[k]) && k > 0 && k < array2.Length - 1 && (char.IsPunctuation(array2[k - 1]) || char.IsPunctuation(array2[k + 1])))
  255. {
  256. if (array2[k] == '(')
  257. {
  258. list.Add(')');
  259. }
  260. else if (array2[k] == ')')
  261. {
  262. list.Add('(');
  263. }
  264. else if (array2[k] == '<')
  265. {
  266. list.Add('>');
  267. }
  268. else if (array2[k] == '>')
  269. {
  270. list.Add('<');
  271. }
  272. else if (array2[k] == '[')
  273. {
  274. list.Add(']');
  275. }
  276. else if (array2[k] == ']')
  277. {
  278. list.Add('[');
  279. }
  280. else if (array2[k] != '￿')
  281. {
  282. list.Add(array2[k]);
  283. }
  284. }
  285. else if (array2[k] == ' ' && k > 0 && k < array2.Length - 1 && (char.IsLower(array2[k - 1]) || char.IsUpper(array2[k - 1]) || char.IsNumber(array2[k - 1])) && (char.IsLower(array2[k + 1]) || char.IsUpper(array2[k + 1]) || char.IsNumber(array2[k + 1])))
  286. {
  287. list2.Add(array2[k]);
  288. }
  289. else if (char.IsNumber(array2[k]) || char.IsLower(array2[k]) || char.IsUpper(array2[k]) || char.IsSymbol(array2[k]) || char.IsPunctuation(array2[k]))
  290. {
  291. if (array2[k] == '(')
  292. {
  293. list2.Add(')');
  294. }
  295. else if (array2[k] == ')')
  296. {
  297. list2.Add('(');
  298. }
  299. else if (array2[k] == '<')
  300. {
  301. list2.Add('>');
  302. }
  303. else if (array2[k] == '>')
  304. {
  305. list2.Add('<');
  306. }
  307. else if (array2[k] == '[')
  308. {
  309. list.Add(']');
  310. }
  311. else if (array2[k] == ']')
  312. {
  313. list.Add('[');
  314. }
  315. else
  316. {
  317. list2.Add(array2[k]);
  318. }
  319. }
  320. else if ((array2[k] >= '\ud800' && array2[k] <= '\udbff') || (array2[k] >= '\udc00' && array2[k] <= '\udfff'))
  321. {
  322. list2.Add(array2[k]);
  323. }
  324. else
  325. {
  326. if (list2.Count > 0)
  327. {
  328. for (int l = 0; l < list2.Count; l++)
  329. {
  330. list.Add(list2[list2.Count - 1 - l]);
  331. }
  332. list2.Clear();
  333. }
  334. if (array2[k] != '￿')
  335. {
  336. list.Add(array2[k]);
  337. }
  338. }
  339. }
  340. if (list2.Count > 0)
  341. {
  342. for (int m = 0; m < list2.Count; m++)
  343. {
  344. list.Add(list2[list2.Count - 1 - m]);
  345. }
  346. list2.Clear();
  347. }
  348. array2 = new char[list.Count];
  349. for (int n = 0; n < array2.Length; n++)
  350. {
  351. array2[n] = list[n];
  352. }
  353. str = new string(array2);
  354. return str;
  355. }
  356. internal static bool IsIgnoredCharacter(char ch)
  357. {
  358. bool flag = char.IsPunctuation(ch);
  359. bool flag2 = char.IsNumber(ch);
  360. bool flag3 = char.IsLower(ch);
  361. bool flag4 = char.IsUpper(ch);
  362. bool flag5 = char.IsSymbol(ch);
  363. bool flag6 = ch == 'ﭖ' || ch == 'ﭺ' || ch == 'ﮊ' || ch == 'ﮒ' || ch == 'ﮎ';
  364. bool flag7 = (ch <= '' && ch >= 'ﹰ') || flag6 || ch == 'ﯼ';
  365. return flag || flag2 || flag3 || flag4 || flag5 || !flag7 || ch == 'a' || ch == '>' || ch == '<' || ch == '؛';
  366. }
  367. internal static bool IsLeadingLetter(char[] letters, int index)
  368. {
  369. bool flag = 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] == 'ﺅ';
  370. bool flag2 = 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] != 'ﺀ';
  371. bool flag3 = index < letters.Length - 1 && letters[index + 1] != ' ' && !char.IsPunctuation(letters[index + 1]) && !char.IsNumber(letters[index + 1]) && !char.IsSymbol(letters[index + 1]) && !char.IsLower(letters[index + 1]) && !char.IsUpper(letters[index + 1]) && letters[index + 1] != 'ﺀ';
  372. return flag && flag2 && flag3;
  373. }
  374. internal static bool IsFinishingLetter(char[] letters, int index)
  375. {
  376. bool flag = index != 0 && (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] != '<';
  377. bool flag2 = letters[index] != ' ' && letters[index] != 'ﺀ';
  378. return flag && flag2;
  379. }
  380. internal static bool IsMiddleLetter(char[] letters, int index)
  381. {
  382. bool flag = 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] != 'ﺀ';
  383. bool flag2 = index != 0 && (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 - 1] != ' ') && letters[index - 1] != '*';
  384. bool flag3 = index < letters.Length - 1 && (letters[index + 1] != ' ' && letters[index + 1] != '\r' && letters[index + 1] != 'ﺀ' && !char.IsNumber(letters[index + 1]) && !char.IsSymbol(letters[index + 1])) && !char.IsPunctuation(letters[index + 1]);
  385. if (flag3 && flag2 && flag)
  386. {
  387. try
  388. {
  389. if (char.IsPunctuation(letters[index + 1]))
  390. {
  391. return false;
  392. }
  393. return true;
  394. }
  395. catch
  396. {
  397. return false;
  398. }
  399. return false;
  400. }
  401. return false;
  402. }
  403. internal static bool showTashkeel = true;
  404. internal static bool useHinduNumbers;
  405. }
  406. }