line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Util::Chinese; |
2
|
6
|
|
|
6
|
|
10214
|
use strict; |
|
6
|
|
|
|
|
41
|
|
|
6
|
|
|
|
|
173
|
|
3
|
6
|
|
|
6
|
|
32
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
144
|
|
4
|
6
|
|
|
6
|
|
27
|
use utf8; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
26
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
211
|
use Exporter 5.57 'import'; |
|
6
|
|
|
|
|
193
|
|
|
6
|
|
|
|
|
314
|
|
7
|
6
|
|
|
6
|
|
6211
|
use Unicode::UCD qw(charscript); |
|
6
|
|
|
|
|
332930
|
|
|
6
|
|
|
|
|
790
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(sentence_iterator phrase_iterator presuf_iterator word_iterator extract_presuf extract_words tokenize_by_script); |
11
|
|
|
|
|
|
|
|
12
|
6
|
|
|
6
|
|
56
|
use List::Util qw(uniq pairmap); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
1877
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub exhaust { |
15
|
2
|
|
|
2
|
0
|
6
|
my ($iter, $cb) = @_; |
16
|
2
|
|
|
|
|
4
|
my @list; |
17
|
2
|
|
|
|
|
6
|
while(defined(my $x = $iter->())) { |
18
|
2
|
|
|
|
|
6
|
push @list, $x; |
19
|
2
|
50
|
|
|
|
20
|
$cb->($x) if defined($cb); |
20
|
|
|
|
|
|
|
} |
21
|
2
|
|
|
|
|
103
|
return @list; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub grep_iterator { |
25
|
5
|
|
|
5
|
0
|
109
|
my ($iter, $cb) = @_; |
26
|
|
|
|
|
|
|
return sub { |
27
|
40
|
|
|
40
|
|
77
|
local $_; |
28
|
40
|
|
|
|
|
56
|
do { |
29
|
45
|
|
|
|
|
96
|
$_ = $iter->(); |
30
|
45
|
100
|
|
|
|
142
|
return undef unless defined($_); |
31
|
|
|
|
|
|
|
} while (! $cb->()); |
32
|
35
|
|
|
|
|
120
|
return $_; |
33
|
|
|
|
|
|
|
} |
34
|
5
|
|
|
|
|
20
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub phrase_iterator { |
37
|
5
|
|
|
5
|
1
|
149
|
my ($input_iter, $opts) = @_; |
38
|
5
|
|
|
|
|
11
|
my @phrases; |
39
|
|
|
|
|
|
|
return sub { |
40
|
1429
|
|
100
|
1429
|
|
346097
|
while(! @phrases && defined(my $text = $input_iter->())) { |
41
|
|
|
|
|
|
|
@phrases = grep { |
42
|
713
|
100
|
100
|
|
|
14864
|
(! /\A\s+\z/) && (! /\p{General_Category=Punctuation}/) && /\p{Han}/ |
|
3110
|
|
|
|
|
18984
|
|
43
|
|
|
|
|
|
|
} split / ( \r?\n | \p{General_Category: Other_Punctuation} )+ /x, $text; |
44
|
|
|
|
|
|
|
} |
45
|
1429
|
|
|
|
|
3440
|
return shift @phrases; |
46
|
|
|
|
|
|
|
} |
47
|
5
|
|
|
|
|
35
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub sentence_iterator { |
50
|
3
|
|
|
3
|
1
|
130757
|
my ($input_iter, $opts) = @_; |
51
|
3
|
|
|
|
|
6
|
my @sentences; |
52
|
|
|
|
|
|
|
return sub { |
53
|
773
|
|
100
|
773
|
|
188053
|
while(! @sentences && defined(my $text = $input_iter->())) { |
54
|
692
|
|
|
|
|
12219
|
@sentences = grep { !/\A\s+\z/ } ($text =~ |
|
771
|
|
|
|
|
5536
|
|
55
|
|
|
|
|
|
|
m/( |
56
|
|
|
|
|
|
|
(?: |
57
|
|
|
|
|
|
|
[^\p{General_Category: Open_Punctuation}\p{General_Category: Close_Punctuation}]+? |
58
|
|
|
|
|
|
|
| .*? \p{General_Category: Open_Punctuation} .*? \p{General_Category: Close_Punctuation} .*? |
59
|
|
|
|
|
|
|
) |
60
|
|
|
|
|
|
|
(?: \z | [\n\?\!。?!]+ ) |
61
|
|
|
|
|
|
|
)/gx); |
62
|
|
|
|
|
|
|
} |
63
|
773
|
|
|
|
|
2950
|
return shift @sentences; |
64
|
|
|
|
|
|
|
} |
65
|
3
|
|
|
|
|
17
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub presuf_iterator { |
68
|
3
|
|
|
3
|
1
|
4656
|
my ($input_iter, $opts) = @_; |
69
|
|
|
|
|
|
|
|
70
|
3
|
|
|
|
|
5
|
my %stats; |
71
|
3
|
|
50
|
|
|
23
|
my $threshold = $opts->{threshold} || 9; # an arbitrary choice. |
72
|
3
|
|
50
|
|
|
21
|
my $lengths = $opts->{lengths} || [2,3]; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $phrase_iter = grep_iterator( |
75
|
|
|
|
|
|
|
phrase_iterator( $input_iter ), |
76
|
21
|
|
|
21
|
|
106
|
sub { /\A\p{Han}+\z/ } |
77
|
3
|
|
|
|
|
9
|
); |
78
|
|
|
|
|
|
|
|
79
|
3
|
|
|
|
|
7
|
my (%extracted, @extracted); |
80
|
|
|
|
|
|
|
return sub { |
81
|
5
|
50
|
|
5
|
|
22
|
if (@extracted) { |
82
|
0
|
|
|
|
|
0
|
return shift @extracted; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
5
|
|
100
|
|
|
16
|
while (!@extracted && defined(my $phrase = $phrase_iter->())) { |
86
|
21
|
|
|
|
|
42
|
for my $len ( @$lengths ) { |
87
|
42
|
|
|
|
|
83
|
my $re = '\p{Han}{' . $len . '}'; |
88
|
42
|
100
|
66
|
|
|
552
|
next unless length($phrase) >= $len * 2 && $phrase =~ /\A($re) .* ($re)\z/x; |
89
|
36
|
|
|
|
|
5722
|
my ($prefix, $suffix) = ($1, $2); |
90
|
36
|
50
|
|
|
|
129
|
$stats{prefix}{$prefix}++ unless $extracted{$prefix}; |
91
|
36
|
50
|
|
|
|
92
|
$stats{suffix}{$suffix}++ unless $extracted{$suffix}; |
92
|
|
|
|
|
|
|
|
93
|
36
|
|
|
|
|
74
|
for my $x ($prefix, $suffix) { |
94
|
72
|
100
|
66
|
|
|
425
|
if (! $extracted{$x} |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
95
|
|
|
|
|
|
|
&& $stats{prefix}{$x} |
96
|
|
|
|
|
|
|
&& $stats{suffix}{$x} |
97
|
|
|
|
|
|
|
&& $stats{prefix}{$x} > $threshold |
98
|
|
|
|
|
|
|
&& $stats{suffix}{$x} > $threshold |
99
|
|
|
|
|
|
|
) { |
100
|
2
|
|
|
|
|
6
|
$extracted{$x} = 1; |
101
|
2
|
|
|
|
|
5
|
delete $stats{prefix}{$x}; |
102
|
2
|
|
|
|
|
3
|
delete $stats{suffix}{$x}; |
103
|
|
|
|
|
|
|
|
104
|
2
|
|
|
|
|
7
|
push @extracted, $x; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
5
|
100
|
|
|
|
12
|
if (@extracted) { |
111
|
2
|
|
|
|
|
8
|
return shift @extracted; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
3
|
|
|
|
|
8
|
return undef; |
115
|
3
|
|
|
|
|
18
|
}; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub extract_presuf { |
119
|
1
|
|
|
1
|
1
|
6174
|
my ($input_iter, $opts) = @_; |
120
|
1
|
|
|
|
|
5
|
return [ exhaust(presuf_iterator($input_iter, $opts)) ]; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub word_iterator { |
124
|
1
|
|
|
1
|
1
|
3
|
my ($input_iter) = @_; |
125
|
|
|
|
|
|
|
|
126
|
1
|
|
|
|
|
3
|
my $threshold = 5; |
127
|
1
|
|
|
|
|
2
|
my (%lcontext, %rcontext, %word, @words); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $phrase_iter = grep_iterator( |
130
|
|
|
|
|
|
|
phrase_iterator( $input_iter ), |
131
|
9
|
|
|
9
|
|
51
|
sub { /\A\p{Han}+\z/ } |
132
|
1
|
|
|
|
|
3
|
); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
return sub { |
135
|
2
|
50
|
|
2
|
|
7
|
if (@words) { |
136
|
0
|
|
|
|
|
0
|
return shift @words; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
2
|
|
100
|
|
|
9
|
while (!@words && defined( my $txt = $phrase_iter->() )) { |
140
|
9
|
|
|
|
|
31
|
my @c = split("", $txt); |
141
|
|
|
|
|
|
|
|
142
|
9
|
|
|
|
|
24
|
for my $i (0..$#c) { |
143
|
67
|
100
|
|
|
|
116
|
if ($i > 0) { |
144
|
58
|
|
|
|
|
155
|
$lcontext{$c[$i]}{$c[$i-1]}++; |
145
|
58
|
|
|
|
|
88
|
for my $n (2,3) { |
146
|
116
|
100
|
|
|
|
204
|
if ($i >= $n) { |
147
|
89
|
|
|
|
|
195
|
my $tok = join('', @c[ ($i-$n+1) .. $i] ); |
148
|
89
|
100
|
|
|
|
164
|
unless ($word{$tok}) { |
149
|
88
|
50
|
|
|
|
163
|
if (length($tok) > 1) { |
150
|
88
|
|
|
|
|
205
|
$lcontext{ $tok }{$c[$i - $n]}++; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
88
|
50
|
66
|
|
|
114
|
if ($threshold <= (keys %{$lcontext{$tok}}) && $threshold <= (keys %{$rcontext{$tok}})) { |
|
88
|
|
|
|
|
249
|
|
|
1
|
|
|
|
|
5
|
|
154
|
0
|
|
|
|
|
0
|
$word{$tok} = 1; |
155
|
0
|
|
|
|
|
0
|
push @words, $tok; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
67
|
100
|
|
|
|
188
|
if ($i < $#c) { |
162
|
58
|
|
|
|
|
152
|
$rcontext{$c[$i]}{$c[$i+1]}++; |
163
|
58
|
|
|
|
|
90
|
for my $n (2,3) { |
164
|
116
|
100
|
|
|
|
221
|
if ($i + $n <= $#c) { |
165
|
89
|
|
|
|
|
193
|
my $tok = join('', @c[$i .. ($i+$n-1)]); |
166
|
89
|
50
|
|
|
|
163
|
unless ($word{$tok}) { |
167
|
89
|
50
|
|
|
|
178
|
if (length($tok) > 1) { |
168
|
89
|
|
|
|
|
262
|
$rcontext{ $tok }{ $c[$i+$n] }++; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
89
|
100
|
66
|
|
|
117
|
if ($threshold <= (keys %{$lcontext{$tok}}) && $threshold <= (keys %{$rcontext{$tok}})) { |
|
89
|
|
|
|
|
364
|
|
|
1
|
|
|
|
|
6
|
|
172
|
1
|
|
|
|
|
3
|
$word{$tok} = 1; |
173
|
1
|
|
|
|
|
4
|
push @words, $tok; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
2
|
|
|
|
|
11
|
return shift @words; |
182
|
|
|
|
|
|
|
} |
183
|
1
|
|
|
|
|
8
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub extract_words { |
186
|
1
|
|
|
1
|
1
|
97
|
return [ exhaust(word_iterator(@_)) ]; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub tokenize_by_script { |
190
|
1
|
|
|
1
|
1
|
94
|
my ($str) = @_; |
191
|
1
|
|
|
|
|
2
|
my @tokens; |
192
|
1
|
|
|
|
|
11
|
my @chars = grep { defined($_) } split "", $str; |
|
41
|
|
|
|
|
69
|
|
193
|
1
|
50
|
|
|
|
5
|
return () unless @chars; |
194
|
|
|
|
|
|
|
|
195
|
1
|
|
|
|
|
3
|
my $t = shift(@chars); |
196
|
1
|
|
|
|
|
10
|
my $s = charscript(ord($t)); |
197
|
1
|
|
|
|
|
15631
|
while(my $char = shift @chars) { |
198
|
40
|
|
|
|
|
101
|
my $_s = charscript(ord($char)); |
199
|
40
|
100
|
|
|
|
2734
|
if ($_s eq $s) { |
200
|
29
|
|
|
|
|
82
|
$t .= $char; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
else { |
203
|
11
|
|
|
|
|
21
|
push @tokens, $t; |
204
|
11
|
|
|
|
|
18
|
$s = $_s; |
205
|
11
|
|
|
|
|
26
|
$t = $char; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
} |
208
|
1
|
|
|
|
|
4
|
push @tokens, $t; |
209
|
1
|
|
|
|
|
5
|
return grep { ! /\A\s*\z/u } @tokens; |
|
12
|
|
|
|
|
69
|
|
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
1; |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
__END__ |