| 1 | package Treex::Tool::Tagger::MeCab; | ||||||||||
| 2 | 4 | $Treex::Tool::Tagger::MeCab::VERSION = '0.13095'; | |||||||||
| 3 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Treex::Core::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Treex::Core::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Treex::Tool::ProcessUtils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use Treex::Core::Resource; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 13 | 2 | my ($self) = @_; | |||||||||
| 14 | |||||||||||
| 15 | # TODO find architecture independent solution | ||||||||||
| 16 | 2 | my $bin_path = require_file_from_share( | |||||||||
| 17 | 'installed_tools/tagger/MeCab/bin/mecab', | ||||||||||
| 18 | ref($self) | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 2 | my $cmd = "$bin_path".' 2>/dev/null'; | |||||||||
| 22 | |||||||||||
| 23 | # start MeCab tagger | ||||||||||
| 24 | 2 | my ( $reader, $writer, $pid ) = Treex::Tool::ProcessUtils::bipipe( $cmd, ':encoding(utf-8)' ); | |||||||||
| 25 | |||||||||||
| 26 | 2 | $self->{reader} = $reader; | |||||||||
| 27 | 2 | $self->{writer} = $writer; | |||||||||
| 28 | 2 | $self->{pid} = $pid; | |||||||||
| 29 | |||||||||||
| 30 | 2 | return; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | sub process_sentence { | ||||||||
|
Subroutine
process_sentence: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||
| 34 | 1 | my ( $self, $sentence ) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 1 | my @tokens; | |||||||||
| 37 | 1 | my $writer = $self->{writer}; | |||||||||
| 38 | 1 | my $reader = $self->{reader}; | |||||||||
| 39 | |||||||||||
| 40 | 1 | print $writer $sentence."\n"; | |||||||||
| 41 | |||||||||||
| 42 | 1 | my $line = <$reader>; | |||||||||
| 43 | |||||||||||
| 44 | # we store each line, which consists of wordform+features into @tokens as a string where each feature/wordform is separated by '\t' | ||||||||||
| 45 | # other block should edit this output as needed | ||||||||||
| 46 | # EOS marks end of sentence | ||||||||||
| 47 | 1 | ▶ | while ( $line !~ "EOS" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | log_fatal("Unitialized line (perhaps MeCab was not initialized correctly).") if (!defined $line); # even with empty string input we should get at least "EOS" line in output, otherwise the tagger wasn't correctly initialized | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | # we don't want to substitute actual commas in the sentence | ||||||||||
| 52 | 0 | $line =~ s{^(.*),\t}{$1#comma\t}; | |||||||||
| 53 | |||||||||||
| 54 | 0 | $line =~ s{(.),}{$1\t}g; | |||||||||
| 55 | |||||||||||
| 56 | 0 | $line =~ s{#comma}{,}; | |||||||||
| 57 | |||||||||||
| 58 | 0 | push @tokens, $line; | |||||||||
| 59 | 0 | $line = <$reader>; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 0 | return @tokens; | |||||||||
| 63 | |||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | # ----------------- cleaning up ------------------ | ||||||||||
| 67 | # # TODO : cleanup | ||||||||||
| 68 | |||||||||||
| 69 | 4 | 1; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||