line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Classifier::NaiveBayes::Tokenizer; |
2
|
1
|
|
|
1
|
|
22056
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use Lingua::Stem::Snowball; |
4
|
|
|
|
|
|
|
use 5.008008; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has 'stemmer' => ( |
7
|
|
|
|
|
|
|
is => 'rw', |
8
|
|
|
|
|
|
|
lazy_build => 1, |
9
|
|
|
|
|
|
|
handles => ['stem_in_place']); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'stemming' => ( |
12
|
|
|
|
|
|
|
is => 'rw',); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has lang => ( |
15
|
|
|
|
|
|
|
is => 'rw', |
16
|
|
|
|
|
|
|
default => sub { 'en' }); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _build_stemmer { return Lingua::Stem::Snowball->new(lang => $_[0]->lang) } |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub words { |
21
|
|
|
|
|
|
|
my ($self, $string, $token_callback) = @_; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my @words = map { lc $_ } $string =~ /(\w+(?:[-']\w+)*)/g; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my %ignore_words = map { $_ => 1 } $self->ignore_words; |
26
|
|
|
|
|
|
|
@words = grep { !$ignore_words{$_} } @words; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$self->stemmer->stem_in_place(\@words) if $self->stemming; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
if ( $token_callback && ref $token_callback eq 'CODE' ) { |
31
|
|
|
|
|
|
|
@words = map { &{$token_callback}($_) } @words; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return \@words; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub ignore_words { |
38
|
|
|
|
|
|
|
return ( |
39
|
|
|
|
|
|
|
'a', 'about', 'above', 'across', 'after', 'afterwards', |
40
|
|
|
|
|
|
|
'again', 'against', 'all', 'almost', 'alone', 'along', |
41
|
|
|
|
|
|
|
'already', 'also', 'although', 'always', 'am', 'among', |
42
|
|
|
|
|
|
|
'amongst', 'amoungst', 'amount', 'an', 'and', 'another', |
43
|
|
|
|
|
|
|
'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', |
44
|
|
|
|
|
|
|
'are', 'around', 'as', 'at', 'back', 'be', |
45
|
|
|
|
|
|
|
'became', 'because', 'become', 'becomes', 'becoming', 'been', |
46
|
|
|
|
|
|
|
'before', 'beforehand', 'behind', 'being', 'below', 'beside', |
47
|
|
|
|
|
|
|
'besides', 'between', 'beyond', 'bill', 'both', 'bottom', |
48
|
|
|
|
|
|
|
'but', 'by', 'call', 'can', 'cannot', 'cant', 'dont', |
49
|
|
|
|
|
|
|
'co', 'computer', 'con', 'could', 'couldnt', 'cry', |
50
|
|
|
|
|
|
|
'de', 'describe', 'detail', 'do', 'done', 'down', |
51
|
|
|
|
|
|
|
'due', 'during', 'each', 'eg', 'eight', 'either', |
52
|
|
|
|
|
|
|
'eleven', 'else', 'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', 'every', |
53
|
|
|
|
|
|
|
'everyone', 'everything', 'everywhere', 'except', 'few', 'fifteen', |
54
|
|
|
|
|
|
|
'fify', 'fill', 'find', 'fire', 'first', 'five', |
55
|
|
|
|
|
|
|
'for', 'former', 'formerly', 'forty', 'found', 'four', |
56
|
|
|
|
|
|
|
'from', 'front', 'full', 'further', 'get', 'give', |
57
|
|
|
|
|
|
|
'go', 'had', 'has', 'hasnt', 'have', 'he', |
58
|
|
|
|
|
|
|
'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', |
59
|
|
|
|
|
|
|
'hereupon', 'hers', 'herself', 'him', 'himself', 'his', |
60
|
|
|
|
|
|
|
'how', 'however', 'hundred', 'i', 'ie', 'if', |
61
|
|
|
|
|
|
|
'in', 'inc', 'indeed', 'interest', 'into', 'is', |
62
|
|
|
|
|
|
|
'it', 'its', 'itself', 'keep', 'last', 'latter', |
63
|
|
|
|
|
|
|
'latterly', 'least', 'less', 'ltd', 'made', 'many', |
64
|
|
|
|
|
|
|
'may', 'me', 'meanwhile', 'might', 'mill', 'mine', |
65
|
|
|
|
|
|
|
'more', 'moreover', 'most', 'mostly', 'move', 'much', |
66
|
|
|
|
|
|
|
'must', 'my', 'myself', 'name', 'namely', 'neither', |
67
|
|
|
|
|
|
|
'never', 'nevertheless', 'next', 'nine', 'no', 'nobody', |
68
|
|
|
|
|
|
|
'none', 'noone', 'nor', 'not', 'nothing', 'now', |
69
|
|
|
|
|
|
|
'nowhere', 'of', 'off', 'often', 'on', 'once', |
70
|
|
|
|
|
|
|
'one', 'only', 'onto', 'or', 'other', 'others', |
71
|
|
|
|
|
|
|
'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', |
72
|
|
|
|
|
|
|
'own', 'part', 'per', 'perhaps', 'please', 'put', |
73
|
|
|
|
|
|
|
'rather', 're', 'same', 'see', 'seem', 'seemed', |
74
|
|
|
|
|
|
|
'seeming', 'seems', 'serious', 'several', 'she', 'should', |
75
|
|
|
|
|
|
|
'show', 'side', 'since', 'sincere', 'six', 'sixty', |
76
|
|
|
|
|
|
|
'so', 'some', 'somehow', 'someone', 'something', 'sometime', |
77
|
|
|
|
|
|
|
'sometimes', 'somewhere', 'still', 'such', 'system', 'take', |
78
|
|
|
|
|
|
|
'ten', 'than', 'that', 'the', 'their', 'them', |
79
|
|
|
|
|
|
|
'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', |
80
|
|
|
|
|
|
|
'therefore', 'therein', 'thereupon', 'these', 'they', 'thick', |
81
|
|
|
|
|
|
|
'thin', 'third', 'this', 'those', 'though', 'three', |
82
|
|
|
|
|
|
|
'through', 'throughout', 'thru', 'thus', 'to', 'together', |
83
|
|
|
|
|
|
|
'too', 'top', 'toward', 'towards', 'twelve', 'twenty', |
84
|
|
|
|
|
|
|
'two', 'un', 'under', 'until', 'up', 'upon', |
85
|
|
|
|
|
|
|
'us', 'very', 'via', 'was', 'we', 'well', |
86
|
|
|
|
|
|
|
'were', 'what', 'whatever', 'when', 'whence', 'whenever', |
87
|
|
|
|
|
|
|
'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', |
88
|
|
|
|
|
|
|
'wherever', 'whether', 'which', 'while', 'whither', 'who', |
89
|
|
|
|
|
|
|
'whoever', 'whole', 'whom', 'whose', 'why', 'will', |
90
|
|
|
|
|
|
|
'with', 'within', 'without', 'would', 'yet', 'you', 'your', 'yours', |
91
|
|
|
|
|
|
|
'yourself', 'yourselves' |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
=head1 NAME |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Data::Classifier::NaiveBayes |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $tokenizer = Data::Classifier::NaiveBayes::Tokenizer->new; |
103
|
|
|
|
|
|
|
say @{$tokenizer->words("Hello World")}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 DESCRIPTION |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L<Data::Classifier::NaiveBayes> |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 METHODS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SEE ALSO |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
L<Moose>, L<Lingua::Stem::Snowball> |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 AUTHOR |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Logan Bell, C<< <logie@cpan.org> >> |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Copyright 2012, Logan Bell |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
124
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |