blib/lib/Lingua/IW/Logical.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 81 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 398 CC 48 SCAR 68.1
CC
48
Cov
0%
log2vis_string
1332.0
get_type
72.0
log2vis_text
42.0
CRAP 903.5
1 #!/usr/bin/perl -w
2 package Lingua::IW::Logical;
3  
4 use strict;
5 use integer;
6  
7 require Exporter;
8  
9 @Lingua::IW::Logical::ISA = qw(Exporter);
10 @Lingua::IW::Logical::EXPORT = qw(log2vis_string log2vis_text);
11 @Lingua::IW::Logical::EXPORT_OK = qw(set_debug);
12 $Lingua::IW::Logical::VERSION="0.5";
13  
14 ################################################################################
15 # Logical-Visual Hebrew subroutines
16 # Author: Stanislav Malyshev <frodo@sharat.co.il>
17 # Date: 02/08/1998
18 # Based on the algorithm from the book 'The Unicode Standard, Version 2.0'
19 # Redistribution and modification of the code is allowed freely under LGPL terms
20 ################################################################################
21  
22 my($ALEPH,$TAV,$debug,$STRONG_RTL,$STRONG_LTR,$WEAK_EN,$WEAK_CS,$WEAK_ET,$WEAK_ES,$NEUTRAL_WS,$NEUTRAL_ON,%mirror);
23  
24 $ALEPH='à';
25 $TAV='ú';
26 $debug=0; # put 1 here for full report of what algorithm is doing
27  
28 # this is entity-type constants
29 # see Unicode 2.0, page 4-11
30 $STRONG_RTL=0;
31 $STRONG_LTR=1;
32 $WEAK_EN=2;
33 $WEAK_CS=3;
34 $WEAK_ET=6;
35 $WEAK_ES=7;
36 $NEUTRAL_WS=4;
37 $NEUTRAL_ON=5;
38  
39 # This is the list of "mirrored" characters
40 # see Unicode 2.0, page 4-22
41 %mirror = (
42 '(' => ')',
43 ')' => '(',
44 '[' => ']',
45 ']' => '[',
46 '{' => '}',
47 '}' => '{',
48 );
49  
50 # subroutine to get type of a character
51 # needs to be converted to a hash for efficiency
52 sub get_type {
53 my($l)=@_;
54 return $STRONG_RTL if($l ge $ALEPH && $l le $TAV);
55 return $STRONG_LTR if($l =~ /[a-zA-Z]/);
56 return $WEAK_EN if($l =~ /[0-9]/);
57 return $WEAK_ET if($l =~ m|[-\$%+\^#]|);
58 return $WEAK_ES if($l =~ m%[./\(\)]%);
59 return $WEAK_CS if($l =~ /[:,;]/);
60 return $NEUTRAL_WS if($l =~ /\s/);
61  
62 return $NEUTRAL_ON;
63 }
64  
65 sub set_debug {
66 my($db)=@_;
67 $debug=$db;
68 }
69  
70 # main reverse subroutine
71 # expects text in "logical" encoding, converts to "visual"
72  
73 sub log2vis_string ($) {
74 my($str)=@_;
75 my($i)=();
76 return $str unless $str =~ /[$ALEPH-$TAV]/o; # shortcut - no hebrew
77
78 my($len)=length($str);
79 # making levels
80 my(@str_types)=map { &get_type($_) } split(//,$str); # get character types
81  
82 print "{",join(":",@str_types),"}\n" if $debug;
83  
84 # resolving weak types
85 # see page 3-15 to 3-23
86 for($i=0;$i<$len;$i++) {
87
88 # EN,ES,EN -> EN,EN,EN
89 if($str_types[$i] == $WEAK_ES && $str_types[$i-1] == $WEAK_EN && $str_types[$i+1] == $WEAK_EN) {
90 $str_types[$i]=$WEAK_EN;
91 next;
92 }
93
94 # EN,CS,EN -> EN,EN,EN
95 if($str_types[$i] == $WEAK_CS && $str_types[$i-1] == $WEAK_EN && $str_types[$i+1] == $WEAK_EN) {
96 $str_types[$i]=$WEAK_EN;
97 next;
98 }
99  
100 # EN, ET -> EN,EN
101 if($i>0 && $str_types[$i-1] == $WEAK_EN && $str_types[$i] == $WEAK_ET && $str_types[$i+1] != $STRONG_RTL) {
102 $str_types[$i]=$WEAK_EN;
103 next;
104 }
105  
106 # ET, EN -> EN,EN
107 if($str_types[$i+1] == $WEAK_EN && $str_types[$i] == $WEAK_ET) {
108 $str_types[$i]=$WEAK_EN;
109 next;
110 }
111  
112 # otherwise: L, ES, EN -> L, N, EN
113 # etc.
114 ## if($i>0 && $str_types[$i-1] == $STRONG_LTR && $str_types[$i+1] == $WEAK_EN) {
115 if($str_types[$i] == $WEAK_CS || $str_types[$i] == $WEAK_ES || $str_types[$i] == $WEAK_ET) {
116 $str_types[$i]=$NEUTRAL_ON;
117 next;
118 }
119  
120 } ## for
121
122 print "<",join(":",@str_types),">\n" if $debug;
123  
124 # making directions
125 # r - RTL, l - LTR, n - neutral (takes current direction), e - embedding level direction
126
127 my($levels)='-' x $len; # initially characters have no directionality
128 my($base)=''; # base directionality of the string
129 my($last_strong,@next_strong)=();
130 for($i=0;$i<$len;$i++) {
131 # first strong character is LTR - all before are LTR
132 if($str_types[$i] == $STRONG_LTR) {
133 substr($levels,$i,1) = 'l';
134 # substr($levels,0,$i) = 'l' x $i unless $base;
135 $base='l' unless $base;
136 for(my($j)=$last_strong;$j<$i;$j++) { $next_strong[$j]=$STRONG_LTR; }
137 $last_strong=$i;
138 next;
139 }
140  
141 # first strong character is RTL - all before are RTL
142 if($str_types[$i] == $STRONG_RTL) {
143 substr($levels,$i,1) = 'r';
144 # substr($levels,0,$i) = 'r' x $i unless $base;
145 $base='r' unless $base;
146 for(my($j)=$last_strong;$j<$i;$j++) { $next_strong[$j]=$STRONG_RTL; }
147 $last_strong=$i;
148 next;
149 }
150
151 # directioning neutrals
152 if($str_types[$i] == $NEUTRAL_ON || $str_types[$i] == $NEUTRAL_WS) {
153
154 # RNR -> RRR
155 if($str_types[$i-1] == $STRONG_RTL && $str_types[$i+1] == $STRONG_RTL) {
156 substr($levels,$i,1)='r';
157 next;
158 }
159
160 # LNL -> LLL
161 if($str_types[$i-1] == $STRONG_LTR && $str_types[$i+1] == $STRONG_LTR) {
162 substr($levels,$i,1)='l';
163 next;
164 }
165
166 # RNL -> ReL
167 if($str_types[$i-1] == $STRONG_RTL && $str_types[$i+1] == $STRONG_LTR) {
168 substr($levels,$i,1)='e';
169 next;
170 }
171
172 # LNR -> LeR
173 if($str_types[$i-1] == $STRONG_LTR && $str_types[$i+1] == $STRONG_RTL) {
174 substr($levels,$i,1)='e';
175 next;
176 }
177
178 # RNW -> RR?
179 if($str_types[$i-1] == $STRONG_RTL && $str_types[$i+1] == $WEAK_EN) {
180 substr($levels,$i,1)='r';
181 next;
182 }
183  
184 # LNW -> LL?
185 if($str_types[$i-1] == $STRONG_LTR && $str_types[$i+1] == $WEAK_EN) {
186 substr($levels,$i,1)='l';
187 next;
188 }
189
190 # if basic directionality is RTL : WNL -> ?LL
191 if($base == 'r' && $str_types[$i-1] == $WEAK_EN && $str_types[$i+1] == $STRONG_LTR) {
192 substr($levels,$i,1)='l';
193 next;
194 }
195  
196 # if basic directionality is RTL : WNR -> ?RR
197 if($base == 'r' && $str_types[$i-1] == $WEAK_EN && $str_types[$i+1] == $STRONG_RTL) {
198 substr($levels,$i,1)='r';
199 next;
200 }
201  
202 substr($levels,$i,1)='e'; # default for neutrals is 'e'
203 next;
204 }
205  
206 # weak entity
207 if($str_types[$i] == $WEAK_EN) {
208 substr($levels,$i,1) = 'n';
209 next;
210 }
211  
212 substr($levels,$i,1) = 'e'; # if not matched - take 'e'
213 }
214  
215 print $levels,"\n" if $debug;
216  
217 # compose string
218 my($dir)=$base; ##substr($str,0,1); # current direction
219 my($cursor)=0;
220 my($outstr)='';
221 my($nowdir)=$dir;
222 for($i=0;$i<$len;$i++) {
223 my($c)=substr($str,$i,1);
224 if(substr($levels,$i,1) eq 'l') {
225 $dir = 'l';
226 }
227 if(substr($levels,$i,1) eq 'r') {
228 $dir = 'r';
229 }
230  
231 if(substr($levels,$i,1) eq 'e') {
232 if($dir eq 'r' && $next_strong[$i] == $STRONG_LTR) {
233 # $dir = 'l';
234 }
235 elsif($dir eq 'l' && $next_strong[$i] == $STRONG_RTL) {
236 $dir = 'r'
237 }
238 elsif($next_strong[$i] == '') {
239 $dir = $base;
240 }
241 }
242  
243 $nowdir=$dir;
244
245 # space between LTR and RTL is moved towards RTL, like
246 # abc ABC -> CBA abc
247 if(substr($levels,$i,1) eq 'e' && $dir eq 'l' && $next_strong[$i] == $STRONG_RTL) {
248 $nowdir = 'r';
249 }
250  
251 if(substr($levels,$i,1) eq 'n') {
252 $nowdir='l';
253 }
254
255 $cursor=0 if $nowdir eq 'r';
256  
257 $c=$mirror{$c} if($nowdir eq 'r' && $mirror{$c} ne '');
258
259 substr($outstr,$cursor,0)=$c;
260
261 $cursor++ if $nowdir eq 'l';
262 print "$dir:$nowdir:$cursor: [$outstr]\n" if $debug;
263 }
264
265 return $outstr;
266  
267 }
268  
269  
270 # this one works with texts, i.e. handles linebreaks, etc. and splits text on the given width
271 sub log2vis_text {
272 my($text,$string_len,$before,$after) = @_;
273  
274 $string_len = 80 unless $string_len;
275 $after = "\n" unless defined($after);
276  
277 my($logstr,$outtext,$visstr);
278 while($text =~ /(.*(\n|$))/g) { # for each line
279 next unless $1;
280 $logstr = $1;
281 chomp($logstr);
282 $visstr = log2vis_string($logstr);
283
284 while(length($visstr) > $string_len) { # we need to divide
285 substr($visstr,-$string_len) =~ /.*?\s(.*)/; # find first space after length
286 $outtext .= ( defined($before) ? $before : ' ' x ($string_len - length($1) - 1) ). $1 . $after; #
287 substr($visstr,-length($1))='';
288 }
289
290 $outtext .= (defined($before) ? $before : ' ' x ($string_len - length($visstr) - 1) ) . $visstr . $after;
291 }
292 return $outtext;
293 }
294