| 1 | package Lingua::FeatureMatrix::Eme; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | our $VERSION = '0.02'; | ||
| 7 | ################################################################## | ||
| 8 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | use Class::MethodMaker | ||
| 10 | 1 | ▶ | new_with_init => 'new', |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | new_hash_init => 'hash_init', | ||
| 12 | |||
| 13 | abstract => 'getFeatureNames', | ||
| 14 | hash_of_lists => 'by_implication', | ||
| 15 | # each key of by_implication is a feature name, each value is a | ||
| 16 | # reference to the Implicature object that set that feature. | ||
| 17 | |||
| 18 | 1 | get_set => 'name'; | |
| 19 | |||
| 20 | ################################################################## | ||
| 21 | sub init { | ||
| 22 | my $self = shift; | ||
| 23 | my $class = ref($self); | ||
| 24 | |||
| 25 | my %features = @_; | ||
| 26 | if ($features{options}) { | ||
| 27 | delete $features{options}; # any data here was only for the | ||
| 28 | # subclass | ||
| 29 | } | ||
| 30 | my (%defaults) = map { $_ => 'unset' } $class->getFeatureNames(); | ||
| 31 | |||
| 32 | $self->hash_init(%defaults, %features); | ||
| 33 | } | ||
| 34 | ################################################################## | ||
| 35 | sub failsContract { | ||
| 36 | my $class = shift; | ||
| 37 | my @features; | ||
| 38 | eval { @features = $class->getFeatureNames(); }; | ||
| 39 | if ($@) { | ||
| 40 | return "trouble calling $class->getFeatureNames(): " . $@; | ||
| 41 | } | ||
| 42 | if (not @features) { | ||
| 43 | return "No features listed!"; | ||
| 44 | } | ||
| 45 | # check that no feature is listed twice | ||
| 46 | my (%features); | ||
| 47 | foreach my $feature (@features) { | ||
| 48 | if ($features{$feature}) { | ||
| 49 | return "listed $feature twice!" | ||
| 50 | } | ||
| 51 | $features{$feature}++; | ||
| 52 | } | ||
| 53 | |||
| 54 | # reaching here indicates that the class fulfils the contract, | ||
| 55 | # return false value | ||
| 56 | return ''; | ||
| 57 | } | ||
| 58 | ################################################################## | ||
| 59 | sub explainFeature { | ||
| 60 | my $self = shift; | ||
| 61 | my $feature = shift; | ||
| 62 | if (not $self->_knowsFeature($feature)) { | ||
| 63 | return undef; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | my(@how) = map {$_->dumpToText() } $self->by_implication($feature); | ||
| 67 | if (@how) { | ||
| 68 | return @how; | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | return "by definition"; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | ################################################################## | ||
| 76 | sub isSpecified { | ||
| 77 | my $self = shift; | ||
| 78 | my $featureName = shift; | ||
| 79 | if (not $self->_knowsFeature($featureName)) { | ||
| 80 | die "unknown feature $featureName!\n"; | ||
| 81 | } | ||
| 82 | # undefined is a legal, specified value that indicates "ungrammatical" | ||
| 83 | if (not defined $self->$featureName() ) { | ||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | # 'unset' indicates "not yet indicated" | ||
| 87 | if ($self->$featureName() eq 'unset') { | ||
| 88 | return 0; | ||
| 89 | } | ||
| 90 | # 0 and 1 are the legal values other than undef | ||
| 91 | return ($self->$featureName() == 0 or $self->$featureName() == 1); | ||
| 92 | } # end isSpecified | ||
| 93 | ################################################################## | ||
| 94 | sub listUnspecified { | ||
| 95 | my $self = shift; | ||
| 96 | my $class = ref($self); | ||
| 97 | my (@unspecified) = (); | ||
| 98 | foreach my $feature ($class->getFeatureNames()) { | ||
| 99 | if (not $self->isSpecified($feature)) { | ||
| 100 | push @unspecified, $feature; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | return @unspecified; | ||
| 104 | } | ||
| 105 | ################################################################## | ||
| 106 | sub isFullySpecified { | ||
| 107 | my $self = shift; | ||
| 108 | return # whether | ||
| 109 | ($self->listUnspecified() == 0); | ||
| 110 | } | ||
| 111 | ################################################################## | ||
| 112 | sub listUserSpecified { | ||
| 113 | my $self = shift; | ||
| 114 | my $class = ref($self); | ||
| 115 | |||
| 116 | my (@userSpecified); | ||
| 117 | foreach my $feature ( $class->getFeatureNames() ) { | ||
| 118 | if (not $self->by_implication_count($feature)) { | ||
| 119 | # must have been set by user | ||
| 120 | if (not $self->isSpecified($feature)) { | ||
| 121 | warn $self->name . | ||
| 122 | " 's feature $feature never set by user or implicature\n"; | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | push @userSpecified, $feature; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | return @userSpecified; | ||
| 130 | } | ||
| 131 | ################################################################## | ||
| 132 | sub isEquivalent { | ||
| 133 | # returns whether this Eme is feature-equivalent to another Eme | ||
| 134 | # passed by argument. | ||
| 135 | my $self = shift; | ||
| 136 | my $other = shift; | ||
| 137 | |||
| 138 | # self and other better be same class | ||
| 139 | my $class = ref($self); | ||
| 140 | if (not $class eq ref($other)) { | ||
| 141 | croak "can't call $class->isEquivalent( ) with ", ref($other), | ||
| 142 | " object; use $class object for argument instead"; | ||
| 143 | } | ||
| 144 | |||
| 145 | FEATURE: foreach my $f ( $class->getFeatureNames() ) { | ||
| 146 | # if this feature not defined in one or both of the emes in question, | ||
| 147 | # skip the feature (they're equivalent) | ||
| 148 | if (not defined $self->$f) { | ||
| 149 | next FEATURE; | ||
| 150 | } | ||
| 151 | if (not defined $other->$f) { | ||
| 152 | next FEATURE; | ||
| 153 | } | ||
| 154 | |||
| 155 | if ($self->$f eq 'unset') { | ||
| 156 | warn "Can't test equivalence:\n" . | ||
| 157 | "feature '$f' not yet set for symbol'" . $self->name . "'\n"; | ||
| 158 | next FEATURE; | ||
| 159 | } | ||
| 160 | elsif ($other->$f eq 'unset') { | ||
| 161 | warn "Can't test equivalence:\n" . | ||
| 162 | "feature '$f' not yet set for symbol'" . $other->name . "'\n"; | ||
| 163 | next FEATURE; | ||
| 164 | } | ||
| 165 | |||
| 166 | # real comparison is here (above mostly checking for user thinkos) | ||
| 167 | if ($self->$f != $other->$f) { | ||
| 168 | return 0; # emes are distinct in at least feature $f, not equivalent | ||
| 169 | } | ||
| 170 | } # end loop over features | ||
| 171 | return 1; # couldn't find a distinction between these | ||
| 172 | } #end sub isEquivalent | ||
| 173 | ################################################################## | ||
| 174 | sub _knowsFeature { | ||
| 175 | my $class = shift; | ||
| 176 | my $putativeFeature = shift; | ||
| 177 | return ($class->can($putativeFeature)); | ||
| 178 | } | ||
| 179 | ################################################################## | ||
| 180 | sub hasFeature { | ||
| 181 | my $self = shift; | ||
| 182 | my $featureName = shift; | ||
| 183 | # check feature name | ||
| 184 | return ($self->$featureName); | ||
| 185 | } | ||
| 186 | ################################################################## | ||
| 187 | sub dumpToText { | ||
| 188 | my $self = shift; | ||
| 189 | my $features = $self->dumpFeaturesToText( $self->getFeatureNames ); | ||
| 190 | return $features ; | ||
| 191 | } | ||
| 192 | ################################################################## | ||
| 193 | sub _dumpFeature { | ||
| 194 | # given a list, puts back into string form: | ||
| 195 | my $class = shift; | ||
| 196 | my $feature = shift; | ||
| 197 | my $value = shift; | ||
| 198 | my $symbol; | ||
| 199 | if (not defined $value) { | ||
| 200 | $symbol = '*'; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | if ($value) { | ||
| 204 | if ($value eq 'unset') { | ||
| 205 | $symbol = '?'; # uh-oh | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $symbol = '+'; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | $symbol = '-'; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | return $symbol . $feature; | ||
| 216 | } | ||
| 217 | ################################################################## | ||
| 218 | sub dumpFeaturesToText { | ||
| 219 | my $self = shift; | ||
| 220 | my $class = ref($self); | ||
| 221 | my @features = @_; | ||
| 222 | my @text; | ||
| 223 | foreach my $feature (@features) { | ||
| 224 | push @text, $class->_dumpFeature($feature, $self->$feature()); | ||
| 225 | } | ||
| 226 | |||
| 227 | return join ' ', '[', @text, ']'; | ||
| 228 | } | ||
| 229 | ################################################################## | ||
| 230 | 1; | ||
| 231 | __END__ | ||