... | ... |
@@ -48,6 +48,33 @@ sub startup { |
48 | 48 |
|
49 | 49 |
$self->render(json => $data); |
50 | 50 |
}); |
51 |
+ |
|
52 |
+ $r->get('/api/word-count/:word')->to(cb => sub { |
|
53 |
+ my $self = shift; |
|
54 |
+ my $word = $self->param('word'); |
|
55 |
+ my $dbi = $self->app->dbi; |
|
56 |
+ |
|
57 |
+ my $word_count_h = {}; |
|
58 |
+ for (my $i = 0; $i < 66; $i++) { |
|
59 |
+ my $num = sprintf "%02d", $i + 1; |
|
60 |
+ |
|
61 |
+ my $content = $dbi->select( |
|
62 |
+ 'content_no_tag', |
|
63 |
+ table => 'book', |
|
64 |
+ where => {id => $num} |
|
65 |
+ )->value; |
|
66 |
+ my $content_length = length $content; |
|
67 |
+ my $word_q = quotemeta($word); |
|
68 |
+ $content =~ s/$word_q//g; |
|
69 |
+ my $content_length_no_word = length $content; |
|
70 |
+ |
|
71 |
+ # 文字の個数 |
|
72 |
+ my $word_count = ($content_length - $content_length_no_word) / length $word; |
|
73 |
+ $word_count_h->{$num} = $word_count; |
|
74 |
+ } |
|
75 |
+ |
|
76 |
+ $self->render(json => {word_count => $word_count_h}); |
|
77 |
+ }); |
|
51 | 78 |
} |
52 | 79 |
|
53 | 80 |
1; |