File Coverage

blib/lib/App/cdif/Command/Mecab.pm
Criterion Covered Total %
statement 17 46 36.9
branch 0 8 0.0
condition n/a
subroutine 6 10 60.0
pod 0 2 0.0
total 23 66 34.8


line stmt bran cond sub pod time code
1             package App::cdif::Command::Mecab;
2              
3 10     10   100750 use parent "App::cdif::Command";
  10         268  
  10         68  
4              
5 10     10   467 use v5.14;
  10         28  
6 10     10   37 use warnings;
  10         12  
  10         464  
7 10     10   47 use utf8;
  10         15  
  10         49  
8 10     10   189 use Carp;
  10         19  
  10         481  
9 10     10   41 use Data::Dumper;
  10         12  
  10         5836  
10              
11             our $debug;
12              
13             sub wordlist {
14 0     0 0   my $obj = shift;
15 0           my $text = shift;
16              
17             ##
18             ## mecab ignores trailing spaces.
19             ##
20             my $removeme = sub {
21 0     0     local *_ = shift;
22 0 0         return sub { 0 } unless /[ \t]+$/m;
  0            
23 0           my $magic = "15570"."67583";
24 0           $magic++ while /$magic/;
25 0           s/[ \t]+\K$/$magic/mg;
26 0           sub { $_ eq $magic };
  0            
27 0           }->(\$text);
28              
29 0           my $eos = "EOS" . "000";
30 0           $eos++ while $text =~ /$eos/;
31 0     0     my $is_newline = sub { $_ eq $eos };
  0            
32              
33 0           my @mecab = ('mecab', '--node-format', '%M\\n', '--eos-format', "$eos\\n");
34 0           my $result = $obj->command(@mecab)->setstdin($text)->update->data;
35 0 0         warn $result =~ s/^/MECAB: /mgr if $debug;
36 0           do {
37 0 0         map { $is_newline->() ? "\n" : $_ }
38 0           grep { not $removeme->() }
39 0 0         map { /\A\w/ ? $_ : uniqchar($_) }
40 0           grep { length }
  0            
41             $result =~ /^([^\w\n]*+)(.*)\n/mg;
42             };
43             }
44              
45             sub uniqchar {
46 0     0 0   my @s;
47 0           for (@_) {
48 0           while (/(\X)\g{-1}*/pg) {
49 0           push @s, ${^MATCH};
50             }
51             }
52 0           @s;
53             }
54              
55             1;