| 1 | package Lingua::Treebank::HeadFinder; | ||
| 2 | use strict; | ||
| 3 | use Carp; | ||
| 4 | use Lingua::Treebank; | ||
| 5 | ################################# | ||
| 6 | sub new { | ||
| 7 | my $class = shift; | ||
| 8 | my %args = @_; | ||
| 9 | |||
| 10 | my $self = bless \%args, $class; | ||
| 11 | |||
| 12 | croak "format not defined" unless defined $self->{format}; | ||
| 13 | |||
| 14 | if ($self->{format} eq 'roark') { | ||
| 15 | $self->read_roark_rules($self->{file}); | ||
| 16 | } | ||
| 17 | elsif ($self->{format} eq 'charniak') { | ||
| 18 | $self->read_charniak_rules($self->{file}); | ||
| 19 | } | ||
| 20 | |||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | ################################# | ||
| 24 | sub read_charniak_rules { | ||
| 25 | my $self = shift; | ||
| 26 | my $file = shift; | ||
| 27 | open my $fh, "<", $file | ||
| 28 | or die "Can't open charniak rules file '$file' for reading: $!\n"; | ||
| 29 | while (<$fh>) { | ||
| 30 | chomp; | ||
| 31 | next if /^\s*#/; | ||
| 32 | |||
| 33 | s/^(\S+)\s+//; | ||
| 34 | my $parent = $1; | ||
| 35 | if ($parent eq '*default*') { | ||
| 36 | $parent = 'DEFAULT'; | ||
| 37 | } | ||
| 38 | |||
| 39 | while (length $_) { | ||
| 40 | s/\( ([^()]+?) \)//x; | ||
| 41 | my $ruleset = $1; | ||
| 42 | |||
| 43 | my ($direction, @candidates) = split " ", $ruleset; | ||
| 44 | s/^\s+//; | ||
| 45 | s/\s+$//; | ||
| 46 | if ($direction eq 'r') { | ||
| 47 | $direction = 'right'; | ||
| 48 | } | ||
| 49 | elsif ($direction eq 'l') { | ||
| 50 | $direction = 'left'; | ||
| 51 | } | ||
| 52 | croak "line $. of $file direction is wrong\n" | ||
| 53 | unless $direction eq 'right' or $direction eq 'left'; | ||
| 54 | |||
| 55 | $self->add_rule(parent => $parent, | ||
| 56 | direction => $direction, | ||
| 57 | candidates => \@candidates, | ||
| 58 | text => $_); | ||
| 59 | } | ||
| 60 | } # loop over lines | ||
| 61 | close $fh | ||
| 62 | or die "can't close rules file '$file' after reading: $!\n"; | ||
| 63 | } | ||
| 64 | ################################# | ||
| 65 | sub read_roark_rules { | ||
| 66 | my ($self) = shift; | ||
| 67 | my ($file) = @_; | ||
| 68 | |||
| 69 | open (my $fh, "<", $file) | ||
| 70 | or die "can't open rules file '$file' for reading: $!\n"; | ||
| 71 | |||
| 72 | while (<$fh>) { | ||
| 73 | chomp; | ||
| 74 | |||
| 75 | # comment lines begin with a sharp | ||
| 76 | next if /^\s*#/; | ||
| 77 | |||
| 78 | # lines look like: NP right NP NN NT | ||
| 79 | |||
| 80 | # and a second line with the same target will be used only if | ||
| 81 | # the first line fails, so fallback choices can be encoded, | ||
| 82 | # e.g.: | ||
| 83 | |||
| 84 | # NP right AJ AAJ | ||
| 85 | |||
| 86 | # also there can be a DEFAULT right NP NN NT -- used last | ||
| 87 | |||
| 88 | my ($parent, $direction, @candidates) = split; | ||
| 89 | |||
| 90 | $direction = lc $direction; # don't care about caps | ||
| 91 | |||
| 92 | if ($direction eq 'r') { | ||
| 93 | $direction = 'right'; | ||
| 94 | } | ||
| 95 | elsif ($direction eq 'l') { | ||
| 96 | $direction = 'left'; | ||
| 97 | } | ||
| 98 | |||
| 99 | if ($parent eq '*default*') { | ||
| 100 | $parent = 'DEFAULT'; | ||
| 101 | } | ||
| 102 | |||
| 103 | croak "line $. of $file direction is wrong\n" | ||
| 104 | unless $direction eq 'right' or $direction eq 'left'; | ||
| 105 | |||
| 106 | $self->add_rule(parent => $parent, | ||
| 107 | direction => $direction, | ||
| 108 | candidates => \@candidates, | ||
| 109 | text => $_); | ||
| 110 | |||
| 111 | } | ||
| 112 | close $fh | ||
| 113 | or die "can't close rules file '$file' after reading: $!\n"; | ||
| 114 | } | ||
| 115 | ################################# | ||
| 116 | sub add_rule { | ||
| 117 | my $self = shift; | ||
| 118 | my %args = @_; | ||
| 119 | |||
| 120 | my %cands = map { $_ => 1 } @{$args{candidates}}; | ||
| 121 | |||
| 122 | my $rule = [ $args{direction}, | ||
| 123 | \%cands, | ||
| 124 | $args{text}, | ||
| 125 | ]; | ||
| 126 | push @{$self->{$args{parent}}}, $rule; | ||
| 127 | } | ||
| 128 | ################################# | ||
| 129 | sub select_head { | ||
| 130 | my $self = shift; | ||
| 131 | my Lingua::Treebank::Const $tree = shift; | ||
| 132 | my $tag = $tree->tag(); | ||
| 133 | my @children = @{$tree->children()}; | ||
| 134 | |||
| 135 | RULE: | ||
| 136 | for my $rule (@{$self->{$tag}}, @{$self->{DEFAULT}}) { | ||
| 137 | my @search; | ||
| 138 | if ($rule->[0] eq 'left' ) { | ||
| 139 | @search = @children ; | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | @search = reverse @children; | ||
| 143 | } | ||
| 144 | for my $tok (@search) { | ||
| 145 | if (not keys %{$rule->[1]}) { | ||
| 146 | # rule is empty but has direction; match anything so | ||
| 147 | # pick the first one | ||
| 148 | return $tok; | ||
| 149 | } | ||
| 150 | my $child_tag = $tok->tag(); | ||
| 151 | if ($rule->[1]{$child_tag}) { | ||
| 152 | # carp "selecting $child_tag as head of $tag"; | ||
| 153 | return $tok; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | # carp "rejecting rule $rule->[2]"; | ||
| 157 | } | ||
| 158 | carp "no rule found for $tag"; | ||
| 159 | } | ||
| 160 | ################################# | ||
| 161 | sub annotate_heads { | ||
| 162 | # recursively annotate a tree | ||
| 163 | my $self = shift; | ||
| 164 | my Lingua::Treebank::Const $tree = shift; | ||
| 165 | |||
| 166 | return if $tree->is_terminal(); | ||
| 167 | |||
| 168 | my $headchild = $self->select_head($tree); | ||
| 169 | $tree->headchild($headchild); | ||
| 170 | |||
| 171 | for my $branch (@{$tree->children()}) { | ||
| 172 | $self->annotate_heads($branch); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | ################################# | ||
| 176 | |||
| 177 | 1; | ||
| 178 | |||
| 179 | __END__ |