-
Notifications
You must be signed in to change notification settings - Fork 1
/
GreekChar.java
213 lines (189 loc) · 6.9 KB
/
GreekChar.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.util.*;
import java.util.regex.*;
/**
*
* @author lubo
*/
public class GreekChar {
private static HashMap<String, GreekChar> characters = new java.util.HashMap();
private static HashSet<String> capsSet = new HashSet();
private static HashSet<String> greekSet = new HashSet();
private static HashSet<String> viSet = new HashSet();
private static Pattern pattern;
private static String regEx = "";
public String greek;
public boolean fivi;
public String greeklish;
public boolean bi;
private GreekChar(String greek, boolean fivi, String greeklish, boolean bi)
{
this.greek = greek;
this.fivi = fivi;
this.greeklish = greeklish;
this.bi = bi;
}
private GreekChar(String greek, boolean fivi)
{
this.greek = greek;
this.fivi = fivi;
}
private GreekChar(String greek)
{
this.greek = greek;
}
private GreekChar(String greek, String greeklish)
{
this.greek = greek;
this.greeklish = greeklish;
}
private static void put(GreekChar cr)
{
if (regEx.length() != 0)
regEx = regEx + "|";
regEx = regEx + cr.greek;
characters.put(cr.greek, cr);
}
private static void put(String greek, String greeklish)
{
if (regEx.length() != 0)
regEx = regEx + "|";
regEx = regEx + greek;
characters.put(greek, new GreekChar(greek, greeklish));
}
private static String fixCase(String text, String mirror)
{
String c_0 = String.valueOf(mirror.charAt(0));
if (capsSet.contains(c_0)) {
if (mirror.length() == 1 || capsSet.contains(String.valueOf(mirror.charAt(1)))) {
return text.toUpperCase();
} else {
return String.valueOf(text.charAt(0)).toUpperCase() + (text.length()>=2 ? String.valueOf(text.charAt(1)) : "");
}
}
else
{
return text;
}
}
public static String translate(String text)
{
if (null == text)
{
return null;
}
int length = text.length();
if (0 == length)
{
return null;
}
int i_1, i_2; String c_1, c_2, replace, group, lower;
GreekChar gc;
StringBuffer sb = new StringBuffer();
Matcher m = GreekChar.pattern.matcher(text);
while (m.find()) {
replace = "";
group = m.group();
lower = group.toLowerCase(); // ΤΣ -> τς
gc = GreekChar.characters.get(lower);
if (gc.bi)
{
i_1 = m.start() -1;
i_2 = m.start() +2;
c_1 = i_1 >= 0 ? String.valueOf(text.charAt(i_1)).toLowerCase() : "";
c_2 = i_2 < length ? String.valueOf(text.charAt(i_2)).toLowerCase() : "";
if (GreekChar.greekSet.contains(c_1) && GreekChar.greekSet.contains(c_2))
{
replace = GreekChar.fixCase("mp", group);
}
else
{
replace = GreekChar.fixCase("b", group);
}
}
else
{
if (gc.fivi)
{
i_2 = m.start() +2;
c_1 = GreekChar.characters.get(String.valueOf(group.charAt(0)).toLowerCase()).greeklish;
c_2 = i_2 < length ? (GreekChar.viSet.contains(String.valueOf(text.charAt(i_2)).toLowerCase()) ? "v" : "f") : "";
replace = GreekChar.fixCase(c_1 + c_2, group);
}
else
{
i_1 = m.start() + group.length();
c_1 = i_1 < length ? String.valueOf(text.charAt(i_1)) : " ";
replace = GreekChar.fixCase(gc.greeklish, group + c_1);
}
}
m.appendReplacement(sb, replace);
}
m.appendTail(sb);
return sb.toString();
}
static
{
GreekChar.put(new GreekChar("αι", "ai"));
GreekChar.put(new GreekChar("αί", "ai"));
GreekChar.put(new GreekChar("οι", "oi"));
GreekChar.put(new GreekChar("οί", "oi"));
GreekChar.put(new GreekChar("ου", "ou"));
GreekChar.put(new GreekChar("ού", "ou"));
GreekChar.put(new GreekChar("ει", "ei"));
GreekChar.put(new GreekChar("εί", "ei"));
GreekChar.put(new GreekChar("αυ", true));
GreekChar.put(new GreekChar("αύ", true));
GreekChar.put(new GreekChar("ευ", true));
GreekChar.put(new GreekChar("εύ", true));
GreekChar.put(new GreekChar("ηυ", true));
GreekChar.put(new GreekChar("ηύ", true));
GreekChar.put(new GreekChar("ντ", "nt"));
GreekChar.put(new GreekChar("μπ", false, null, true));
GreekChar.put(new GreekChar("τσ", "ts"));
GreekChar.put(new GreekChar("τς", "ts"));
GreekChar.put(new GreekChar("τζ", "tz"));
GreekChar.put(new GreekChar("γγ", "ng"));
GreekChar.put(new GreekChar("γκ", "gk"));
GreekChar.put(new GreekChar("γχ", "nch"));
GreekChar.put(new GreekChar("γξ", "nx"));
GreekChar.put(new GreekChar("θ" , "th"));
GreekChar.put(new GreekChar("χ" , "ch"));
GreekChar.put(new GreekChar("ψ" , "ps"));
String grLetters = "αάβγδεέζηήθιίϊΐκλμνξοόπρσςτυύϋΰφχψωώ";
String engLetters = "aavgdeezii.iiiiklmnxooprsstyyyyf..oo";
String chGreek, chLatin;
for(int i = 0, l = grLetters.length(); i<l; i++)
{
chGreek = String.valueOf(grLetters.charAt(i));
chLatin = String.valueOf(engLetters.charAt(i));
if (!GreekChar.characters.containsKey(chGreek))
{
GreekChar.put(chGreek, chLatin);
}
}
for(int i = 0, l = grLetters.length(); i<l; i++)
{
chGreek = String.valueOf(grLetters.charAt(i));
GreekChar.greekSet.add(chGreek);
}
grLetters = "ΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ";
for(int i = 0, l = grLetters.length(); i<l; i++)
{
chGreek = String.valueOf(grLetters.charAt(i));
GreekChar.capsSet.add(chGreek);
}
grLetters = "αβγδεζηλιmμνορω";
for(int i = 0, l = grLetters.length(); i<l; i++)
{
chGreek = String.valueOf(grLetters.charAt(i));
GreekChar.viSet.add(chGreek);
}
pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
}
}