blib/lib/Lingua/ZH/MMSEG.pm

filter: Statementstmt 100% 15 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 20 / 20 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Lingua::ZH::MMSEG;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Encode qw (is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use encoding 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use List::Util qw(sum);
8  
9 our $VERSION=0.4005;
10  
11 require Exporter;
12 our @ISA = qw(Exporter);
13 our @EXPORT = qw(mmseg fmm word_freq);
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Lingua::ZH::MMSEG Mandarin Chinese segmentation
20  
21 =head1 SYNOPSIS
22  
23 #!/usr/bin/perl
24 use utf8;
25 use Lingua::ZH::MMSEG;
26  
27 my $zh_string="現代漢語的複合動詞可分三個結構語意關係來探討";
28  
29 my @phrases = mmseg($zh_string);
30 # use MMSEG algorithm
31  
32 my @phrases = fmm($zh_string);
33 # use Forward Maximum Matching algorithm
34  
35 while (<>) {
36 chomp;
37 push @phrases, mmseg;
38 } # mmseg and fmm will parse $_ automaticly
39  
40 print $_, word_freq($_) for @phrases;
41 # you can get phrase frequency by calling word_freq
42  
43 =head1 DESCRIPTION
44  
45 A problem in computational analysis of Chinese text is that there are no word
46 boundaries in conventionally printed text. Since the word is such a fundamental
47 linguistic unit, it is necessary to identify words in Chinese text so that
48 higher-level analyses can be performed.
49  
50 Lingua::ZH::MMSEG implements L<MMSEG|http://technology.chtsai.org/mmseg/>
51 original developed by L<Chih-Hao-Tsai|http://chtsai.org/>. The whole module is
52 rewritten in pure Perl, and the phrase library is
53 L<新酷音 forked from OpenFoundry|http://www.openfoundry.org/of/projects/436>.
54  
55 =head1 INSTALL
56  
57 To install this module, just type
58  
59 cpanm Lingua::ZH::MMSEG
60  
61 If you don't have cpanm,
62  
63 curl -LO http://bit.ly/cpanm
64 chmod +x cpanm
65 sudo cp cpanm /usr/local/bin
66  
67 =head1 FUNCTIONS
68  
69 =head2 mmseg
70  
71 @phrases = mmseg($zh_string);
72 @phrases = mmseg;
73 # use $_ automatically
74  
75 C<mmseg> convert a mandarin Chinese string to a sequence of phrases using
76 L<MMSEG|http://technology.chtsai.org/mmseg/> algorithm. If there were any
77 english containted in the input string, it simply parse the linked ascii code as
78 one phrase. For example:
79  
80 $_ = "這裡有中文Today is Wednesday.這邊又有中文 I go to school on Friday.";
81 print "$_\n" for mmseg;
82  
83 這裡有
84 中文
85 Today is Wednesday.
86 這邊
87 又有
88 中文
89 I go to school on Friday.
90  
91 The ascii characters are recognized by C</[ -~]+/>.
92  
93 =head2 fmm (Forward Maximum Matching)
94  
95 @phrases = fmm($zh_string);
96 @phrases = fmm;
97 # use $_ automatically
98  
99 C<fmm> uses forward maximum matching (so called longest match principle) to
100 convert a mandarin Chinese string to a sequence of phrases. It uses the same
101 rule of C<mmseg> to deal with ascii string. The advantage of C<fmm> is it has
102 lower complexity compare to C<mmseg>; the disadvantage is it cannot solve
103 ambiguity when there is multiple way to seperate a string.
104  
105 =head2 word_freq
106  
107 $freq = word_freq($phrase);
108 $freq = word_freq;
109 # use $_ automatically
110  
111 C<word_freq> return the phrase frequency defined in L<新酷音|http://www.openfoundry.org/of/projects/436>.
112  
113 =head1 AUTHOR
114  
115 Felix Ren-Chyan Chern (dryman) C<< <idryman@gmail.com> >>
116  
117 =head1 LICENSE AND COPYRIGHT
118  
119 L<GNU Lesser General Public License 2.1 |http://www.opensource.org/licenses/lgpl-2.1.php>
120  
121 =cut
122  
123 our %dict;
124  
125 while (<DATA>){
126 chomp;
127 my ($phrase,$freq) = split;
128 $dict{$phrase}=$freq;
129 }
130  
131 sub word_freq {
132 my $string = $_[0] ? $_[0] : $_;
133 $dict{$string};
134 }
135  
136 sub mmseg {
137 my $string = $_[0] ? $_[0] : $_;
138 my @phrases;
139 die unless is_utf8($string);
140 chomp ($string);
141 for my $str (split (/([ -~]+)/, $string)) {
142 if ($str =~ /^[ -~]/) {
143 push @phrases, $str;
144 next;
145 }
146 while($str){
147 my $word1 = &_mmseg($str);
148 push @phrases, $word1;
149 $str = substr $str, length $word1;
150 }
151 }
152 return @phrases;
153 }
154  
155 sub fmm {
156 my $string = $_[0] ? $_[0] : $_;
157 my @phrases;
158 die unless is_utf8($string);
159 chomp ($string);
160 for my $str (split (/([ -~])+/, $string)) {
161 if ($str =~ /^[ -~]/) {
162 push @phrases, $str,
163 next;
164 }
165 while($str){
166 for (reverse (1..(length $str))) {
167 my $match = substr $str, 0, $_;
168 if (defined $dict{$match} or $_==1){
169 push @phrases, $match;
170 $str = substr $str, $_;
171 last;
172 }
173 }
174 }
175 }
176 return @phrases;
177 }
178  
179
180 sub _mmseg {
181 my $str = shift;
182 my @chunk = &_findChunk($str);
183  
184 return $chunk[0]->{w1} if $#chunk == 0;
185  
186 # rule 1, find max length chunks
187 my @mlc_tmp = sort {$b->{len} <=> $a->{len}} @chunk;
188 my @max_len_chunk = grep {$_->{len} == $mlc_tmp[0]->{len}} @mlc_tmp;
189 return $max_len_chunk[0]->{w1} if $#max_len_chunk == 0;
190  
191 # rule 2, find max avg length chunks
192 my @malc_tmp = sort {$b->{avglen} <=> $a->{avglen}} @max_len_chunk;
193 my @max_avglen_chunk = grep {$_->{avglen} == $malc_tmp[0]->{avglen}} @malc_tmp;
194 return $max_avglen_chunk[0]->{w1} if $#max_avglen_chunk == 0;
195  
196 # rule 3, smallest varience
197 for (@max_len_chunk) {
198 my $avg = $_->{avglen};
199 my @word = ($_->{w1},$_->{w2},$_->{w3});
200 pop @word unless $word[$#word];
201 my @len = map {length $_} @word;
202 my $varience = sqrt ((sum (map {$_**2-$avg**2} @len))/(scalar @len));
203 $_->{varience} = $varience;
204 }
205 my @mvc_tmp = sort {$a->{varience} <=> $b->{varience}} @max_len_chunk;
206 my @min_varience_chunk =
207 grep {abs($_->{varience} - $mvc_tmp[0]->{varience})<0.01} @mvc_tmp;
208 return $min_varience_chunk[0]->{w1} if $#min_varience_chunk == 0;
209 # rule 4, check length one word and choose max freq of it
210 for (@min_varience_chunk) {
211 my $freq = 0;
212 $freq += $dict{$_->{w1}} if length $_->{w1} == 1 and defined $dict{$_->{w1}};
213 $freq += $dict{$_->{w2}} if length $_->{w2} == 1 and defined $dict{$_->{w2}};
214 $freq += $dict{$_->{w3}} if length $_->{w3} == 1 and defined $dict{$_->{w3}};
215 $_->{freq} = $freq;
216 }
217 my @last = sort {$b->{freq} <=> $a->{freq}} @min_varience_chunk;
218 return $last[0]->{w1};
219 }
220  
221
222  
223  
224 sub _findChunk{
225 my $str = shift;
226 my $index = 0;
227 my @chunk;
228  
229 my @word1;
230 # will fail if $str=""
231 for (1..(length($str) - $index)){
232 my $substr = substr $str, $index, $_;
233 push @word1, $substr if defined $dict{$substr} or $_==1;
234 }
235 foreach my $w1 (@word1){
236 my $l1 = length $w1;
237 my $index = $index + $l1;
238 my @word2;
239
240 if (length($str) - $index == 0){
241 push @chunk, {
242 w1 => $w1,
243 w2 => undef,
244 w3 => undef,
245 len => $l1,
246 avglen => $l1,
247 };
248 next;
249 }
250  
251 for (1..(length($str) - $index)){
252 my $substr = substr $str, $index, $_;
253 push @word2, $substr if defined $dict{$substr} or $_==1;
254 }
255  
256 foreach my $w2 (@word2){
257 my $l2 = length $w2;
258 my $index = $index + $l2;
259  
260 if (length($str) - $index == 0){
261 push @chunk, {
262 w1 => $w1,
263 w2 => $w2,
264 w3 => undef,
265 len => $l1+$l2,
266 avglen => ($l1+$l2)/2,
267 };
268 next;
269 }
270  
271 for (1..(length($str) - $index)){
272 my $substr = substr $str, $index, $_;
273 if (defined $dict{$substr} or $_==1){
274 my $w3 = $substr;
275 my $l3 = length $w3;
276 push @chunk, {
277 w1 => $w1,
278 w2 => $w2,
279 w3 => $w3,
280 len => $l1+$l2+$l3,
281 avglen => ($l1+$l2+$l3)/3,
282 };
283 }
284 }
285 }
286 }
287 return @chunk;
288 }
289  
290 1;
291  
292 __DATA__
↑ Summary