-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconv.php
279 lines (233 loc) · 8.25 KB
/
conv.php
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
<?
header('Content-Type: text/html; charset=utf-8');
if (isset($_GET['src'])) {
die(highlight_file(__FILE__, true));
}
error_reporting(E_ALL);
$dir = isset($_GET['dir']) ? $_GET['dir'] : (isset($_POST['dir']) ? $_POST['dir'] : 1);
$c = isset($_GET['c']) ? $_GET['c'] : (isset($_POST['c']) ? $_POST['c'] : false);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Latviešu translita konvertors</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#wrapper {
width: 80%;
margin: 0 auto;
text-align: left;
}
body {
text-align: center;
font-family: tahoma, sans-serif;
font-size: 90%;
}
h1,
h2 {
font-weight: normal;
color: green;
border-bottom: 1px solid green;
}
h2 {
color: maroon;
border-color: maroon;
border-bottom-style: dashed;
}
.t {
width: 100%;
min-height: 150px;
font-family: "courier new", monospace;
font-size: 95%;
color: #333;
border-style: solid;
border-width: 1px;
border-color: #000 #ccc #ccc #000 ;
background-color: #f0f0f0;
}
.t:focus {
background-color: #ffc;
}
div.t {
width: auto;
}
table td {
border-color: #ccf;
border-width: 1px;
border-style: solid;
padding: 2px;
}
#footer {
border-top: 1px solid #666;
padding-top: .5em;
margin-top: .5em;
}
p.submit {
text-align: center;
}
</style>
</head>
<body>
<div id="wrapper">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-1458323184003510";
/* dev.laacz.lv projects - large */
google_ad_slot = "4627569637";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<h1>Konvertējam latviešu tekstus uz/no translita.</h1>
<!--
<p>Implementācija nav pilnīga, protams. Taču, manām vajadzībām pilnībā pietiek (lai pēc tam izbrauktu cauri ar spellčekeru un visu salabotu, kas palicis neizlabots).</p>
-->
<h2>Ievadi tekstu</h2>
<form action="<?=basename(__FILE__)?>" method="post">
<dl>
<dt><label for="c">Teksts:</label> </dt>
<dd>
<textarea id="c" name="c" class="t"><?=htmlspecialchars($c)?></textarea>
</dd>
<dt>Konvertācija: </dt>
<dd><label for="dir_1">No translita uz parasto:</label> <input type="radio" id="dir_1" name="dir" value="1" <?=$dir == 1 ? 'checked="checked"' : ''?> /> <label for="dir_1">('aa' uz 'ā')</label></dd>
<dd><label for="dir_2">No parastā uz translitu:</label> <input type="radio" id="dir_2" name="dir" value="2" <?=$dir == 2 ? 'checked="checked"' : ''?> /> <label for="dir_2">('ā' uz 'aa')</label></dd>
</dl>
<p class="submit"><input type="submit" value="Spied šo pogu, lai viss notiktu »»" /></p>
</form>
<?
mb_internal_encoding('utf-8');
function strup($str) {
$str = strtoupper($str);
$str = str_replace('ŗ', 'Ŗ', $str);
$str = mb_strtoupper($str);
return($str);
}
function strlow($str) {
$str = strtolower($str);
$str = str_replace('Ŗ', 'ŗ', $str);
$str = mb_strtolower($str);
return($str);
}
$f = Array('aa', 'ch', 'ee', 'gj', 'ii', 'kj', 'lj', 'nj', 'sh', 'uu', 'zh', 'oo');
$t = Array('ā', 'č', 'ē', 'ģ', 'ī', 'ķ', 'ļ', 'ņ', 'š', 'ū', 'ž', 'ō');
// Brute force some of the most common exceptions
$exceptions = Array(
'ieel', # Pieelpot, ieelpa, ieeļļot
'ieej', # Iezvanpieeja, pieeja, ieeja
'(sh)eem', # Shēma, bet ne Šērlija
'(sh)em', # Shematisks
'neeso', 'neesa', # Neesošs, neesamība, bet ne nēši (diemžēl)
'neeko', # Neekonomisks
'neel', # Neelastīgs
'jaunj', # Jaunjelgava
'alcheim', # Alcheimers (due to popular demand)
'sargjos', # Aizsargjosla
'amonjak', # Amonjaks
'anjon', # Anjons, kanjons
'atelj', # Ateljē
'ljard', # Miljards
'ljon', # Miljons, bataljons
'ilja', # Briljants
'ek(sh)[iu]', # Ekshumācija, ekhibicionists :)
'injek', # Injekcija
'injic', # Injicēt
'ema(lj)[ae]', # Emalja (izķer arī "nemaļams", bet lai jau...), emalja
'dishar', # Disharmonija
'konju', # Konjugācija, konjuktivīts, konjuktūra,
'kadiljak', # Kadiljaks
'trolj', # Kontroljautājums
'lielj', # Lieljauda
'reljef', # Reljefs
'senjo', # Senjors
'rienj', # Skārienjūtīgs
'malkj', # Smalkjūtīgs
'vakuum', # Vakuumsūknis
'vinjet', # Vinjete
'vish[aeiou]', # Vishaotiskākais, vish
);
for ($i = 0; $i < count($f); $i++) {
$tolv[$f[$i]] = $t[$i];
if (!isset($fromlv[$t[$i]]) || trim($fromlv[$t[$i]]) == '')
$fromlv[$t[$i]] = $f[$i];
}
function tolv($q) {
global $f, $t, $tolv, $fromlv, $exceptions;
$c = $q;
// Parse exceptions
$replacements = Array(); $i = 0;
foreach ($exceptions as $exception) {
$c = preg_replace_callback('/' . $exception . '/i', function($m) use(&$replacements, &$i){
if (isset($m[1])) {
$tmp = str_replace($m[1], '{{' . $i . '}}', $m[0]);
$replacements[$i] = $m[1];
} else {
$tmp = '{{' . $i . '}}';
$replacements[$i] = $m[0];
}
$i++;
return $tmp;
}, $c);
}
$c = str_replace('eee', 'eē', $c);
$c = str_replace('Eee', 'Eē', $c);
$c = str_replace('eEe', 'eĒ', $c);
$c = str_replace('EEe', 'EĒ', $c);
$c = str_replace('EEE', 'EĒ', $c);
for ($i = 0; $i < strlen($c); $i++) {
$ch = substr($c, $i, 2);
$chs = strlow($ch);
$c1 = substr($ch, 0, 1);
$c2 = substr($ch, 1, 2);
if (in_array($chs, $f)) {
$tmp = $c1 == strup($c1) ? strup($tolv[$chs]) : $tolv[$chs];
$c = substr($c, 0, $i) . $tmp . substr($c, $i+2);
}
}
$c = str_replace('iē', 'iee', $c);
foreach ($replacements as $k=>$v) {
$c = str_replace('{{' . $k . '}}', $v, $c);
}
return ($c);
}
function fromlv($q) {
global $f, $t, $tolv, $fromlv;
$c = $q;
for ($i = 0; $i < mb_strlen($c); $i++) {
$ch = mb_substr($c, $i, 1);
$chs = mb_strtolower($ch, 'UTF-8');
if (in_array($chs, $t)) {
$ch1 = mb_substr($fromlv[$chs], 0, 1);
$ch2 = mb_substr($fromlv[$chs], 1, 2);
if ($ch != $chs) {
$c1 = mb_substr($c, $i+1, 1);
if ($c1 == strup($c1)) {
$tmp = strup($ch1.$ch2);
} else {
$tmp = strup($ch1).$ch2;
}
} else {
$tmp = $fromlv[$chs];
}
$c = mb_substr($c, 0, $i) . $tmp . mb_substr($c, $i+1);
}
}
return ($c);
}
if (strlen($c) && $dir) {
$o = $dir == 1 ? tolv($c) : fromlv($c);
?>
<h2>Rezultāts</h2>
<textarea style="margin-left: 3em; " class="t"><?=htmlspecialchars($o);?></textarea>
<?}?>
<h2>Transliterācijas tabula</h2>
<table>
<tr><th>Transliterācija:</th><?for ($i = 0; $i < count($f); $i++) {?><td><?=strup($f[$i])?></td><?}?></tr>
<tr><th>Ierastā rakstība:</th><?for ($i = 0; $i < count($f); $i++) {?><td><?=strup($t[$i])?></td><?}?></tr>
</table>
<p id="footer">Autors - <a href="http://laacz.lv/">Kaspars Foigts</a>. Kaut kad 2002. gadā, vai? <a href="https://github.com/laacz/misc">Kods githubā</a></p>
</div>
</body>
</html>