| 1 | # WordNet::SenseRelate::Preprocess::Compounds v0.09 | ||
| 2 | # (Last updated $Id: Compounds.pm,v 1.8 2006/12/24 12:18:46 sidz1979 Exp $) | ||
| 3 | |||
| 4 | package WordNet::SenseRelate::Preprocess::Compounds; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Exporter; | ||
| 9 | use WordNet::SenseRelate::Tools; | ||
| 10 | use WordNet::SenseRelate::Word; | ||
| 11 | |||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | our $VERSION = '0.09'; | ||
| 14 | |||
| 15 | # Constructor for this module | ||
| 16 | sub new | ||
| 17 | { | ||
| 18 | my $class = shift; | ||
| 19 | my $wntools = shift; | ||
| 20 | my $self = {}; | ||
| 21 | my $trace = shift; | ||
| 22 | $trace = 0 if (!defined $trace); | ||
| 23 | |||
| 24 | # Create the preprocessor object | ||
| 25 | $class = ref $class || $class; | ||
| 26 | bless($self, $class); | ||
| 27 | |||
| 28 | # Read in the wordnet data | ||
| 29 | if ( !defined $wntools | ||
| 30 | || !ref $wntools | ||
| 31 | || ref($wntools) ne "WordNet::SenseRelate::Tools") | ||
| 32 | { | ||
| 33 | $wntools = WordNet::SenseRelate::Tools->new($wntools); | ||
| 34 | return undef if (!defined $wntools); | ||
| 35 | } | ||
| 36 | $self->{wntools} = $wntools; | ||
| 37 | $self->{trace} = $trace; | ||
| 38 | $self->{tracestring} = ""; | ||
| 39 | |||
| 40 | # No options for this module | ||
| 41 | $self->{optionlist} = {}; | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | # Preprocess a given input instance: | ||
| 47 | # (1) Convert to lowercase | ||
| 48 | # (2) Remove unwanted characters | ||
| 49 | # (3) Combine all consecutive occurrence of numbers into one | ||
| 50 | # (4) Detect compounds | ||
| 51 | # (5) Split the words on white spaces | ||
| 52 | # (6) Create a context object | ||
| 53 | sub preprocess | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | my $instance = shift; | ||
| 57 | my $wntools = $self->{wntools}; | ||
| 58 | if (!defined($wntools)) | ||
| 59 | { | ||
| 60 | $wntools = {}; | ||
| 61 | $wntools->{compounds} = {}; | ||
| 62 | } | ||
| 63 | |||
| 64 | return undef if (!defined $self || !defined $instance); | ||
| 65 | my $trace = $self->{trace}; | ||
| 66 | |||
| 67 | # Create empty context object | ||
| 68 | # Instance Fields: | ||
| 69 | # (a) text (array) | ||
| 70 | # (b) words (array) | ||
| 71 | # (c) head (scalar:int) | ||
| 72 | # (d) target (scalar:int) | ||
| 73 | # (e) wordobjects (array) | ||
| 74 | # (f) lexelt (scalar:"art.n") | ||
| 75 | # (g) id (scalar:string) | ||
| 76 | # (h) answer (scalar:string, optional) | ||
| 77 | # (i) targetpos (scalar:string, "n/v/a/r") | ||
| 78 | my $context = {}; | ||
| 79 | $context->{words} = []; | ||
| 80 | $context->{text} = []; | ||
| 81 | $context->{head} = -1; | ||
| 82 | $context->{target} = -1; | ||
| 83 | $context->{wordobjects} = []; | ||
| 84 | $context->{lexelt} = $instance->{lexelt} if (defined $instance->{lexelt}); | ||
| 85 | $context->{id} = $instance->{id} if (defined $instance->{id}); | ||
| 86 | $context->{answer} = $instance->{answer} if (defined $instance->{answer}); | ||
| 87 | $context->{targetpos} = $instance->{targetpos} | ||
| 88 | if (defined $instance->{targetpos}); | ||
| 89 | |||
| 90 | # Check that the text segments exist | ||
| 91 | if (defined $instance->{text}) | ||
| 92 | { | ||
| 93 | foreach my $textseg (@{$instance->{text}}) | ||
| 94 | { | ||
| 95 | push(@{$context->{text}}, $textseg); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | $context->{head} = $instance->{head} if (defined $instance->{head}); | ||
| 99 | |||
| 100 | # Detect compounds in the words | ||
| 101 | my $string; | ||
| 102 | my $done; | ||
| 103 | my $temp; | ||
| 104 | my $firstPointer; | ||
| 105 | my $secondPointer; | ||
| 106 | |||
| 107 | # Start compound detection | ||
| 108 | $firstPointer = 0; | ||
| 109 | $string = ""; | ||
| 110 | |||
| 111 | while ($firstPointer <= $#{$instance->{words}}) | ||
| 112 | { | ||
| 113 | $secondPointer = ( | ||
| 114 | ($#{$instance->{words}} > ($firstPointer + 7)) | ||
| 115 | ? ($firstPointer + 7) | ||
| 116 | : ($#{$instance->{words}}) | ||
| 117 | ); | ||
| 118 | $done = 0; | ||
| 119 | while ($secondPointer > $firstPointer && !$done) | ||
| 120 | { | ||
| 121 | $temp = | ||
| 122 | join("_", @{$instance->{words}}[$firstPointer .. $secondPointer]); | ||
| 123 | if (defined $wntools->{compounds}->{$temp}) | ||
| 124 | { | ||
| 125 | push(@{$context->{words}}, $temp); | ||
| 126 | push( | ||
| 127 | @{$context->{wordobjects}}, | ||
| 128 | WordNet::SenseRelate::Word->new($temp) | ||
| 129 | ); | ||
| 130 | $context->{target} = scalar(@{$context->{words}}) - 1 | ||
| 131 | if ( defined $instance->{target} | ||
| 132 | && $instance->{target} >= $firstPointer | ||
| 133 | && $instance->{target} <= $secondPointer); | ||
| 134 | $done = 1; | ||
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | $secondPointer--; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | if (!$done) | ||
| 142 | { | ||
| 143 | push(@{$context->{words}}, $instance->{words}->[$firstPointer]); | ||
| 144 | push( | ||
| 145 | @{$context->{wordobjects}}, | ||
| 146 | WordNet::SenseRelate::Word->new( | ||
| 147 | $instance->{words}->[$firstPointer] | ||
| 148 | ) | ||
| 149 | ); | ||
| 150 | $context->{target} = scalar(@{$context->{words}}) - 1 | ||
| 151 | if (defined $instance->{target} | ||
| 152 | && $instance->{target} == $firstPointer); | ||
| 153 | } | ||
| 154 | $firstPointer = $secondPointer + 1; | ||
| 155 | } | ||
| 156 | |||
| 157 | return $context; | ||
| 158 | } | ||
| 159 | |||
| 160 | # Get the trace string, and reset the trace | ||
| 161 | sub getTraceString | ||
| 162 | { | ||
| 163 | my $self = shift; | ||
| 164 | return "" | ||
| 165 | if ( !defined $self | ||
| 166 | || !ref($self) | ||
| 167 | || ref($self) ne "WordNet::SenseRelate::Preprocess::Compounds"); | ||
| 168 | my $returnString = ""; | ||
| 169 | $returnString = $self->{tracestring} if (defined $self->{tracestring}); | ||
| 170 | $self->{tracestring} = ""; | ||
| 171 | return $returnString; | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | |||
| 176 | |||
| 177 | __END__ |