blib/lib/Lingua/FeatureMatrix/Implicature.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 158 CC 19 SCAR 39.5
CC
19
Cov
0%
apply
110.0
matches
56.0
dependsOn
12.0
CRAP 51.9
1 package Lingua::FeatureMatrix::Implicature;
2
3 use 5.006;
4 use strict;
5 use warnings;
6 use Carp;
7 our $VERSION = '0.01';
8
9 use Class::MethodMaker
10 new_with_init => 'new',
11
12 # use get_set so we can initialize to undef without warning
13 get_set => [ qw [ implier implicant ] ];
14
15 # hash => [ qw [ implier implicant ] ];
16 use Lingua::FeatureMatrix::Eme;
17 ##################################################################
18 sub init {
19 my $self = shift;
20 my %implier = %{shift @_};
21 my %implicant = %{shift @_};
22
23 {
24 # these two calls *should* allow undef to be a value...
25 $self->implier(\%implier);
26 $self->implicant(\%implicant);
27 }
28
29 # foreach my $key ($self->implier_keys) {
30 # if (not defined $self->implier($key)) {
31 # warn "shouldn't specify * for features in implier\n"
32
33 }
34 ##################################################################
35 sub matches {
36 my $self = shift;
37 my Lingua::FeatureMatrix::Eme $eme = shift;
38 # returns whether the eme passed in by argument matches the implier
39
40 FEATURE:
41 foreach my $feature ( keys %{$self->implier()} ) {
42 if (not $eme->can($feature)) {
43 warn ref($eme) . " doesn't know about feature $feature\n";
44 next FEATURE;
45 }
46 if (not defined $self->implier()->{$feature}) {
47 if (not defined $eme->$feature) {
48 next FEATURE; # we're okay -- both underspecified
49 }
50 else {
51 return 0; # implier requires "unspecified", eme has specified
52 # it
53 }
54 }
55 if (defined $self->implier()->{$feature} and not defined $eme->$feature) {
56 return 0; # implier requires, eme has explicitly unspecified
57 }
58 if ($eme->$feature eq 'unset') {
59 return 0; # doesn't match if not yet set. Is this the right
60 # behavior?
61 }
62 if ($self->implier()->{$feature} != $eme->$feature) {
63 # both defined, but different signs
64 return 0;
65 }
66 }
67 return 1;
68 }
69 ##################################################################
70 sub dependsOn {
71 # check to see if $other's implicant shares any features with
72 # $self's implier. e.g.
73
74 # other [ +high ] => [ +vow ]
75 # self [ +vow ] => [ +voice ]
76
77 # since [vow] is in other's implicant, and in self's implier, self
78 # *does* depend on other
79
80 my $self = shift;
81 my Lingua::FeatureMatrix::Implicature $other = shift;
82
83 my @dependencies;
84 foreach my $feature( keys %{$self->implier} ) {
85 if (defined $other->implicant->{$feature}) {
86 my $action;
87 if ($other->implicant->{$feature} == $self->implier->{$feature}) {
88 $action = 'feeds';
89 }
90 else {
91 $action = 'bleeds';
92 }
93 # should this test "exists"?
94 push @dependencies, "$action ($feature)";
95 }
96 }
97 return @dependencies;
98 }
99 ##################################################################
100 sub dumpToText {
101 my $self = shift;
102
103 my (@implier, @implicant);
104 foreach (sort keys %{$self->implier()}) {
105 push @implier,
106 Lingua::FeatureMatrix::Eme->_dumpFeature($_,
107 $self->implier->{$_});
108 }
109 foreach (sort keys %{$self->implicant()}) {
110 push @implicant,
111 Lingua::FeatureMatrix::Eme->_dumpFeature($_,
112 $self->implicant->{$_});
113 }
114 return join (' ', '[', @implier, ']', '=>', '[', @implicant, ']');
115 }
116 ##################################################################
117 sub apply {
118 my $self = shift;
119 my Lingua::FeatureMatrix::Eme $eme = shift;
120 # modifies the eme passed in according to the implicant
121 foreach my $feature( keys %{$self->implicant} ) {
122 # issues warnings if implicated field already populated?
123 my $val = $self->implicant->{$feature};
124
125 my $isProblem;
126 if (defined $eme->$feature() and $eme->$feature eq 'unset') {
127 }
128 elsif (not defined $eme->$feature()) {
129 if (defined $val) {
130 $isProblem = 1;
131 }
132 else {
133 $isProblem = 0;
134 }
135 }
136 else {
137 # defined $eme->$feature()
138 if (not defined $val) {
139 $isProblem = 1;
140 }
141 else {
142 # both defined
143 $isProblem = ($eme->$feature != $val);
144 }
145 }
146 if ($isProblem) {
147 croak "implicature ", $self->dumpToText,
148 " wanted to assign new value '",
149 (defined $val ? $val : 'undef'),
150 "' to $feature but eme '",
151 $eme->name(),
152 "' already had value '",
153 (defined $eme->$feature ? $eme->$feature : 'undef'),
154 "' at $feature!\n";
155 }
156 elsif (defined $isProblem) {
157 # my $implier = $eme->by_implication($feature);
158 if (not $eme->by_implication_count($feature)) {
159 warn (join '', "Implicature '", $self->dumpToText,
160 "' and eme '", $eme->name(), "' definition are ",
161 "mutually redundant with respect to the '$feature' feature\n");
162 }
163 }
164
165 $eme->$feature($val);
166
167 $eme->by_implication_push($feature => $self);
168 }
169 }
170 ##################################################################
171 sub sortByRuleOrder {
172 my $class = shift;
173 my @implicatures = @_;
174
175
176 foreach my $implicature (@implicatures) {
177
178 # look for match from current implicant to another rule's implier.
179 foreach my $otherImplicature (@implicatures) {
180 # skip self!
181 next if ($implicature == $otherImplicature);
182
183 # if current implicant matches other implicature's implier,
184 # current implicature must come first.
185
186 # JGK: we should draw a directed arc between current and other;
187 # then do a topological sort.
188 }
189 }
190 my @sorted;
191
192
193
194 # ...
195 return @sorted;
196 }
197
198 1;
199 __END__