| 1 | package Treex::Block::W2A::JA::TagMeCab; | ||||||||||||||||||||||||||||
| 2 | 2 | $Treex::Block::W2A::JA::TagMeCab::VERSION = '0.13095'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Treex::Tool::Tagger::MeCab; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | extends 'Treex::Core::Block'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | has _form_corrections => ( | ||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => 'HashRef[Str]', | ||||||||||||||||||||||||||||
| 16 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||
| 18 | 1 | q(``) => q("), | |||||||||||||||||||||||||||
| 19 | q('') => q("), | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||
| 22 | 2 | documentation => q{Possible changes in forms done by tagger}, | |||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | has tagger => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | return; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub process_start { | ||||||||||||||||||||||||||
|
Subroutine
process_start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | my $tagger = Treex::Tool::Tagger::MeCab->new(); | |||||||||||||||||||||||||||
| 37 | 1 | $self->set_tagger( $tagger ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub process_zone { | ||||||||||||||||||||||||||
|
Subroutine
process_zone: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my ( $self, $zone ) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # get the source sentence | ||||||||||||||||||||||||||||
| 48 | 0 | my $sentence = $zone->sentence; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | log_fatal("No sentence in zone") if !defined $sentence; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | log_fatal(qq{There's already atree in zone}) if $zone->has_atree(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | log_debug("Processing sentence: $sentence"); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $result = ""; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my $debug = ""; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my ( @tokens ) = $self->tagger->process_sentence( $sentence ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # modifies the output format of MeCab wrapper | ||||||||||||||||||||||||||||
| 60 | 0 | foreach my $token ( @tokens ) { | |||||||||||||||||||||||||||
| 61 | 0 | my @features = split /\t/, $token; | |||||||||||||||||||||||||||
| 62 | 0 | my $wordform = $features[0]; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $bTag = $features[1].'-'.$features[2].'-'.$features[3].'-'.$features[4]; | |||||||||||||||||||||||||||
| 65 | 0 | my $lemma = $features[7]; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($bTag !~ "BOS" && $bTag !~ "空白") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$bTag =~ /BOS/ or $bTag =~ /\x{7a7a}\x{767d}/
MC/DC0%0/2
$bTag =~ /BOS/ or $bTag =~ /\x{7a7a}\x{767d}/
$bTag =~ /BOS/
$bTag =~ /\x{7a7a}\x{767d}/
Truth table0%0/3
A or B
A= $bTag =~ /BOS/B= $bTag =~ /\x{7a7a}\x{767d}/
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $lemma = $wordform if $lemma =~ m/\*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $bTag =~ s{^(.+)$}{<$1>}; | |||||||||||||||||||||||||||
| 71 | 0 | my $eTag = $bTag; | |||||||||||||||||||||||||||
| 72 | 0 | $eTag =~ s{<}{</}; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | $result .= ' '.$bTag.$wordform.$eTag.$lemma; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | $result =~ s{^\s+}{}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # split on whitespace, tags nor tokens doesn't contain spaces | ||||||||||||||||||||||||||||
| 81 | 0 | my @tagged = split /\s+/, $result; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # create a-tree | ||||||||||||||||||||||||||||
| 84 | 0 | my $a_root = $zone->create_atree(); | |||||||||||||||||||||||||||
| 85 | 0 | my $tag_regex = qr{ | |||||||||||||||||||||||||||
| 86 | <([^\-]+\-[^\-]+\-[^\-]+\-[^\-]+)> #<tag> | ||||||||||||||||||||||||||||
| 87 | ([^<]+) #form | ||||||||||||||||||||||||||||
| 88 | </\1> #</tag> | ||||||||||||||||||||||||||||
| 89 | (.+) #lemma | ||||||||||||||||||||||||||||
| 90 | }x; | ||||||||||||||||||||||||||||
| 91 | 0 | my $space_start = qr{^\s+}; | |||||||||||||||||||||||||||
| 92 | 0 | my $ord = 1; | |||||||||||||||||||||||||||
| 93 | 0 | foreach my $tag_pair (@tagged) { | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $tag_pair =~ $tag_regex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $form = $2; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $tag = $1; | |||||||||||||||||||||||||||
| 98 | 0 | $tag =~ s{^<(\w+)>.$}{$1}; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $lemma = $3; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $debug .= "$form $tag; "; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ( $sentence =~ s/^\Q$form\E// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # check if there is space after word | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $no_space_after = $sentence =~ m/$space_start/ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( $sentence eq q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $no_space_after = 0; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # delete it | ||||||||||||||||||||||||||||
| 113 | 0 | $sentence =~ s{$space_start}{}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # and create node under root | ||||||||||||||||||||||||||||
| 116 | 0 | $a_root->create_child( | |||||||||||||||||||||||||||
| 117 | form => $form, | ||||||||||||||||||||||||||||
| 118 | tag => $tag, | ||||||||||||||||||||||||||||
| 119 | lemma => $lemma, | ||||||||||||||||||||||||||||
| 120 | no_space_after => $no_space_after, | ||||||||||||||||||||||||||||
| 121 | ord => $ord++, | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | 0 | log_fatal("Mismatch between tagged word and original sentence: Tagged: $form; $debug. Original: $sentence."); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||
| 129 | 0 | log_fatal("Incorrect output format from MeCab: $tag_pair debug: $debug"); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | return 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||