| 1 | package Treex::Block::W2A::EN::TagLinguaEn; | ||||||||||||||||||||
| 2 | BEGIN { | ||||||||||||||||||||
| 3 | 2 | ▶ | $Treex::Block::W2A::EN::TagLinguaEn::VERSION = '0.06903_1'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | 2 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use feature qw(switch); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Treex::Core::Common; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | extends 'Treex::Core::Block'; | |||||||||||||||||||
| 10 | 2 | ▶ | use Lingua::EN::Tagger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | has _tagger => ( | |||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||
| 13 | isa => 'Lingua::EN::Tagger', | ||||||||||||||||||||
| 14 | builder => '_build_tagger', | ||||||||||||||||||||
| 15 | lazy => 1, | ||||||||||||||||||||
| 16 | init_arg => undef, | ||||||||||||||||||||
| 17 | predicate => '_tagger_built', | ||||||||||||||||||||
| 18 | documentation => q{Tagger object}, | ||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | has _form_corrections => ( | ||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||
| 23 | isa => 'HashRef[Str]', | ||||||||||||||||||||
| 24 | default => sub { | ||||||||||||||||||||
| 25 | { | ||||||||||||||||||||
| 26 | 1 | ▶ | q(``) => q("), | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | q('') => q("), | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||
| 30 | 2 | documentation => q{Possible changes in forms done by tagger}, | |||||||||||||||||||
| 31 | ); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub _build_tagger { | ||||||||||||||||||||
| 34 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_build_tagger: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 1 | my $tagger = Lingua::EN::Tagger->new(); | |||||||||||||||||||
| 36 | 1 | return $tagger; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub _revert_form { #because Lingua::EN::Tagger changes some forms to another, we need to restore original | ||||||||||||||||||||
| 40 | 48 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_revert_form: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 41 | 48 | my %args = @_; | |||||||||||||||||||
| 42 | 48 | my $new = $args{new}; | |||||||||||||||||||
| 43 | 48 | ▶ | return $self->_form_corrections->{$new} // $new; | ||||||||||||||||||
|
Condition66%2/3
$self->_form_corrections->{$new} // $new
MC/DC0%0/2
$self->_form_corrections->{$new} // $new
$self->_form_corrections->{$new}
$new
Truth table66%2/3
A // B
A= $self->_form_corrections->{$new}B= $new
| |||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # using new switch syntax | ||||||||||||||||||||
| 48 | sub _correct_lingua_tag { # substitution according to http://search.cpan.org/src/ACOBURN/Lingua-EN-Tagger-0.13/README | ||||||||||||||||||||
| 49 | # puvodni tagset je na http://www.computing.dcu.ie/~acahill/tagset.html | ||||||||||||||||||||
| 50 | 48 | ▶ | my ( $self, $linguatag, $wordform ) = @_; | ||||||||||||||||||
|
Subroutine
_correct_lingua_tag: called CC=1 SCAR=2.0
| |||||||||||||||||||||
| 51 | 48 | given ($linguatag) { | |||||||||||||||||||
| 52 | 48 | when ('DET') { | |||||||||||||||||||
| 53 | 0 | return 'DT'; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | 48 | when ('PRPS') { | |||||||||||||||||||
| 56 | 0 | return 'PRP$'; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | 48 | when (/^[LR]RB$/) { | |||||||||||||||||||
| 59 | 0 | return "-$linguatag-"; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 48 | when (/^PP/) { # allowed "tags" of punctuation mark in PennTB: # $ '' ( ) , . : `` | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | given ($wordform) { | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | when (/^(?:#|$|''|,|\.|:|``)$/) { | |||||||||||||||||||
| 66 | 0 | return $wordform; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | 0 | when (/[\(\[\{]/) { | |||||||||||||||||||
| 69 | 0 | return "-LRB-"; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | 0 | when (/[\)\]\}]/) { | |||||||||||||||||||
| 72 | 0 | return "-RRB-"; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | 0 | default { | |||||||||||||||||||
| 75 | 0 | return "."; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | 48 | default { | |||||||||||||||||||
| 81 | 48 | return $linguatag; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # v lingua-taggeru maji pro punktuaci zvlastni tagy, to ale v ptb nebylo! | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # when ("LRB") {return "."} # hack, zavorky totiz collins nesezere - tyhle vsechny zameny by spis mely bejt ve wrapperu ke collinsu | ||||||||||||||||||||
| 87 | # when ("RRB") {return "."} | ||||||||||||||||||||
| 88 | # when ("PP" ) {return "."} | ||||||||||||||||||||
| 89 | # when ("PPL") {return "``"} | ||||||||||||||||||||
| 90 | # when ("PPR") {return "''"} | ||||||||||||||||||||
| 91 | # when ("PPC") {return ","} | ||||||||||||||||||||
| 92 | # when ("PPS") {return ","} #POZOR, cunarna, tagger dava PPS pomlcce a Collins na tom pak pada, ale nevim, jaky tag teda patri | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | sub process_zone { | ||||||||||||||||||||
| 98 | 2 | ▶ | my ( $self, $zone ) = @_; | ||||||||||||||||||
|
Subroutine
process_zone: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | # get the source sentence | ||||||||||||||||||||
| 101 | 2 | my $sentence = $zone->sentence; | |||||||||||||||||||
| 102 | 2 | ▶ | log_fatal("No sentence in zone") if !defined $sentence; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 2 | ▶ | log_fatal(qq{There's already atree in zone}) if $zone->has_atree(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 2 | log_debug("Processing sentence: $sentence"); | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | #split on whitespace, tags nor tokens doesn't contain spaces | ||||||||||||||||||||
| 107 | 2 | my @tagged = split /\s+/, $self->_tagger->add_tags($sentence); | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | # create a-tree | ||||||||||||||||||||
| 110 | 2 | my $a_root = $zone->create_atree(); | |||||||||||||||||||
| 111 | 2 | my $tag_regex = qr{ | |||||||||||||||||||
| 112 | <(\w+)> #<tag> | ||||||||||||||||||||
| 113 | ([^<]+) #form | ||||||||||||||||||||
| 114 | </\1> #</tag> | ||||||||||||||||||||
| 115 | }x; | ||||||||||||||||||||
| 116 | 2 | my $space_start = qr{^\s+}; | |||||||||||||||||||
| 117 | 2 | my $ord = 1; | |||||||||||||||||||
| 118 | 2 | foreach my $tag_pair (@tagged) { | |||||||||||||||||||
| 119 | 48 | ▶ | if ( $tag_pair =~ $tag_regex ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 48 | my $form = $self->_revert_form( new => $2 ); | |||||||||||||||||||
| 121 | 48 | my $tag = $self->_correct_lingua_tag( $1, $form ); | |||||||||||||||||||
| 122 | 48 | ▶ | if ( $sentence =~ s/^\Q$form\E// ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | #check if there is space after word | ||||||||||||||||||||
| 125 | 48 | ▶ | my $no_space_after = $sentence =~ m/$space_start/ ? 0 : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 48 | ▶ | if ( $sentence eq q{} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 1 | $no_space_after = 0; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | #delete it | ||||||||||||||||||||
| 131 | 48 | $sentence =~ s{$space_start}{}; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # and create node under root | ||||||||||||||||||||
| 134 | 48 | $a_root->create_child( | |||||||||||||||||||
| 135 | form => $form, | ||||||||||||||||||||
| 136 | tag => $tag, | ||||||||||||||||||||
| 137 | no_space_after => $no_space_after, | ||||||||||||||||||||
| 138 | ord => $ord++, | ||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||
| 142 | 0 | log_fatal("Mismatch between tagged word and original sentence: Tagged: $form. Original: $sentence."); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||
| 146 | 0 | log_fatal("Incorrect output format from Lingua::EN::Tagger: $tag_pair"); | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 2 | return 1; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 2 | 1; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =pod | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =encoding utf-8 | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head1 NAME | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | Treex::Block::W2A::EN::TagLinguaEn | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head1 VERSION | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head1 DESCRIPTION | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Each node in analytical tree is tagged using C<Lingua::EN::Tagger> (Penn Treebank POS tags). | ||||||||||||||||||||
| 170 | Because Lingua::EN::Tagger does its own tokenization, it checks if tokenization is same. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head1 AUTHORS | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | Tomáš Kraut <kraut@ufal.mff.cuni.cz> | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | |||||||||||||||||||||