File Coverage

blib/lib/AI/Classifier/Japanese.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package AI::Classifier::Japanese;
2 5     5   104792 use 5.008005;
  5         19  
  5         239  
3 5     5   29 use strict;
  5         14  
  5         178  
4 5     5   31 use warnings;
  5         15  
  5         263  
5              
6             our $VERSION = "0.01";
7              
8 5     5   4783 use Mouse;
  5         174678  
  5         30  
9 5     5   16020 use Text::MeCab;
  0            
  0            
10             use Algorithm::NaiveBayes;
11              
12             my $nb = Algorithm::NaiveBayes->new;
13              
14             sub add_training_text {
15             my ($self, $text, $category) = @_;
16              
17             my $words_freq_ref = &_convert_text_to_bow($text);
18             $nb->add_instance(
19             attributes => $words_freq_ref,
20             label => $category
21             );
22             }
23              
24             sub train {
25             $nb->train;
26             }
27              
28             sub labels {
29             $nb->labels;
30             }
31              
32             sub predict {
33             my ($self, $text) = @_;
34              
35             my $words_freq_ref = &_convert_text_to_bow($text);
36             my $result_ref = $nb->predict(
37             attributes => $words_freq_ref
38             );
39             }
40              
41             sub _convert_text_to_bow {
42             my $text = shift;
43              
44             my $words_ref = &_parse_text($text);
45             my $words_freq_ref = {};
46             foreach (@$words_ref) {
47             $words_freq_ref->{$_}++;
48             }
49             return $words_freq_ref;
50             }
51              
52             sub _parse_text {
53             my $text = shift;
54              
55             my $mecab = Text::MeCab->new();
56             my $node = $mecab->parse($text);
57             my $words_ref = [];
58              
59             while ($node) {
60             if (&_is_keyword($node->posid)) {
61             push @$words_ref, $node->surface;
62             }
63             $node = $node->next;
64             }
65             return $words_ref;
66             }
67              
68             sub save_state {
69             my ($self, $path) = @_;
70             $nb->save_state($path);
71             }
72              
73             sub restore_state {
74             my ($self, $path) = @_;
75             $nb = Algorithm::NaiveBayes->restore_state($path);
76             }
77              
78             sub _is_keyword {
79             my $posid = shift;
80              
81             return &_is_noun($posid) || &_is_verb($posid) || &_is_adj($posid);
82             }
83              
84             # See: http://mecab.googlecode.com/svn/trunk/mecab/doc/posid.html
85             sub _is_interjection {
86             return $_[0] == 2;
87             }
88             sub _is_adj {
89             return 10 <= $_[0] && $_[0] < 13;
90             }
91             sub _is_aux {
92             return $_[0] == 25;
93             }
94             sub _is_conjunction {
95             return $_[0] == 26;
96             }
97             sub _is_particls {
98             return 27 <= $_[0] && $_[0] < 31;
99             }
100             sub _is_verb {
101             return 31 <= $_[0] && $_[0] < 34;
102             }
103             sub _is_noun {
104             return 36 <= $_[0] && $_[0] < 68;
105             }
106             sub _is_prenominal_adj {
107             return $_[0] == 68;
108             }
109              
110             __PACKAGE__->meta->make_immutable();
111              
112             1;
113             __END__