lib/Convert/Dayi.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 91.6% 22 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 33.3% 2 / 6 totaltotal 93.2% 96 / 103 CC 15 SCAR 11.5
CC
15
Cov
97%
from_ascii
4.1
init_keys
4.0
init_words
4.0
CRAP 3.2
↑ Summary
1 package Convert::Dayi;
2  
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =encoding utf8
8  
9 =head1 NAME
10  
11 Convert::Dayi - Dayi Convention Module
12  
13 =head1 VERSION
14  
15 Version 0.05
16  
17 =cut
18  
19 1 our $VERSION = '0.05';
20 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our @ISA = qw(Exporter);
22 1 our @EXPORT_OK = qw(as_ascii as_dayi from_ascii);
23  
24 =head1 SYNOPSIS
25  
26 use utf8; # you will need this
27 use Convert::Dayi qw(as_ascii as_dayi from_ascii);
28  
29 print as_ascii("大易中文") # v db/ o1 kx
30  
31 print as_dayi("大易中文") # 禾 日馬竹 口言 立水
32  
33 for from_ascii function , you need to provide index (start from 1)
34  
35 print from_ascii('v1 db/1') # 大易
36  
37 print from_ascii('v db/') # 大易 (if the word is not defined , select index 1 by default)
38  
39 to translate chinese from STDIN:
40  
41 #!/usr/bin/env perl
42 use utf8;
43 use Convert::Dayi qw(as_ascii);
44 use Encode;
45 while( <STDIN> ) {
46 my $word = Encode::decode_utf8( $_ );
47 print as_ascii( $word );
48 }
49  
50 =head1 DESCRIPTION
51  
52 L<Convert::Dayi> module provides a way to convert chinese words to ascii code by Input Method Keys.
53  
54 You can also convert the keycode to chinese back.
55  
56 =head1 EXPORT
57  
58 =head1 FUNCTIONS
59  
60 =cut
61  
62 1 our %KEYS = ();
63 1 our %KEYS_REV = ();
64 1 our %WORDS = ();
65 1 our %WORDS_REV = ();
66  
67 1 ▶INIT {
Subroutine INIT: called CC=1 SCAR=0.0
68 1 init();
69 }
70  
71 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
72 1 init_keys();
73 1 init_words();
74 }
75  
76 1 ▶sub init_keys {
Subroutine init_keys: called CC=4 SCAR=13.9
Pod undocumented
77 1 ▶ while( <DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 86 0 86
78 86 ▶ if( /^KEYPROMPT\((.)\):\s+(.)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^KEYPROMPT\((.)\):\s+(.)/) { } 46 40 86
elsif (/BEGINDICTIONARY/) { } 1 39 40
79 46 $KEYS{ $1 } = $2;
80 46 $KEYS_REV{ $2 } = $1;
81 }
82 elsif( /BEGINDICTIONARY/ ) {
83 1 last;
84 }
85 }
86  
87 }
88  
89 1 ▶sub init_words {
Subroutine init_words: called CC=4 SCAR=13.9
Pod undocumented
90 1 ▶ while( <DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 13414 1 13415
91 13414 ▶ if( $_ !~ m/^#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /^#/) 13413 1 13414
92 13413 my ($key,$word) = split /\s/;
93 13413 $WORDS{ $word } = $key;
94 13413 my $key_index = 0;
95 13413 ▶ while( defined( $WORDS_REV{ $key . ++$key_index } ) ) { }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Convert::Dayi::WORDS_REV{$key . ++$key_index}) 3163 13413 16576
96 13413 $WORDS_REV{ $key . $key_index } = $word;
97 }
98 }
99 }
100  
101 # must provide index
102 =head2 from_ascii
103  
104 =cut
105  
106 1 ▶sub from_ascii {
Subroutine from_ascii: called CC=4 SCAR=14.0
Pod undocumented
107 1 my $string = shift;
108 1 my $out = '';
109 1 for( split /\s/,$string ) {
110 3 ▶ if( defined $WORDS_REV{$_} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Convert::Dayi::WORDS_REV{$_}) { } 2 1 3
elsif (defined $Convert::Dayi::WORDS_REV{$_ . '1'}) { } 0 1 1
111 2 $out .= $WORDS_REV{$_};
112 }
113 elsif( defined $WORDS_REV{$_ . '1' } ) {
114 0 $out .= $WORDS_REV{$_ . '1'}; # try index 1
115 }
116 else {
117 1 $out .= $_;
118 }
119 }
120 1 return $out;
121 }
122  
123 =head2 as_dayi
124  
125 =cut
126  
127 1 ▶sub as_dayi {
Subroutine as_dayi: called CC=4 SCAR=13.9
Pod documented
128 1 my $string = shift;
129 1 my $out = '';
130 1 ▶ while( $string =~ /(.)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /(.)/g) 7 1 8
131 7 my $word = $1;
132  
133 7 ▶ if( defined $WORDS{$word} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Convert::Dayi::WORDS{$word}) { } 4 3 7
134 4 my $keys = $WORDS{ $word } ;
135 4 for ( split //,$keys ) {
136 8 $out .= $KEYS{ $_ };
137 }
138 4 $out .= ' ';
139 }
140 else {
141 3 $out .= $word;
142 }
143 }
144 1 return $out;
145 }
146  
147 =head2 as_ascii
148  
149 =cut
150  
151 1 ▶sub as_ascii {
Subroutine as_ascii: called CC=3 SCAR=11.0
Pod documented
152 1 my $string = shift;
153 1 my $out = '';
154 1 ▶ while( $string =~ /(.)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /(.)/g) 9 1 10
155 9 my $word = $1;
156 9 ▶ if( defined $WORDS{$word} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Convert::Dayi::WORDS{$word}) { } 4 5 9
157 4 $out .= $WORDS{$word} . " ";
158 }
159 else {
160 5 $out .= $word;
161 }
162 }
163 1 return $out;
164 }
165  
166  
167 =head1 AUTHOR
168  
169 Cornelius, C<< <cornelius.howl_at_gmail.com> >>
170  
171 =head1 BUGS
172  
173 Please report any bugs or feature requests to C<bug-convert-dayi at rt.cpan.org>, or through
174 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Convert-Dayi>. I will be notified, and then you'll
175 automatically be notified of progress on your bug as I make changes.
176  
177  
178  
179  
180 =head1 SUPPORT
181  
182 You can find documentation for this module with the perldoc command.
183  
184 perldoc Convert::Dayi
185  
186  
187 You can also look for information at:
188  
189 =over 4
190  
191 =item * RT: CPAN's request tracker
192  
193 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Convert-Dayi>
194  
195 =item * AnnoCPAN: Annotated CPAN documentation
196  
197 L<http://annocpan.org/dist/Convert-Dayi>
198  
199 =item * CPAN Ratings
200  
201 L<http://cpanratings.perl.org/d/Convert-Dayi>
202  
203 =item * Search CPAN
204  
205 L<http://search.cpan.org/dist/Convert-Dayi/>
206  
207 =back
208  
209  
210 =head1 ACKNOWLEDGEMENTS
211  
212  
213 =head1 COPYRIGHT & LICENSE
214  
215 Copyright 2009 Cornelius, all rights reserved.
216  
217 This program is free software; you can redistribute it and/or modify it
218 under the same terms as Perl itself.
219  
220  
221 =cut
222  
223 1 1; # End of Convert::Dayi
224 __DATA__
↑ Summary