-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeva2sidh.cs
More file actions
301 lines (260 loc) · 11 KB
/
deva2sidh.cs
File metadata and controls
301 lines (260 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace VRI.CSCD.Conversion
{
class Deva2Siam
{
static void Main(string[] args)
{
try
{
if (args.Length < 2 || args.Length > 3)
{
PrintUsage();
return;
}
FileInfo fi = new FileInfo(args[0]);
if (!fi.Exists)
{
Console.WriteLine("Input file does not exist");
return;
}
DirectoryInfo di = new DirectoryInfo(args[1]);
if (!di.Exists)
{
Console.Write("Output directory does not exist. Create it? [y/n] ");
if (Console.ReadLine().ToLower().StartsWith("y"))
{
di.Create();
}
else
{
return;
}
}
Deva2Siam d2 = new Deva2Siam();
d2.InputFilePath = args[0];
d2.OutputFilePath = Path.Combine(di.FullName, fi.Name);
d2.Convert();
Console.WriteLine("Conversion Complete: " + d2.OutputFilePath);
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex);
}
}
static void PrintUsage()
{
Console.WriteLine("Deva2Siam: Transliterates Devanagari to Siam Script (R.S. 112 Style)");
Console.WriteLine("Features: Uses Mai Han-akat for inherent 'a' in closed syllables,");
Console.WriteLine(" Thanthakhat for stops, and Yamakkan for liquids/fricatives.");
Console.WriteLine("Syntax: deva2siam input output_dir");
}
private Hashtable dev2Thai;
// Set of characters that use Yamakkan (ยามักการ) instead of Thanthakhat
private string yamakkanChars = "\x092F\x0930\x0932\x0935\x0938\x0939\x0933"; // ย, ร, ล, ว, ส, ห, ฬ
public Deva2Siam()
{
dev2Thai = new Hashtable();
dev2Thai['\x0902'] = '\x0E4D'; // niggahita
// independent vowels
dev2Thai['\x0905'] = '\x0E2D'; // a
dev2Thai['\x0906'] = "\x0E2D\x0E32"; // aa
dev2Thai['\x0907'] = "\x0E2D\x0E34"; // i
dev2Thai['\x0908'] = "\x0E2D\x0E35"; // ii
dev2Thai['\x0909'] = "\x0E2D\x0E38"; // u
dev2Thai['\x090A'] = "\x0E2D\x0E39"; // uu
dev2Thai['\x090F'] = "\x0E40\x0E2D"; // e
dev2Thai['\x0913'] = "\x0E42\x0E2D"; // o
// Consonants
// velar
dev2Thai['\x0915'] = '\x0E01'; // ka
dev2Thai['\x0916'] = '\x0E02'; // kha
dev2Thai['\x0917'] = '\x0E04'; // ga
dev2Thai['\x0918'] = '\x0E06'; // gha
dev2Thai['\x0919'] = '\x0E07'; // nga
// palatal
dev2Thai['\x091A'] = '\x0E08'; // ca
dev2Thai['\x091B'] = '\x0E09'; // cha
dev2Thai['\x091C'] = '\x0E0A'; // ja
dev2Thai['\x091D'] = '\x0E0C'; // jha
dev2Thai['\x091E'] = '\x0E0D'; // ña
// retroflex
dev2Thai['\x091F'] = '\x0E0F'; // tta
dev2Thai['\x0920'] = '\x0E10'; // ttha
dev2Thai['\x0921'] = '\x0E11'; // dda
dev2Thai['\x0922'] = '\x0E12'; // ddha
dev2Thai['\x0923'] = '\x0E13'; // nna
// dental
dev2Thai['\x0924'] = '\x0E15'; // ta
dev2Thai['\x0925'] = '\x0E16'; // tha
dev2Thai['\x0926'] = '\x0E17'; // da
dev2Thai['\x0927'] = '\x0E18'; // dha
dev2Thai['\x0928'] = '\x0E19'; // na
// labial
dev2Thai['\x092A'] = '\x0E1B'; // pa
dev2Thai['\x092B'] = '\x0E1C'; // pha
dev2Thai['\x092C'] = '\x0E1E'; // ba
dev2Thai['\x092D'] = '\x0E20'; // bha
dev2Thai['\x092E'] = '\x0E21'; // ma
// liquids/fricatives
dev2Thai['\x092F'] = '\x0E22'; // ya
dev2Thai['\x0930'] = '\x0E23'; // ra
dev2Thai['\x0932'] = '\x0E25'; // la
dev2Thai['\x0935'] = '\x0E27'; // va
dev2Thai['\x0938'] = '\x0E2A'; // sa
dev2Thai['\x0939'] = '\x0E2B'; // ha
dev2Thai['\x0933'] = '\x0E2C'; // la (h)
// dependent vowels
dev2Thai['\x093E'] = '\x0E32'; // aa
dev2Thai['\x093F'] = '\x0E34'; // i
dev2Thai['\x0940'] = '\x0E35'; // ii
dev2Thai['\x0941'] = '\x0E38'; // u
dev2Thai['\x0942'] = '\x0E39'; // uu
dev2Thai['\x0947'] = '\x0E40'; // e
dev2Thai['\x094B'] = '\x0E42'; // o
// Virama is handled in logic, not fixed map
// dev2Thai['\x094D'] = ...;
// numerals
dev2Thai['\x0966'] = '\x0E50';
dev2Thai['\x0967'] = '\x0E51';
dev2Thai['\x0968'] = '\x0E52';
dev2Thai['\x0969'] = '\x0E53';
dev2Thai['\x096A'] = '\x0E54';
dev2Thai['\x096B'] = '\x0E55';
dev2Thai['\x096C'] = '\x0E56';
dev2Thai['\x096D'] = '\x0E57';
dev2Thai['\x096E'] = '\x0E58';
dev2Thai['\x096F'] = '\x0E59';
dev2Thai['\x0970'] = '.';
dev2Thai['\x200C'] = "";
dev2Thai['\x200D'] = "";
}
public string InputFilePath { get; set; }
public string OutputFilePath { get; set; }
public void Convert()
{
using (StreamReader sr = new StreamReader(InputFilePath))
{
string devStr = sr.ReadToEnd();
// Adjust stylesheet if present
devStr = devStr.Replace("tipitaka-deva.xsl", "tipitaka-siam.xsl");
string str = ConvertLogic(devStr);
str = ConvertDandas(str);
str = CleanupPunctuation(str);
using (StreamWriter sw = new StreamWriter(OutputFilePath, false, Encoding.Unicode))
{
sw.Write(str);
}
}
}
// Core conversion logic
public string ConvertLogic(string devStr)
{
devStr = devStr.Replace("\x200D", ""); // Remove ZWJ
// Pre-process: Move E and O before consonant (Standard Thai rendering rule)
devStr = Regex.Replace(devStr, "([\x0915-\x0939])\x0947", "\x0E40$1");
devStr = Regex.Replace(devStr, "([\x0915-\x0939])\x094B", "\x0E42$1");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < devStr.Length; i++)
{
char c = devStr[i];
if (c == '\x094D') // Virama Detection
{
// === 1. Auto Mai Han-akat Logic (Replacing Phinthu) ===
// Logic: If pattern is [Consonant A] [Consonant B] [Virama],
// and [Consonant A] implies inherent 'a' (no other vowel signs),
// then insert Mai Han-akat before [Consonant B].
if (i >= 2)
{
char consA = devStr[i - 2];
char consB = devStr[i - 1];
// Check if both are consonants
if (IsDevConsonant(consA) && IsDevConsonant(consB))
{
// We assume inherent 'a' because if there was a dependent vowel,
// it would be between consA and consB (in Devanagari encoding).
// Since they are adjacent, consA has inherent 'a'.
// Insert Mai Han-akat (\x0E31) into StringBuilder
// The StringBuilder currently ends with Thai translation of ConsB.
// We need to insert BEFORE ConsB.
// Assuming Thai Consonants are length 1.
if (sb.Length > 0)
{
sb.Insert(sb.Length - 1, '\x0E31');
}
}
}
// === 2. Thanthakhat vs Yamakkan Logic ===
char prevChar = devStr[i - 1];
char nextChar = (i + 1 < devStr.Length) ? devStr[i + 1] : '\0';
// Default to Thanthakhat (การันต์/ทัณฑฆาต)
char mark = '\x0E4C';
// Check if previous char is in the "Yamakkan List" (ย, ร, ล, ว, ส, ห, ฬ)
if (yamakkanChars.IndexOf(prevChar) >= 0)
{
mark = '\x0E4E'; // Set to Yamakkan (ยามักการ)
// EXCEPTION: 'S' (ส) in geminate context (e.g. Tassa - ตัส์ส)
// If current is 'S' and next is also 'S', use Thanthakhat.
if (prevChar == '\x0938' && nextChar == '\x0938')
{
mark = '\x0E4C';
}
}
sb.Append(mark);
}
else if (dev2Thai.ContainsKey(c))
{
sb.Append(dev2Thai[c]);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
private bool IsDevConsonant(char c)
{
// Range Ka (0915) to Ha (0939)
return (c >= '\x0915' && c <= '\x0939');
}
public string ConvertDandas(string str)
{
// Gatha handling
str = Regex.Replace(str, "<p rend=\"gatha[a-z0-9]*\">.+?</p>",
new MatchEvaluator(this.ConvertGathaDandas));
// Center handling (Namo)
str = Regex.Replace(str, "<p rend=\"centre\">.+?</p>",
new MatchEvaluator(this.ConvertNamoTassaDandas));
// Default: Convert Dandas to Paiyannoi for Siam style sectioning
str = str.Replace("\x0964", "\x0E2F");
str = str.Replace("\x0965", "\x0E2F");
return str;
}
public string ConvertGathaDandas(Match m)
{
string s = m.Value;
s = s.Replace("\x0964", ";");
s = s.Replace("\x0965", "\x0E2F");
return s;
}
public string ConvertNamoTassaDandas(Match m)
{
string s = m.Value;
return s.Replace("\x0965", "\x0E2F");
}
public string CleanupPunctuation(string str)
{
str = str.Replace(" ,", ",");
str = str.Replace(" ?", "?");
str = str.Replace(" !", "!");
str = str.Replace(" ;", ";");
str = str.Replace(" .", ".");
return str;
}
}
}