| 1 | # WordNet::SenseRelate::Tools v0.09 | ||
| 2 | # (Last updated $Id: Tools.pm,v 1.4 2006/12/24 12:18:45 sidz1979 Exp $) | ||
| 3 | |||
| 4 | package WordNet::SenseRelate::Tools; | ||
| 5 | |||
| 6 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 4 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use WordNet::QueryData; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | our @ISA = qw(Exporter); | ||
| 12 | our $VERSION = '0.09'; | ||
| 13 | |||
| 14 | # Constructor for this module | ||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $class = shift; | ||
| 18 | my $wn = shift; | ||
| 19 | my $self = {}; | ||
| 20 | |||
| 21 | # Create the preprocessor object | ||
| 22 | $class = ref $class || $class; | ||
| 23 | bless($self, $class); | ||
| 24 | |||
| 25 | # Read in the wordnet data | ||
| 26 | if (!defined $wn || !ref $wn || ref($wn) ne "WordNet::QueryData") | ||
| 27 | { | ||
| 28 | my $wnpath = undef; | ||
| 29 | $wnpath = $wn if(defined $wn && !ref($wn) && $wn ne ""); | ||
| 30 | $wn = WordNet::QueryData->new($wnpath); | ||
| 31 | return undef if (!defined $wn); | ||
| 32 | } | ||
| 33 | $self->{wn} = $wn; | ||
| 34 | |||
| 35 | # Get the compounds from WordNet | ||
| 36 | foreach my $pos ('n', 'v', 'a', 'r') | ||
| 37 | { | ||
| 38 | foreach my $word ($wn->listAllWords($pos)) | ||
| 39 | { | ||
| 40 | $self->{compounds}->{$word} = 1 if ($word =~ /_/); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | # Detect compounds in a block of text | ||
| 48 | sub compoundify | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | my $block = shift; | ||
| 52 | |||
| 53 | return $block | ||
| 54 | if (!defined $block || !ref $self || !defined $self->{compounds}); | ||
| 55 | |||
| 56 | my $string; | ||
| 57 | my $done; | ||
| 58 | my $temp; | ||
| 59 | my $firstPointer; | ||
| 60 | my $secondPointer; | ||
| 61 | my @wordsArray; | ||
| 62 | |||
| 63 | # get all the words into an array | ||
| 64 | @wordsArray = (); | ||
| 65 | while ($block =~ /(\w+)/g) | ||
| 66 | { | ||
| 67 | push(@wordsArray, $1); | ||
| 68 | } | ||
| 69 | |||
| 70 | # now compoundify, GREEDILY!! | ||
| 71 | $firstPointer = 0; | ||
| 72 | $string = ""; | ||
| 73 | |||
| 74 | while ($firstPointer <= $#wordsArray) | ||
| 75 | { | ||
| 76 | $secondPointer = | ||
| 77 | ( ($#wordsArray > ($firstPointer + 7)) | ||
| 78 | ? ($firstPointer + 7) | ||
| 79 | : ($#wordsArray)); | ||
| 80 | $done = 0; | ||
| 81 | while ($secondPointer > $firstPointer && !$done) | ||
| 82 | { | ||
| 83 | $temp = join("_", @wordsArray[$firstPointer .. $secondPointer]); | ||
| 84 | if (defined $self->{compounds}->{$temp}) | ||
| 85 | { | ||
| 86 | $string .= "$temp "; | ||
| 87 | $done = 1; | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | $secondPointer--; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | if (!$done) | ||
| 95 | { | ||
| 96 | $string .= "$wordsArray[$firstPointer] "; | ||
| 97 | } | ||
| 98 | $firstPointer = $secondPointer + 1; | ||
| 99 | } | ||
| 100 | $string =~ s/ $//; | ||
| 101 | |||
| 102 | return $string; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | __END__ | ||