-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeva2cyrl.cs
More file actions
294 lines (251 loc) · 10.4 KB
/
deva2cyrl.cs
File metadata and controls
294 lines (251 loc) · 10.4 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
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace VRI.CSCD.Conversion
{
class Deva2Cyrl
{
static void Main(string[] args)
{
try
{
if (args.Length < 2 || args.Length > 3)
{
PrintUsage();
return;
}
// validate args
FileInfo fi = new FileInfo(args[0]);
if (fi.Exists == false)
{
Console.WriteLine("Input file does not exist");
return;
}
DirectoryInfo di = new DirectoryInfo(args[1]);
if (di.Exists == false)
{
Console.Write("Output directory does not exist. Would you like to create it?[y/n] ");
if (Console.ReadLine().ToLower().Substring(0, 1) == "y")
{
di.Create();
}
else
{
return;
}
}
Deva2Cyrl d2 = new Deva2Cyrl();
d2.InputFilePath = args[0];
d2.OutputFilePath = di.FullName + "\\" + fi.Name;
/*d2.ParagraphElements = ConfigurationManager.AppSettings["ParagraphElements"].Split(',');
d2.IgnoreElements = ConfigurationManager.AppSettings["IgnoreElements"].Split(',');
d2.CapitalMarker = System.Convert.ToChar(
System.Convert.ToInt32(
ConfigurationManager.AppSettings["CapitalMarker"])
).ToString();*/
d2.Convert();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex);
}
}
static void PrintUsage()
{
Console.WriteLine("Transliterates Unicode Devanagari to Unicode Cyrillic");
Console.WriteLine("syntax:");
Console.WriteLine("deva2cyrl input [output]");
}
// end static methods
private Hashtable deva2Cyrl;
public Deva2Cyrl()
{
deva2Cyrl = new Hashtable();
deva2Cyrl['\x0902'] = "\x043C\x0323"; // niggahita
// velar stops
deva2Cyrl['\x0915'] = '\x0433'; // ka
deva2Cyrl['\x0916'] = '\x043A'; // kha
deva2Cyrl['\x0917'] = "\x0433\x0307"; // ga
deva2Cyrl['\x0918'] = "\x0433\x0445"; // gha
deva2Cyrl['\x0919'] = "\x043D\x0307"; // n overdot a
// palatal stops
deva2Cyrl['\x091A'] = '\x0436'; // ca
deva2Cyrl['\x091B'] = '\x0447'; // cha
deva2Cyrl['\x091C'] = "\x0436\x0307"; // ja
deva2Cyrl['\x091D'] = "\x0436\x0445"; // jha
deva2Cyrl['\x091E'] = "\x043D\x0303"; // ña
// retroflex stops
deva2Cyrl['\x091F'] = '\x0434'; // t underdot a
deva2Cyrl['\x0920'] = '\x0442'; // t underdot ha
deva2Cyrl['\x0921'] = "\x0434\x0323"; // d underdot a
deva2Cyrl['\x0922'] = "\x0434\x0445"; // d underdot ha
deva2Cyrl['\x0923'] = "\x043D\x0323"; // n underdot a
// dental stops
deva2Cyrl['\x0924'] = "\x0434\x0307"; // ta
deva2Cyrl['\x0925'] = "\x0442\x0307"; // tha
deva2Cyrl['\x0926'] = "\x0434\x0307\x0323"; // da
deva2Cyrl['\x0927'] = "\x0434\x0307\x0445"; // dha
deva2Cyrl['\x0928'] = '\x043D'; // na
// labial stops
deva2Cyrl['\x092A'] = '\x0431'; // pa
deva2Cyrl['\x092B'] = '\x043F'; // pha
deva2Cyrl['\x092C'] = "\x0431\x0323"; // ba
deva2Cyrl['\x092D'] = "\x0431\x0445"; // bha
deva2Cyrl['\x092E'] = '\x043C'; // ma
// liquids, fricatives, etc.
deva2Cyrl['\x092F'] = '\x044F'; // ya
deva2Cyrl['\x0930'] = '\x0440'; // ra
deva2Cyrl['\x0932'] = '\x043B'; // la
deva2Cyrl['\x0935'] = '\x0432'; // va
deva2Cyrl['\x0938'] = '\x0441'; // sa
deva2Cyrl['\x0939'] = '\x0445'; // ha
deva2Cyrl['\x0933'] = "\x043B\x0323"; // l underdot a
// independent vowels
deva2Cyrl['\x0905'] = '\x0430'; // a
deva2Cyrl['\x0906'] = "\x0430\x0430"; // aa
deva2Cyrl['\x0907'] = '\x0438'; // i
deva2Cyrl['\x0908'] = "\x0438\x0439"; // ii
deva2Cyrl['\x0909'] = '\x0443'; // u
deva2Cyrl['\x090A'] = "\x0443\x0443"; // uu
deva2Cyrl['\x090F'] = '\x0437'; // e
deva2Cyrl['\x0913'] = '\x043E'; // o
// dependent vowel signs
deva2Cyrl['\x093E'] = "\x0430\x0430"; // aa
deva2Cyrl['\x093F'] = '\x0438'; // i
deva2Cyrl['\x0940'] = "\x0438\x0439"; // ii
deva2Cyrl['\x0941'] = '\x0443'; // u
deva2Cyrl['\x0942'] = "\x0443\x0443"; // uu
deva2Cyrl['\x0947'] = '\x0437'; // e
deva2Cyrl['\x094B'] = '\x043E'; // o
// various signs
deva2Cyrl['\x094D'] = ""; // virama
// let Devanagari danda (U+0964) and double danda (U+0965)
// pass through unmodified
// numerals
deva2Cyrl['\x0966'] = '0';
deva2Cyrl['\x0967'] = '1';
deva2Cyrl['\x0968'] = '2';
deva2Cyrl['\x0969'] = '3';
deva2Cyrl['\x096A'] = '4';
deva2Cyrl['\x096B'] = '5';
deva2Cyrl['\x096C'] = '6';
deva2Cyrl['\x096D'] = '7';
deva2Cyrl['\x096E'] = '8';
deva2Cyrl['\x096F'] = '9';
deva2Cyrl['\x0970'] = "."; // Dev abbreviation sign
deva2Cyrl['\x200C'] = ""; // ZWNJ (ignore)
deva2Cyrl['\x200D'] = ""; // ZWJ (ignore)
}
public string InputFilePath
{
get { return inputFilePath; }
set { inputFilePath = value; }
}
private string inputFilePath;
public string OutputFilePath
{
get { return outputFilePath; }
set { outputFilePath = value; }
}
private string outputFilePath;
public string[] ParagraphElements
{
get { return paragraphElements; }
set { paragraphElements = value; }
}
private string[] paragraphElements;
public string[] IgnoreElements
{
get { return ignoreElements; }
set { ignoreElements = value; }
}
private string[] ignoreElements;
public string CapitalMarker
{
get { return capitalMarker; }
set { capitalMarker = value; }
}
private string capitalMarker;
public void Convert()
{
StreamReader sr = new StreamReader(InputFilePath);
string devStr = sr.ReadToEnd();
sr.Close();
// change name of stylesheet for Cyrillic
devStr = devStr.Replace("tipitaka-deva.xsl", "tipitaka-cyrl.xsl");
// mark the Devanagari text for programmatic capitalization
//Capitalizer capitalizer = new Capitalizer(ParagraphElements, IgnoreElements, CapitalMarker);
//devStr = capitalizer.MarkCapitals(devStr);
// remove Dev abbreviation sign before an ellipsis. We don't want a 4th dot after pe.
devStr = devStr.Replace("\x0970\x2026", "\x2026");
string str = Convert(devStr);
//str = capitalizer.Capitalize(str);
str = ConvertDandas(str);
str = CleanupPunctuation(str);
StreamWriter sw = new StreamWriter(OutputFilePath, false, Encoding.Unicode);
sw.Write(str);
sw.Flush();
sw.Close();
}
// more generalized, reusable conversion method:
// no stylesheet modifications, capitalization, etc.
public string Convert(string devStr)
{
// Insert Cyrillic 'a' after all consonants that are not followed by virama, dependent vowel or cyrillic a
// (This still works after we inserted ZWJ in the Devanagari. The ZWJ goes after virama.)
devStr = Regex.Replace(devStr, "([\x0915-\x0939])([^\x093E-\x094D\x0430])", "$1\x0430$2");
devStr = Regex.Replace(devStr, "([\x0915-\x0939])([^\x093E-\x094D\x0430])", "$1\x0430$2");
// TODO: figure out how to backtrack so this replace doesn't have to be done twice
StringBuilder sb = new StringBuilder();
foreach (char c in devStr.ToCharArray())
{
if (deva2Cyrl.ContainsKey(c))
sb.Append(deva2Cyrl[c]);
else
sb.Append(c);
}
return sb.ToString();
}
public string ConvertDandas(string str)
{
// in gathas, single dandas convert to semicolon, double to period
// Regex note: the +? is the lazy quantifier which finds the shortest match
str = Regex.Replace(str, "<p rend=\"gatha[a-z0-9]*\">.+?</p>",
new MatchEvaluator(this.ConvertGathaDandas));
// remove double dandas around namo tassa
str = Regex.Replace(str, "<p rend=\"centre\">.+?</p>",
new MatchEvaluator(this.RemoveNamoTassaDandas));
// convert all others to period
str = str.Replace("\x0964", ".");
str = str.Replace("\x0965", ".");
return str;
}
public string ConvertGathaDandas(Match m)
{
string str = m.Value;
str = str.Replace("\x0964", ";");
str = str.Replace("\x0965", ".");
return str;
}
public string RemoveNamoTassaDandas(Match m)
{
string str = m.Value;
return str.Replace("\x0965", "");
}
// There should be no spaces before these
// punctuation marks.
public string CleanupPunctuation(string str)
{
str = str.Replace(" ,", ",");
str = str.Replace(" ?", "?");
str = str.Replace(" !", "!");
str = str.Replace(" ;", ";");
// does not affect peyyalas because they have ellipses now
str = str.Replace(" .", ".");
return str;
}
}
}