-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtelugu_parser.php
More file actions
180 lines (160 loc) · 5.11 KB
/
telugu_parser.php
File metadata and controls
180 lines (160 loc) · 5.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
<?php
// telugu_parser.php primarily deals with splitting an input string
// into a series of logical characters
// contact Siva.Jasthi@metrostate.edu for any clarifications on
// telugu processing logic used in these functions.
function stripSpacesTelugu($log_chars) {
$code_points = parseToCodePoints(implode($log_chars));
$build = array();
$build_i = 0;
for($i=0; $i < count($code_points); $i++) {
if(strcmp($log_chars[$i], " ") == 0) continue;
else {
$build[$build_i++] = $log_chars[$i];
if(isHalant(end($code_points[$i]) && $i + 1 < count($code_points))) {
if($code_points[$i+1] == 32) { // if the next character is a space...
$build[$build_i][count($build[$build_i])] = json_decode("\u200c");
}
}
}
}
return $build;
}
// $word expects a single utf-8 encoded word
// returns a 2 dimensional array, representing the unicoded logical characters of the word
function parseToCodePoints($word) {
$word_array = explode_telugu(json_encode($word));
$i = 0;
$logical_chars = array();
$ch_buffer = array();
while( $i < count($word_array) ) {
$current_ch = $word_array[$i++];
$ch_buffer[count($ch_buffer)] = $current_ch;
if( $i == count($word_array) ) {
$logical_chars[count($logical_chars)] = $ch_buffer;
continue;
}
$next_ch = $word_array[$i];
if(isDependent($next_ch)) {
$ch_buffer[count($ch_buffer)] = $next_ch;
$i++;
$logical_chars[count($logical_chars)] = $ch_buffer;
$ch_buffer = array();
continue;
}
if(isHalant($current_ch)) {
if(isConsonant($next_ch)) {
if($i < count($word_array)) {
continue;
}
$ch_buffer[count($ch_buffer)] = $current_ch;
}
$logical_chars[count($logical_chars)] = $ch_buffer;
$ch_buffer = array();
continue;
} else if(isConsonant($current_ch)) {
if(isHalant($next_ch) || isDependentVowel($next_ch)) {
if($i < count($word_array)) {
continue;
}
$ch_buffer[count($ch_buffer)] = $current_ch;
}
$logical_chars[count($logical_chars)] = $ch_buffer;
$ch_buffer = array();
continue;
} else if(isVowel($current_ch)) {
if(isDependentVowel($next_ch)) {
$ch_buffer[count($ch_buffer)] = $next_ch;
$i++;
}
$logical_chars[count($logical_chars)] = $ch_buffer;
$ch_buffer = array();
continue;
}
$logical_chars[count($logical_chars)] = $ch_buffer;
$ch_buffer = array();
}
return $logical_chars;
}
// returns a 2d array of the logical characters, but using Telugu characters
function parseToLogicalCharacters($word) {
if(is_array($word)) {
for($i=0; $i < count($word); $i++)
$word[$i] = parseToCharacter($word[$i]);
return $word;
}
else return parseToLogicalCharacters(parseToCodePoints($word));
}
function parseToCharacter($logical_char) {
$telugu_char = "";
foreach($logical_char as $char) {
if(isTelugu($char)) $telugu_char .= sprintf("\\u%'04s", dechex($char));
else return chr($char);
}
return json_decode('"'.$telugu_char.'"');
}
function explode_telugu($to_explode) {
// POS //
$pos=0;
$e_pos=0;
$exploded = array();
while($pos < strlen($to_explode) - 1) {
if(strcmp($to_explode[$pos], "\"") == 0) {
$pos++;
continue;
}
if(strcmp($to_explode[$pos], "\\") == 0) { // if the the character in question is a slash...
if(strcmp($to_explode[$pos + 1], "u") == 0) { // ...followed by a u...
// PHP7 no longer convert 0x strings to numbers automatically
//$char = 0 + ("0x" . substr($to_explode, $pos + 2, 4)); // worked in old version of php
$char = intval(substr($to_explode, $pos + 2, 4), 16); // convert to a number
if(isTelugu($char)) {
// if it matches, add it as a character, bump the counter up by six, and continue
$exploded[$e_pos++] = $char;
$pos += 6;
continue;
}
}
}
$exploded[$e_pos++] = ord($to_explode[$pos++]);
}
// remove hidden char space 8204
if (($key = array_search('8204', $exploded)) !== false) {
unset($exploded[$key]);
}
$exploded = array_values(array_filter($exploded));
return $exploded;
}
function isConsonant($ch) {
return ( $ch >= 0x0c15 && $ch <= 0x0c39 );
}
function isDependentVowel($ch) {
return ( $ch >= 0x0c3e && $ch <= 0x0c4c );
}
function isDependent($ch) {
return ( ($ch == 0x0c01) || ($ch == 0x0c02) || ($ch == 0x0c03) );
}
function isVowel($ch) {
return ($ch >= 0x0c05 && $ch <= 0x0c14);
}
function isHalant($ch) {
return $ch == 0x0c4d;
}
function isTeluguNumber($ch) {
return ($ch >= 0x0c66 && $ch <= 0x0c6f);
}
// this function is not perfect: there are gaps in the Telugu unicode table,
// so it is theoretically possible to get a false positive. However, other than
// intentionally feeding this function bad data, there's no practical way to get
// that false positive, and nothing harmful would happen if you did
function isTelugu($ch) {
return ( $ch >= 0x0c00 && $ch <= 0x0c7f ) || ( $ch == 0x200c );
}
function is_blank_Telugu($hexVal){
$is_blank = false;
$blankArray = array("c00","c01","c02","c03","c0d","c11","c29","c34");
if(in_array($hexVal, $blankArray)){
return true;
}
return $is_blank;
}