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 9     9   117884 use parent "App::cdif::Command";
  9         319  
  9         87  
4              
5 9     9   697 use v5.14;
  9         35  
6 9     9   52 use warnings;
  9         22  
  9         613  
7 9     9   55 use utf8;
  9         17  
  9         69  
8 9     9   250 use Carp;
  9         18  
  9         606  
9 9     9   51 use Data::Dumper;
  9         15  
  9         7768  
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;