-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomanize.js
104 lines (98 loc) · 3.6 KB
/
Romanize.js
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
/**
* Created by gschizas on 17/9/2014.
*/
function romanizeText(greekText) {
var simpleTranslationGreek = "άβδέζήιίϊΐκλνξόπρσςτυύϋΰφωώ";
var simpleTranslationLatin = "avdeziiiiiklnxoprsstyyyyfoo";
var digraphTranslationGreek = "θχψ";
var digraphTranslationLatin = "thchps";
var digraphYpsilonGreek = "αεη";
var digraphYpsilonLatin = "aei";
var digraphYpsilonBeta = "βγδζλμνραάεέηήιίϊΐοόυύϋΰωώ";
var digraphYpsilonPhi = "θκξπστφχψ";
var result = "";
var cursor = 0;
var letter, newLetter, previousLetter, nextLetter, thirdLetter;
var isUpper, isUpper2;
while (cursor < greekText.length) {
letter = greekText.charAt(cursor);
previousLetter = (cursor > 0) ? greekText.charAt(cursor - 1) : "";
nextLetter = (cursor < greekText.length - 1) ? greekText.charAt(cursor + 1) : "";
thirdLetter = (cursor < greekText.length - 2) ? greekText.charAt(cursor + 2) : "";
isUpper = (letter.toUpperCase() == letter);
isUpper2 = (nextLetter.toUpperCase() == nextLetter);
letter = letter.toLowerCase();
previousLetter = previousLetter.toLowerCase();
nextLetter = nextLetter.toLowerCase();
thirdLetter = thirdLetter.toLowerCase();
if (simpleTranslationGreek.indexOf(letter) >= 0) {
newLetter = simpleTranslationLatin.charAt(simpleTranslationGreek.indexOf(letter));
}
else if (digraphTranslationGreek.indexOf(letter) >= 0) {
var diphthong_index = digraphTranslationGreek.indexOf(letter);
newLetter = digraphTranslationLatin.substr(diphthong_index * 2, 2);
}
else if (digraphYpsilonGreek.indexOf(letter) >= 0) {
newLetter = digraphYpsilonLatin.charAt(digraphYpsilonGreek.indexOf(letter));
if (nextLetter == "υ" || nextLetter == "ύ") {
if (digraphYpsilonBeta.indexOf(thirdLetter) >= 0) {
newLetter += "v";
cursor++;
}
else if (digraphYpsilonPhi.indexOf(thirdLetter) >= 0) {
newLetter += "f";
cursor++;
}
}
}
else if (letter == "γ") {
if (nextLetter == "γ") {
newLetter = "ng";
cursor++;
}
else if (nextLetter == "ξ") {
newLetter = "nx";
cursor++;
}
else if (nextLetter == "χ") {
newLetter = "nch";
cursor++;
}
else {
newLetter = "g";
}
}
else if (letter == "μ") {
if (nextLetter == "π") {
if (previousLetter.trim() == "" || thirdLetter.trim() == "") {
newLetter = "b";
cursor++;
}
else {
newLetter = "mp";
cursor++;
}
}
else {
newLetter = "m";
}
}
else if (letter == "ο") {
newLetter = "o";
if (nextLetter == "υ" || nextLetter == "ύ") {
newLetter += "u";
cursor++;
}
}
else {
newLetter = letter;
}
if (isUpper) {
newLetter = newLetter.charAt(0).toUpperCase() +
(isUpper2 ? newLetter.substring(1).toUpperCase() : newLetter.substring(1).toLowerCase());
}
result += newLetter;
cursor++;
}
return result;
}