File Coverage

blib/lib/WordNet/Extend/Insert.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # WordNet::Extend::Insert.pm version 0.030
2             # Updated: 10/13/16
3             #
4             # Ted Pedersen, University of Minnesota Duluth
5             # tpederse at d.umn.edu
6             #
7             # Jon Rusert, University of Minnesota Duluth
8             # ruse0008 at d.umn.edu
9             #
10             # This program is free software: you can redistribute it and/or modify
11             # it under the terms of the GNU General Public License as published by
12             # the Free Software Foundation, either version 3 of the License, or
13             # (at your option) any later version.
14             #
15             # This program is distributed in the hope that it will be useful,
16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             # GNU General Public License for more details.
19             #
20             # You should have received a copy of the GNU General Public License
21             # along with this program. If not, see .
22             #
23              
24             package WordNet::Extend::Insert;
25              
26             =head1 NAME
27              
28             WordNet::Extend::Insert - Perl module for inserting a lemma into
29             WordNet.
30              
31             =head1 SYNOPSIS
32              
33             =head2 Basic Usage Example
34              
35             use WordNet::Extend::Insert;
36              
37             my $insert = WordNet::Extend::Insert->new();
38              
39             @in1 = ("crackberry","noun","withdef.1", "A BlackBerry, a handheld device considered addictive for its networking capability.");
40              
41             @in2 = ("slackberry","noun","withdef.2", "A mocking name for crackberry.");
42              
43             @loc1 = ("withdef.5","cellphone#n#1");
44              
45             @loc2 = ("withdef.6","crackberry#n#1");
46              
47             $insert->attach(\@in1, \@loc1);
48              
49             $insert->merge(\@in2, \@loc2);
50              
51             =head1 DESCRIPTION
52              
53             =head2 Introduction
54              
55             WordNet is a widely used tool in NLP and other research areas. A drawback of WordNet is the amount of time between updates. WordNet was last updated and released in December, 2006, and no further updates are planned. WordNet::Extend::Insert aims to allow developers insert their own lemmas into WordNet which can help keep WordNet updated with new language in the world. It can also revert back to the original untouched WordNet (by calling restoreWordNet) if the user makes a mistake or simply wants the untouched WordNet to access.
56              
57             =over
58             =cut
59              
60 1     1   878 use WordNet::QueryData;
  0            
  0            
61             use Getopt::Long;
62             use File::Spec;
63             use File::Copy;
64             use File::Find;
65              
66             our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
67              
68             @ISA = qw(Exporter);
69              
70             %EXPORT_TAGS = ();
71              
72             @EXPORT_OK = ();
73              
74             @EXPORT = ();
75              
76             $VERSION = '0.030';
77              
78             #**************Variables**********************
79             $wn = WordNet::QueryData->new; #to be used to access data from wordnet
80             $WNHOME = "/usr/local/WordNet-3.0";
81             $WNSEARCHDICT = "$WNHOME/dict";
82             $wnCRLength = 29; #number of lines the copyright takes up in data.pos and index.pos.
83             #*********************************************
84              
85             GetOptions('help' => \$help);
86             if($help == 1)
87             {
88             printHelp();
89             exit(0);
90             }
91              
92             =head2 Methods
93              
94             The following methods are defined in this package:
95              
96             =head3 Public methods
97              
98             =over
99              
100             =item $obj->new()
101              
102             The constructor for WordNet::Extend::Insert objects.
103              
104             Parameters: none.
105              
106             Return value: the new blessed object
107              
108             =cut
109              
110             sub new
111             {
112             my $class = shift;
113             my $self = {};
114              
115             $self->{errorString} = '';
116             $self->{error}=0;
117              
118             bless $self, $class;
119            
120             return $self;
121             }
122              
123             =item $obj->getError()
124              
125             Allows the object to check if any errors have occurred.
126             Returns an array ($error, $errString), where $error
127             value equal to 1 represents a warning and 2 represents
128             an error with the requested commands. (If a user would
129             run attach() without enough arguments, the error code
130             would return 2). $errorString contains what error occurred.
131              
132             Parameter: None
133              
134             Returns: array of the form ($error, $errorString).
135              
136             =cut
137             sub getError()
138             {
139             my $self = shift;
140             my $error = $self->{error};
141             my $errString = $self->{errorString};
142             $self->{error}=0;
143             $self->{errorString} = "";
144             $errString =~ s/^[\r\n\t ]+//;
145             return ($error, $errString);
146             }
147              
148             =item $obj->attach($newSynset, $location)
149              
150             Takes in a new synset and inserts it into WordNet at the specified location
151             by attaching it to the specified location lemma as a hyponym. The location should
152             be represented by "lemma#pos#senseNum". For example, to attach to the 2nd sense
153             of the noun window, the location would be "window#n#2".
154              
155             Parameters: Synset array in form (lemma, part-of-speech, item-id, definition)
156             or "word\tpos\titem-id\tdef", and location to be inserted in form
157             (item-id, WordNet sense).
158              
159             Returns: nothing
160              
161             =cut
162             sub attach()
163             {
164             #need to load in new QueryData
165             $wn = WordNet::QueryData->new;
166             my $base = 0;
167             if(scalar @_ == 3)#checks if method entered by object.
168             {
169             $base = 1;
170             }
171            
172             my @newSyn = @{$_[$base]};
173             $base = $base +1;
174             if(scalar @newSyn == 1) #in second form
175             {
176             my @tempSyn = split("\t", $newSyn[0]);
177             @newSyn = @tempSyn;
178             }
179             my $pos = substr($newSyn[1], 0, 1);
180             my @location = @{$_[$base]};
181             my $write = 1; #write flag changes to 0 if error occurs so no write() will occur.
182              
183             if(scalar @newSyn < 4)
184             {
185             my $self = shift;
186             $self->{error} = 2;
187             $self->{errorString} = "New synset does not contain enough elements.";
188             $write = 0;
189             }
190            
191             if(scalar @location < 2)
192             {
193             my $self = shift;
194             $self->{error} = 2;
195             $self->{errorString} = "Location does not contain enough elements.";
196             $write = 0;
197             }
198              
199             unless (defined $wn->offset("$location[1]"))
200             {
201             my $self = shift;
202             $self->{error} = 2;
203             $self->{errorString} = "Location does not exist in WordNet.";
204             $write = 0;
205             }
206              
207             if($write == 1)
208             {
209             my $newOffset = findNewOffset($newSyn[1]);
210             my %CRNotice;
211             my %DataSpace;
212             my %offsetMap; #used to stored changes in offsets.
213             my $indexPos = "";
214             my $dataPos = "";
215             my $indexSense = "";
216             my $posNum = 0;
217             my $locationLemma = $location[1];
218             $locationLemma =~ s/#.*//; #extract lemma
219             my $locationPos = $newSyn[1]; #must be same pos as new.
220             my $locationOffset = $wn->offset("$location[1]");
221             while(length($locationOffset) < 8) #QueryData->offset() does not keep the 8 digits, need to add back lost 0's
222             {
223             $locationOffset = "0".$locationOffset;
224             }
225             my $indexFile = "$WNSEARCHDICT/index.$locationPos";
226             my $dataFile = "$WNSEARCHDICT/data.$locationPos";
227             my $senseFile = "$WNSEARCHDICT/index.sense";
228             open (WNINDEXNEW, '>', "$indexFile.new") or die $!;
229             open (WNDATANEW, '>', "$dataFile.new") or die $!;
230             open (WNSENSENEW, '>', "$senseFile.new") or die $!;
231              
232             #make filehandles hot
233             my $fhIndex = select(WNINDEXNEW);
234             $|=1;
235             select($fhIndex);
236              
237             my $fhData = select(WNDATANEW);
238             $|=1;
239             select($fhData);
240              
241             my $fhSense = select(WNSENSENEW);
242             $|=1;
243             select($fhSense);
244            
245             if($pos eq "n")
246             {
247             $posNum = 1;
248             }
249             else
250             {
251             if($pos eq "v")
252             {
253             $posNum = 2;
254             }
255             else
256             {
257             my $self = shift;
258             $self->{error} = 2;
259             $self->{errorString} = "Part of speech must be verb or noun";
260             $write = 0;
261             }
262             }
263            
264              
265             if(isNewWord($newSyn[0], $newSyn[1]) == 0)
266             {
267             my %hypData = %{getDataInfo($locationOffset, $locationPos)};
268             my %hypInfo = %{getIndexInfo($locationLemma, $locationPos)};
269             #print from three required files.
270             open WNINDEX, "$indexFile" or die $!;
271             open WNDATA, "$dataFile" or die $!;
272             open WNSENSE, "$senseFile" or die $!;
273             open (WNDATATEMP, '>', "$dataFile.temp") or die $!;
274            
275             my $changed = 0;
276             my $curLine = 1;
277             while()
278             {
279             for $tempIn (split("\n"))
280             {
281             my $spaceTmp = $tempIn;
282             $spaceTmp =~ /( *)$/;
283             $DataSpace{$curLine} = length($1);
284             if($curLine > $wnCRLength)
285             {
286             my @tempLine = split /\s/, $tempIn;
287             if($changed == 1)
288             {
289             my $newNewOffset = $tempLine[0] +18;
290             while(length($newNewOffset) < 8)
291             {
292             $newNewOffset = "0".$newNewOffset;
293             }
294             $offsetMap{$tempLine[0]} = $newNewOffset;
295             }
296             else
297             {
298             $offsetMap{$tempLine[0]} = $tempLine[0];
299             }
300            
301             if($tempLine[0] == $locationOffset)
302             {
303             my $newPcnt = $hypData{'p_cnt'} + 1;
304             while(length $newPcnt < 3)#needs to be represented by 3 digits.
305             {
306             $newPcnt = "0".$newPcnt;
307             }
308             $newOffset = $newOffset + 18; #18 is the length of new data being added.
309             $tempIn = "$hypData{'synset_offset'} $hypData{'lex_filenum'} $hypData{'ss_type'} $hypData{'w_cnt'} $hypData{'word_lex_id'} $newPcnt $hypData{'ptr'} ~ $newOffset $pos 0000 | $hypData{'gloss'}";
310             $changed = 1;
311             }
312             }
313             else
314             {
315             $CRNotice{$curLine} = $tempIn;
316             }
317              
318             $curLine+=1;
319            
320             print WNDATATEMP "$tempIn\n";
321             }
322             }
323              
324             $indexPos ="$newSyn[0] $pos 1 1 \@ 1 0 $newOffset";
325             $dataPos = "$newOffset $hypData{'lex_filenum'} $pos 01 $newSyn[0] 0 001 \@ $hypData{'synset_offset'} $pos 0000 | $newSyn[3]";
326             $indexSense = "$newSyn[0]%$posNum:$hypData{'lex_filenum'}:00:: $newOffset 1 0";
327              
328             close WNDATATEMP;
329             open WNDATATEMP, "$dataFile.temp" or die $!;
330              
331             $curLine = 1;
332             while()
333             {
334             for $tempIn (split("\n"))
335             {
336             my @tempLine = split /\s/, $tempIn;
337             for my $i (0 .. $#tempLine)
338             {
339             if(exists $offsetMap{$tempLine[$i]})
340             {
341             $tempLine[$i] = "$offsetMap{$tempLine[$i]}";
342             }
343            
344             }
345             $tempIn = join(' ', @tempLine);
346            
347             if($curLine > $wnCRLength)
348             {
349             for($i=1; $i <= $DataSpace{$curLine}; $i++)
350             {
351             $tempIn = $tempIn . " ";
352             }
353             print WNDATANEW "$tempIn\n";
354             }
355             else
356             {
357             print WNDATANEW "$CRNotice{$curLine}\n";
358             }
359             $curLine+=1;
360             }
361             }
362             print WNDATANEW "$dataPos \n";
363              
364             $curLine = 1;
365             $alpha = 1;
366             while()
367             {
368             for $tempIn (split("\n"))
369             {
370             if($curLine > $wnCRLength)
371             {
372             #need to add hyponym pointer if it did not exist before on hypernym
373             if($tempIn =~ /^$locationLemma\b[^-]/)
374             {
375             unless($tempIn =~ /\~/)
376             {
377             my $newPcnt = $hypInfo{'p_cnt'};
378             $newPcnt+=1;
379             $tempIn ="$hypInfo{'lemma'} $hypInfo{'pos'} $hypInfo{'synset_cnt'} $newPcnt $hypInfo{'ptr_symbol'} ~ $hypInfo{'sense_cnt'} $hypInfo{'tagsense_cnt'} $hypInfo{'synset_offset'}";
380             }
381             }
382              
383             my @tempLine = split /\s/, $tempIn;
384            
385             #add in $indexPos alphabetically
386             if($alpha == 1)
387             {
388             if(($tempLine[0] cmp $newSyn[0]) == 1 )
389             {
390             print WNINDEXNEW "$indexPos \n";
391             $alpha = 0;
392             }
393             }
394            
395             my $tmpPcnt = $tempLine[2];
396             my $offsetPtr = scalar(@tempLine) - 1;
397             while($tmpPcnt > 0)
398             {
399             if(exists $offsetMap{$tempLine[$offsetPtr]})
400             {
401             $tempLine[$offsetPtr] = "$offsetMap{$tempLine[$offsetPtr]}";
402             }
403             $tmpPcnt-=1;
404             $offsetPtr-=1;
405             }
406             $tempIn = join(' ', @tempLine);
407            
408             }
409             else
410             {
411             $curLine+=1;
412             }
413             print WNINDEXNEW "$tempIn \n";
414             }
415             }
416              
417             $alpha = 1;
418             while()
419             {
420             for $tempIn (split("\n"))
421             {
422            
423             my @tempLine = split /\s/, $tempIn;
424             #add in $indexSense alphabetically
425             if($alpha == 1)
426             {
427             if(($tempLine[0] cmp $newSyn[0]) == 1 )
428             {
429             print WNSENSENEW "$indexSense\n";
430             $alpha = 0;
431             }
432             }
433            
434             if(exists $offsetMap{$tempLine[1]})
435             {
436             $tempLine[1] = "$offsetMap{$tempLine[1]}";
437             }
438             $tempIn = join(' ', @tempLine);
439            
440             print WNSENSENEW "$tempIn\n";
441             }
442             }
443            
444             close WNINDEX;
445             close WNDATA;
446             close WNSENSE;
447             close WNDATATEMP;
448             }
449             else #lemma already exists
450             {
451             my %hypData = %{getDataInfo($locationOffset, $locationPos)};
452             my %hypInfo = %{getIndexInfo($locationLemma, $locationPos)};
453             my %lemmaIndex = %{getIndexInfo($newSyn[0], $newSyn[1])};
454             my $newSynNum = $lemmaIndex{'synset_cnt'} + 1;
455            
456             #print to three required files.
457             open WNINDEX, "$indexFile" or die $!;
458             open WNDATA, "$dataFile" or die $!;
459             open WNSENSE, "$senseFile" or die $!;
460             open (WNDATATEMP, '>', "$dataFile.temp") or die $!;
461            
462             my $changed = 0;
463             my $curLine = 1;
464             while()
465             {
466             for $tempIn (split("\n"))
467             {
468             my $spaceTmp = $tempIn;
469             $spaceTmp =~ /( *)$/;
470             $DataSpace{$curLine} = length($1);
471             if($curLine > $wnCRLength)
472             {
473             my @tempLine = split /\s/, $tempIn;
474             if($changed == 1)
475             {
476             my $newNewOffset = $tempLine[0] +18;
477             while(length($newNewOffset) < 8)
478             {
479             $newNewOffset = "0".$newNewOffset;
480             }
481             $offsetMap{$tempLine[0]} = $newNewOffset;
482             }
483             else
484             {
485             $offsetMap{$tempLine[0]} = $tempLine[0];
486             }
487            
488             if($tempLine[0] == $locationOffset)
489             {
490             my $newPcnt = $hypData{'p_cnt'} + 1;
491             while(length $newPcnt < 3)#needs to be represented by 3 digits.
492             {
493             $newPcnt = "0".$newPcnt;
494             }
495             $newOffset = $newOffset + 18; #14 is the length of new data being added.
496             $tempIn = "$hypData{'synset_offset'} $hypData{'lex_filenum'} $hypData{'ss_type'} $hypData{'w_cnt'} $hypData{'word_lex_id'} $newPcnt $hypData{'ptr'} ~ $newOffset $pos 0000 | $hypData{'gloss'}";
497             $changed = 1;
498             }
499             }
500             else
501             {
502             $CRNotice{$curLine} = $tempIn;
503             }
504              
505             $curLine+=1;
506            
507             print WNDATATEMP "$tempIn\n";
508             }
509             }
510            
511             $indexPos ="$newSyn[0] $pos $newSynNum $lemmaIndex{'p_cnt'} $lemmaIndex{'ptr_symbol'} $newSynNum $lemmaIndex{'tagsense_cnt'} $lemmaIndex{'synset_offset'} $newOffset";
512             $dataPos = "$newOffset $hypData{'lex_filenum'} $pos 01 $newSyn[0] 0 001 @ $hypData{'synset_offset'} $pos 0000 | $newSyn[3]";
513             $indexSense = "$newSyn[0]%$posNum:$hypData{'lex_filenum'}:00:: $newOffset $newSynNum 0";
514              
515             close WNDATATEMP;
516             open WNDATATEMP, "$dataFile.temp" or die $!;
517              
518             $curLine = 1;
519             while()
520             {
521             for $tempIn (split("\n"))
522             {
523             my @tempLine = split /\s/, $tempIn;
524             for my $i (0 .. $#tempLine)
525             {
526             if(exists $offsetMap{$tempLine[$i]})
527             {
528             $tempLine[$i] = "$offsetMap{$tempLine[$i]}";
529             }
530            
531             }
532             $tempIn = join(' ', @tempLine);
533              
534             if($curLine > $wnCRLength)
535             {
536             for($i=1; $i <= $DataSpace{$curLine}; $i++)
537             {
538             $tempIn = $tempIn . " ";
539             }
540             print WNDATANEW "$tempIn\n";
541             }
542             else
543             {
544             print WNDATANEW "$CRNotice{$curLine}\n";
545             }
546             $curLine+=1;
547             }
548             }
549             print WNDATANEW "$dataPos \n";
550              
551             $curLine = 1;
552             while()
553             {
554             for $tempIn (split("\n"))
555             {
556             if($curLine > $wnCRLength)
557             {
558             #need to add hyponym pointer if it did not exist before on hypernym
559             if($tempIn =~ /^$locationLemma\b[^-]/)
560             {
561             unless($tempIn =~ /\~/)
562             {
563             my $newPcnt = $hypInfo{'p_cnt'};
564             $newPcnt+=1;
565             $tempIn ="$hypInfo{'lemma'} $hypInfo{'pos'} $hypInfo{'synset_cnt'} $newPcnt $hypInfo{'ptr_symbol'} ~ $hypInfo{'sense_cnt'} $hypInfo{'tagsense_cnt'} $hypInfo{'synset_offset'}";
566             }
567             }
568              
569             if($tempIn =~ /^$newSyn[0]\b[^-]/)
570             {
571             $tempIn = "$indexPos";
572             }
573             my @tempLine = split /\s/, $tempIn;
574             my $tmpPcnt = $tempLine[2];
575             my $offsetPtr = scalar(@tempLine) - 1;
576             while($tmpPcnt > 0)
577             {
578             if(exists $offsetMap{$tempLine[$offsetPtr]})
579             {
580             $tempLine[$offsetPtr] = "$offsetMap{$tempLine[$offsetPtr]}";
581             }
582             $tmpPcnt-=1;
583             $offsetPtr-=1;
584             }
585             $tempIn = join(' ', @tempLine);
586            
587             }
588             else
589             {
590             $curLine+=1;
591             }
592              
593             print WNINDEXNEW "$tempIn \n";
594             }
595             }
596            
597             $alpha = 1;
598             while()
599             {
600             for $tempIn (split("\n"))
601             {
602             my @tempLine = split /\s/, $tempIn;
603             #add in $indexSense alphabetically
604             if($alpha == 1)
605             {
606             if(($tempLine[0] cmp $newSyn[0]) == 1 )
607             {
608             print WNSENSENEW "$indexSense\n";
609             $alpha = 0;
610             }
611             }
612            
613             if(exists $offsetMap{$tempLine[1]})
614             {
615             $tempLine[1] = "$offsetMap{$tempLine[1]}";
616             }
617             $tempIn = join(' ', @tempLine);
618            
619             print WNSENSENEW "$tempIn\n";
620             }
621             }
622            
623             close WNINDEX;
624             close WNDATA;
625             close WNSENSE;
626             close WNDATATEMP;
627             }
628            
629              
630             close WNSENSENEW;
631             close WNDATANEW;
632             close WNSENSENEW;
633              
634             if($write == 1)#if write was successful, overwrite old files with new.
635             {
636             #make backup files for last change
637             #first remove old last files
638             unlink glob "$WNSEARCHDICT/*.last";
639              
640             #next make new last files
641             copy($indexFile, "$indexFile.last");
642             copy($dataFile, "$dataFile.last");
643             copy($senseFile, "$senseFile.last");
644            
645             #if no backup files exists for restoreWordnet() make for easy revert.
646             my $backupcheck = "$indexFile.backup";
647             unless(-f $backupcheck)
648             {
649             copy($indexFile, "$indexFile.backup");
650             copy($dataFile, "$dataFile.backup");
651             }
652              
653             unless(-f "$senseFile.backup")
654             {
655             copy($senseFile, "$senseFile.backup");
656             }
657              
658             if(-f "$dataFile.temp")
659             {
660             unlink "$dataFile.temp";
661             }
662            
663             #overwrite old files with new updated files
664             unlink $indexFile;
665             unlink $dataFile;
666             unlink $senseFile;
667             move("$indexFile.new", $indexFile);
668             move("$dataFile.new", $dataFile);
669             move("$senseFile.new", $senseFile);
670             }
671             }
672              
673             }
674              
675             =item $obj->merge($newSynset, $location)
676              
677             Takes in a new synset and inserts it into WordNet at the specified location
678             by merging it into the specified location lemma as a synset. The location should
679             be represented by "lemma#pos#senseNum". For example, to merge to the 2nd sense
680             of the noun window, the location would be "window#n#2".
681              
682             Parameters: Synset array in form (lemma, part-of-speech, item-id, definition)
683             or "word\tpos\titem-id\tdef", and location to be inserted in form
684             (item-id, WordNet sense).
685              
686             Returns: nothing
687              
688             =cut
689             sub merge()
690             {
691             #need to load in new QueryData
692             $wn = WordNet::QueryData->new;
693             my $base = 0;
694             if(scalar @_ == 3)#checks if method entered by object.
695             {
696             $base = 1;
697             }
698            
699             my @newSyn = @{$_[$base]};
700             $base = $base +1;
701             if(scalar @newSyn == 1) #in second form
702             {
703             my @tempSyn = split("\t", $newSyn[0]);
704             @newSyn = @tempSyn;
705             }
706             my $pos = substr($newSyn[1], 0, 1);
707             my @location = @{$_[$base]};
708             my $write = 1; #write flag changes to 0 if error occurs so no write() will occur.
709              
710             if(scalar @newSyn < 4)
711             {
712             my $self = shift;
713             $self->{error} = 2;
714             $self->{errorString} = "New synset does not contain enough elements.";
715             $write = 0;
716             }
717            
718             if(scalar @location < 2)
719             {
720             my $self = shift;
721             $self->{error} = 2;
722             $self->{errorString} = "Location does not contain enough elements.";
723             $write = 0;
724             }
725              
726             unless (defined $wn->offset("$location[1]"))
727             {
728             my $self = shift;
729             $self->{error} = 2;
730             $self->{errorString} = "Location does not exist in WordNet.";
731             $write = 0;
732             }
733            
734             if($write == 1)
735             {
736             my $newOffset = findNewOffset($newSyn[1]);
737             my %offsetMap; #used to stored changes in offsets.
738             my %CRNotice;
739             my %DataSpace;
740             my $indexPos = "";
741             my $dataPos = "";
742             my $indexSense = "";
743             my $posNum = 0;
744             my $locationLemma = $location[1];
745             $locationLemma =~ s/#.*//; #extract lemma
746             my $locationPos = $newSyn[1]; #must be same pos as new.
747             my $locationOffset = $wn->offset("$location[1]");
748             while(length($locationOffset) < 8) #QueryData->offset() does not keep the 8 digits, need to add back lost 0's
749             {
750             $locationOffset = "0".$locationOffset;
751             }
752             my $indexFile = "$WNSEARCHDICT/index.$locationPos";
753             my $dataFile = "$WNSEARCHDICT/data.$locationPos";
754             my $senseFile = "$WNSEARCHDICT/index.sense";
755             open (WNINDEXNEW, '>', "$indexFile.new") or die $!;
756             open (WNDATANEW, '>', "$dataFile.new") or die $!;
757             open (WNSENSENEW, '>', "$senseFile.new") or die $!;
758              
759             #make filehandles hot
760             my $fhIndex = select(WNINDEXNEW);
761             $|=1;
762             select($fhIndex);
763              
764             my $fhData = select(WNDATANEW);
765             $|=1;
766             select($fhData);
767              
768             my $fhSense = select(WNSENSENEW);
769             $|=1;
770             select($fhSense);
771            
772             if($pos eq "n")
773             {
774             $posNum = 1;
775             }
776             else
777             {
778             if($pos eq "v")
779             {
780             $posNum = 2;
781             }
782             else
783             {
784             my $self = shift;
785             $self->{error} = 2;
786             $self->{errorString} = "Part of speech must be verb or noun";
787             $write = 0;
788             }
789             }
790            
791              
792             if(isNewWord($newSyn[0], $newSyn[1]) == 0)
793             {
794             my %synIndex = %{getIndexInfo($locationLemma, $locationPos)};
795             my %synData = %{getDataInfo($locationOffset, $locationPos)};
796            
797             #print to three required files.
798             open WNINDEX, "$indexFile" or die $!;
799             open WNDATA, "$dataFile" or die $!;
800             open WNSENSE, "$senseFile" or die $!;
801             open (WNDATATEMP, '>', "$dataFile.temp") or die $!;
802            
803             my $changed = 0;
804             my $curLine = 1;
805             my $newWordLength = length($newSyn[0]) + 2;
806             while()
807             {
808             for $tempIn (split("\n"))
809             {
810             my $spaceTmp = $tempIn;
811             $spaceTmp =~ /( *)$/;
812             $DataSpace{$curLine} = length($1);
813             if($curLine > $wnCRLength)
814             {
815             my @tempLine = split /\s/, $tempIn;
816             if($changed == 1)
817             {
818             my $newNewOffset = $tempLine[0] + $newWordLength;
819             while(length($newNewOffset) < 8)
820             {
821             $newNewOffset = "0".$newNewOffset;
822             }
823             $offsetMap{$tempLine[0]} = $newNewOffset;
824             }
825             else
826             {
827             $offsetMap{$tempLine[0]} = $tempLine[0];
828             }
829              
830             if($tempLine[0] == $locationOffset)
831             {
832             $changed = 1;
833             }
834             }
835             else
836             {
837             $CRNotice{$curLine} = $tempIn;
838             }
839             $curLine+=1;
840              
841             print WNDATATEMP "$tempIn\n";
842             }
843             }
844            
845             $indexPos = "$newSyn[0] $pos 1 $synIndex{'p_cnt'} $synIndex{'ptr_symbol'} 1 0 $locationOffset";
846             my $wcnt = $synData{'w_cnt'} + 1;
847             $dataPos = "$locationOffset $synData{'lex_filenum'} $synData{'ss_type'} $wcnt $synData{'word_lex_id'} $newSyn[0] 0 $synData{'p_cnt'} $synData{'ptr'} | $synData{'gloss'}";
848             $indexSense = "$newSyn[0]%$posNum:$synData{'lex_filenum'}:00:: $locationOffset 1 0";
849              
850             close WNDATATEMP;
851             open WNDATATEMP, "$dataFile.temp" or die $!;
852              
853             $curLine = 1;
854             while()
855             {
856             for $tempIn (split("\n"))
857             {
858             if($tempIn =~ /^$locationOffset\b/)
859             {
860             $tempIn = $dataPos;
861             }
862            
863             my @tempLine = split /\s/, $tempIn;
864             for my $i (0 .. $#tempLine)
865             {
866             if(exists $offsetMap{$tempLine[$i]})
867             {
868             $tempLine[$i] = "$offsetMap{$tempLine[$i]}";
869             }
870              
871             }
872             $tempIn = join(' ', @tempLine);
873              
874             if($curLine > $wnCRLength)
875             {
876             for($i=1; $i <= $DataSpace{$curLine}; $i++)
877             {
878             $tempIn = $tempIn . " ";
879             }
880             print WNDATANEW "$tempIn\n";
881             }
882             else
883             {
884             print WNDATANEW "$CRNotice{$curLine}\n";
885             }
886             $curLine+=1;
887             }
888             }
889              
890             my $alpha = 1;
891             $curLine = 1;
892             while()
893             {
894             for $tempIn (split("\n"))
895             {
896             if($curLine > $wnCRLength)
897             {
898             my @tempLine = split /\s/, $tempIn;
899            
900             #add in $indexPos alphabetically
901             if($alpha == 1)
902             {
903             if(($tempLine[0] cmp $newSyn[0]) == 1 )
904             {
905             print WNINDEXNEW "$indexPos \n";
906             $alpha = 0;
907             }
908             }
909            
910             my $tmpPcnt = $tempLine[2];
911             my $offsetPtr = scalar(@tempLine) - 1;
912             while($tmpPcnt > 0)
913             {
914             if(exists $offsetMap{$tempLine[$offsetPtr]})
915             {
916             $tempLine[$offsetPtr] = "$offsetMap{$tempLine[$offsetPtr]}";
917             }
918             $tmpPcnt-=1;
919             $offsetPtr-=1;
920             }
921             $tempIn = join(' ', @tempLine);
922             }
923             else
924             {
925             $curLine+=1;
926             }
927             print WNINDEXNEW "$tempIn \n";
928             }
929             }
930              
931             $alpha = 1;
932             while()
933             {
934             for $tempIn (split("\n"))
935             {
936             my @tempLine = split /\s/, $tempIn;
937             #add in $indexSense alphabetically
938             if($alpha == 1)
939             {
940             if(($tempLine[0] cmp $newSyn[0]) == 1 )
941             {
942             print WNSENSENEW "$indexSense\n";
943             $alpha = 0;
944             }
945             }
946            
947             if(exists $offsetMap{$tempLine[1]})
948             {
949             $tempLine[1] = "$offsetMap{$tempLine[1]}";
950             }
951             $tempIn = join(' ', @tempLine);
952              
953             print WNSENSENEW "$tempIn\n";
954             }
955             }
956             print WNSENSENEW "$indexSense\n";
957             close WNINDEX;
958             close WNDATA;
959             close WNSENSE;
960             close WNDATATEMP;
961             }
962             else #lemma already exists
963             {
964             my %synIndex = %{getIndexInfo($locationLemma, $locationPos)};
965             my %synData = %{getDataInfo($locationOffset, $locationPos)};
966             my %lemmaIndex = %{getIndexInfo($newSyn[0], $newSyn[1])};
967             my $newSynNum =$lemmaIndex{'synset_cnt'} + 1;
968              
969             #print to three required files.
970             open WNINDEX, "$indexFile" or die $!;
971             open WNDATA, "$dataFile" or die $!;
972             open WNSENSE, "$senseFile" or die $!;
973             open (WNDATATEMP, '>', "$dataFile.temp") or die $!;
974            
975             my $changed = 0;
976             my $curLine = 1;
977             my $newWordLength = length($newSyn[0]) + 3;
978             while()
979             {
980             for $tempIn (split("\n"))
981             {
982             my $spaceTmp = $tempIn;
983             $spaceTmp =~ /( *)$/;
984             $DataSpace{$curLine} = length($1);
985             if($curLine > $wnCRLength)
986             {
987             my @tempLine = split /\s/, $tempIn;
988             if($changed == 1)
989             {
990             my $newNewOffset = $tempLine[0] + $newWordLength;
991             while(length($newNewOffset) < 8)
992             {
993             $newNewOffset = "0".$newNewOffset;
994             }
995             $offsetMap{$tempLine[0]} = $newNewOffset;
996             }
997             else
998             {
999             $offsetMap{$tempLine[0]} = $tempLine[0];
1000             }
1001              
1002             if($tempLine[0] == $locationOffset)
1003             {
1004             $changed = 1;
1005             }
1006             }
1007             else
1008             {
1009             $CRNotice{$curLine} = $tempIn;
1010             }
1011             $curLine+=1;
1012              
1013             print WNDATATEMP "$tempIn\n";
1014             }
1015             }
1016            
1017            
1018            
1019             $indexPos = "$newSyn[0] $pos $newSynNum $lemmaIndex{'p_cnt'} $lemmaIndex{'ptr_symbol'} $newSynNum $lemmaIndex{'tagsense_cnt'} $lemmaIndex{'synset_offset'} $locationOffset";
1020             my $wcnt = $synData{'w_cnt'} + 1;
1021             if(length $wcnt < 2)
1022             {
1023             $wcnt = "0".$wcnt; #needs to be represented by 2 digit number.
1024             }
1025             $dataPos = "$locationOffset $synData{'lex_filenum'} $synData{'ss_type'} $wcnt $synData{'word_lex_id'} $newSyn[0] 0 $synData{'p_cnt'} $synData{'ptr'} | $synData{'gloss'}";
1026             $indexSense = "$newSyn[0]%$posNum:$synData{'lex_filenum'}:00:: $locationOffset $newSynNum 0";
1027              
1028             close WNDATATEMP;
1029             open WNDATATEMP, "$dataFile.temp" or die $!;
1030              
1031             $curLine = 1;
1032             while()
1033             {
1034             for $tempIn (split("\n"))
1035             {
1036             if($tempIn =~ /^$locationOffset\b/)
1037             {
1038             $tempIn = $dataPos;
1039             }
1040              
1041             my @tempLine = split /\s/, $tempIn;
1042             for my $i (0 .. $#tempLine)
1043             {
1044             if(exists $offsetMap{$tempLine[$i]})
1045             {
1046             $tempLine[$i] = "$offsetMap{$tempLine[$i]}";
1047             }
1048              
1049             }
1050             $tempIn = join(' ', @tempLine);
1051              
1052             if($curLine > $wnCRLength)
1053             {
1054             for($i=1; $i <= $DataSpace{$curLine}; $i++)
1055             {
1056             $tempIn = $tempIn . " ";
1057             }
1058             print WNDATANEW "$tempIn\n";
1059             }
1060             else
1061             {
1062             print WNDATANEW "$CRNotice{$curLine}\n";
1063             }
1064             $curLine+=1;
1065             }
1066             }
1067            
1068             $curLine = 1;
1069             while()
1070             {
1071             for $tempIn (split("\n"))
1072             {
1073             if($curLine > $wnCRLength)
1074             {
1075             if($tempIn =~ /^$newSyn[0]\b[^-]/)
1076             {
1077             $tempIn = $indexPos;
1078             }
1079             my @tempLine = split /\s/, $tempIn;
1080             my $tmpPcnt = $tempLine[2];
1081             my $offsetPtr = scalar(@tempLine) - 1;
1082             while($tmpPcnt > 0)
1083             {
1084             if(exists $offsetMap{$tempLine[$offsetPtr]})
1085             {
1086             $tempLine[$offsetPtr] = "$offsetMap{$tempLine[$offsetPtr]}";
1087             }
1088             $tmpPcnt-=1;
1089             $offsetPtr-=1;
1090             }
1091             $tempIn = join(' ', @tempLine);
1092             }
1093             else
1094             {
1095             $curLine+=1;
1096             }
1097             print WNINDEXNEW "$tempIn \n";
1098             }
1099             }
1100              
1101             $alpha = 1;
1102             while()
1103             {
1104             for $tempIn (split("\n"))
1105             {
1106             my @tempLine = split /\s/, $tempIn;
1107             #add in $indexSense alphabetically
1108             if($alpha == 1)
1109             {
1110             if(($tempLine[0] cmp $newSyn[0]) == 1 )
1111             {
1112             print WNSENSENEW "$indexSense\n";
1113             $alpha = 0;
1114             }
1115             }
1116            
1117             if(exists $offsetMap{$tempLine[1]})
1118             {
1119             $tempLine[1] = "$offsetMap{$tempLine[1]}";
1120             }
1121             $tempIn = join(' ', @tempLine);
1122              
1123             print WNSENSENEW "$tempIn\n";
1124             }
1125             }
1126             print WNSENSENEW "$indexSense\n";
1127             close WNINDEX;
1128             close WNDATA;
1129             close WNSENSE;
1130             close WNDATATEMP;
1131             }
1132              
1133             close WNSENSENEW;
1134             close WNDATANEW;
1135             close WNSENSENEW;
1136              
1137             if($write == 1)#if write was successful, overwrite old files with new.
1138             {
1139             #make backup files for last change
1140             #first remove old last files
1141             unlink glob "$WNSEARCHDICT/*.last";
1142              
1143             #next make new last files
1144             copy($indexFile, "$indexFile.last");
1145             copy($dataFile, "$dataFile.last");
1146             copy($senseFile, "$senseFile.last");
1147            
1148             #if no backup files for restoreWordnet() exists make for easy revert.
1149             my $backupcheck = "$indexFile.backup";
1150             unless(-f $backupcheck)
1151             {
1152             copy($indexFile, "$indexFile.backup");
1153             copy($dataFile, "$dataFile.backup");
1154             }
1155              
1156             unless(-f "$senseFile.backup")
1157             {
1158             copy($senseFile, "$senseFile.backup");
1159             }
1160              
1161             if(-f "$dataFile.temp")
1162             {
1163             unlink "$dataFile.temp";
1164             }
1165            
1166             #overwrite old files with new updated files
1167             unlink $indexFile;
1168             unlink $dataFile;
1169             unlink $senseFile;
1170             move("$indexFile.new", $indexFile);
1171             move("$dataFile.new", $dataFile);
1172             move("$senseFile.new", $senseFile);
1173             }
1174             }
1175              
1176             }
1177              
1178             =item $obj->restoreWordNet()
1179              
1180             Causes all WordNet\dict files to be restored to their original
1181             state before any inserts were performed. This is equivalent to
1182             installing WordNet\dict fresh on your machine.
1183              
1184             Parameter: none
1185              
1186             Returns: nothing
1187              
1188             =cut
1189              
1190             sub restoreWordNet()
1191             {
1192             my $backupFlag = 0;
1193            
1194             if(-f "$WNSEARCHDICT/index.noun.backup")
1195             {
1196             unlink "$WNSEARCHDICT/index.noun";
1197             unlink "$WNSEARCHDICT/data.noun";
1198             $backupFlag = 1;
1199              
1200             move("$WNSEARCHDICT/index.noun.backup", "$WNSEARCHDICT/index.noun");
1201             move("$WNSEARCHDICT/data.noun.backup", "$WNSEARCHDICT/data.noun");
1202             }
1203              
1204             if(-f "$WNSEARCHDICT/index.verb.backup")
1205             {
1206             unlink "$WNSEARCHDICT/index.verb";
1207             unlink "$WNSEARCHDICT/data.verb";
1208             $backupFlag = 1;
1209              
1210             move("$WNSEARCHDICT/index.verb.backup", "$WNSEARCHDICT/index.verb");
1211             move("$WNSEARCHDICT/data.verb.backup", "$WNSEARCHDICT/data.verb");
1212             }
1213              
1214             if(-f "$WNSEARCHDICT/index.adj.backup")
1215             {
1216             unlink "$WNSEARCHDICT/index.adj";
1217             unlink "$WNSEARCHDICT/data.adj";
1218             $backupFlag = 1;
1219              
1220             move("$WNSEARCHDICT/index.adj.backup", "$WNSEARCHDICT/index.adj");
1221             move("$WNSEARCHDICT/data.adj.backup", "$WNSEARCHDICT/data.adj");
1222             }
1223              
1224             if(-f "$WNSEARCHDICT/index.adv.backup")
1225             {
1226             unlink "$WNSEARCHDICT/index.adv";
1227             unlink "$WNSEARCHDICT/data.adv";
1228             $backupFlag = 1;
1229              
1230             move("$WNSEARCHDICT/index.adv.backup", "$WNSEARCHDICT/index.adv");
1231             move("$WNSEARCHDICT/data.adv.backup", "$WNSEARCHDICT/data.adv");
1232             }
1233            
1234             if($backupFlag == 1)
1235             {
1236             unlink "$WNSEARCHDICT/index.sense";
1237              
1238             move("$WNSEARCHDICT/index.sense.backup", "$WNSEARCHDICT/index.sense");
1239              
1240             unlink glob "$WNSEARCHDICT/*.last";
1241             }
1242             }
1243              
1244             =item $obj->revertLastChange()
1245              
1246             Allows the user to undo the last insert made to WordNet.
1247              
1248             Parameter: none
1249              
1250             Returns: nothing
1251              
1252             =cut
1253              
1254             sub revertLastChange()
1255             {
1256             my $backupFlag = 0;
1257            
1258             if(-f "$WNSEARCHDICT/index.noun.last")
1259             {
1260             unlink "$WNSEARCHDICT/index.noun";
1261             unlink "$WNSEARCHDICT/data.noun";
1262             $backupFlag = 1;
1263              
1264             move("$WNSEARCHDICT/index.noun.last", "$WNSEARCHDICT/index.noun");
1265             move("$WNSEARCHDICT/data.noun.last", "$WNSEARCHDICT/data.noun");
1266             }
1267              
1268             if(-f "$WNSEARCHDICT/index.verb.last")
1269             {
1270             unlink "$WNSEARCHDICT/index.verb";
1271             unlink "$WNSEARCHDICT/data.verb";
1272             $backupFlag = 1;
1273              
1274             move("$WNSEARCHDICT/index.verb.last", "$WNSEARCHDICT/index.verb");
1275             move("$WNSEARCHDICT/data.verb.last", "$WNSEARCHDICT/data.verb");
1276             }
1277              
1278             if(-f "$WNSEARCHDICT/index.adj.last")
1279             {
1280             unlink "$WNSEARCHDICT/index.adj";
1281             unlink "$WNSEARCHDICT/data.adj";
1282             $backupFlag = 1;
1283              
1284             move("$WNSEARCHDICT/index.adj.last", "$WNSEARCHDICT/index.adj");
1285             move("$WNSEARCHDICT/data.adj.last", "$WNSEARCHDICT/data.adj");
1286             }
1287              
1288             if(-f "$WNSEARCHDICT/index.adv.last")
1289             {
1290             unlink "$WNSEARCHDICT/index.adv";
1291             unlink "$WNSEARCHDICT/data.adv";
1292             $backupFlag = 1;
1293              
1294             move("$WNSEARCHDICT/index.adv.last", "$WNSEARCHDICT/index.adv");
1295             move("$WNSEARCHDICT/data.adv.last", "$WNSEARCHDICT/data.adv");
1296             }
1297              
1298             if($backupFlag == 1)
1299             {
1300             unlink "$WNSEARCHDICT/index.sense";
1301              
1302             move("$WNSEARCHDICT/index.sense.last", "$WNSEARCHDICT/index.sense");
1303             }
1304              
1305             }
1306            
1307             =item $obj->isNewWord($lemma, $pos)
1308              
1309             Takes in a lemma and searches wordnet to see if it exists.
1310              
1311             Parameter: the lemma to search against along with the part of speech.
1312              
1313             Returns: 1 if lemma is found or 0 if not.
1314              
1315             =cut
1316              
1317             sub isNewWord()
1318             {
1319             my $base = 0;
1320             if(scalar @_ == 3)
1321             {
1322             $base = 1;#checks if method entered by object.
1323             }
1324             my $lemma = $_[$base];
1325             $base = $base +1;
1326             my $pos = $_[$base];
1327             my $indexFile = "$WNSEARCHDICT/index.$pos"; #wn file to be searched\
1328              
1329             open WNINDEX, "$indexFile" or die $!;
1330              
1331             while()
1332             {
1333             for $tempIn (split("\n"))
1334             {
1335             if($tempIn =~ /^$lemma\b[^-]/)
1336             {
1337             close WNINDEX;
1338             return 1;
1339             }
1340             }
1341             }
1342              
1343             close WNINDEX;
1344             return 0;
1345            
1346             }
1347              
1348             =item $obj->getIndexInfo($lemma, $pos)
1349              
1350             Takes in lemma and returns the information from the index.pos file.
1351              
1352             Parameter: the lemma info required and part of speech
1353              
1354             Returns: hash lemma info from index.pos with following information:
1355             lemma pos synset_cnt p_cnt ptr_symbol sense_cnt tagsense_cnt synset_offset
1356              
1357             =cut
1358              
1359             sub getIndexInfo()
1360             {
1361             my $base = 0;
1362             if(scalar @_ == 3)
1363             {
1364             $base = 1;#checks if method entered by object.
1365             }
1366             my $lemma = $_[$base];
1367             $base = $base+1;
1368             my $pos = $_[$base];
1369             my $indexFile = "$WNSEARCHDICT/index.$pos";
1370             my $indexInfoLine = "";
1371             my %indexInfo;
1372             open WNINDEX, "$indexFile" or die $!;
1373              
1374             while()
1375             {
1376             for $tempIn (split("\n"))
1377             {
1378             if($tempIn =~ /^$lemma\b[^-]/)
1379             {
1380             $indexInfoLine = $tempIn;
1381             close WNINDEX;
1382             }
1383             }
1384             }
1385              
1386             my @index = split /\s/, $indexInfoLine;
1387              
1388             $indexInfo{'lemma'} = $index[0];
1389             $indexInfo{'pos'} = $index[1];
1390             $indexInfo{'synset_cnt'} = $index[2];
1391             $indexInfo{'p_cnt'} = $index[3];
1392              
1393             #We gather all pointer symbols into one string for storing in the hash.
1394             my $pcnt = $index[3];
1395             my $ptrSym = "";
1396             my $offset = 0;
1397             while($pcnt >0)
1398             {
1399             my $sym = 4 + $offset;
1400             $ptrSym = $ptrSym . " $index[$sym]";
1401             $pcnt-=1;
1402             if($pcnt > 0)
1403             {
1404             $offset += 1;
1405             }
1406             }
1407             $ptrSym =~ s/^\s+//; #remove extra front whitespace
1408             $indexInfo{'ptr_symbol'} = $ptrSym;
1409            
1410             my $indexPtr = 5 + $offset; #new pointer to account for different number of ptr symbols
1411             $indexInfo{'sense_cnt'} = $index[$indexPtr];
1412             $indexPtr+=1;
1413             $indexInfo{'tagsense_cnt'} = $index[$indexPtr];
1414             $indexPtr+=1;
1415              
1416             #Finally we gather all offsets into one string to store in the hash.
1417             my $scnt = $index[2];
1418             my $indexOffsets = "";
1419             while($scnt > 0)
1420             {
1421             $indexOffsets = $indexOffsets . " $index[$indexPtr]";
1422             $indexPtr+=1;
1423             $scnt-=1;
1424             }
1425             $indexOffsets =~ s/^\s+//; #remove extra front whitespace
1426             $indexInfo{'synset_offset'} = $indexOffsets;
1427            
1428             return \%indexInfo;
1429             }
1430              
1431             =item $obj->getDataInfo($synsetOffset, $pos)
1432            
1433             Takes in synset offset and pos to find data associated with it in data.pos.
1434              
1435             Parameters: the synset offset and part of speech
1436              
1437             Returns: hash offset info from data.pos with following information:
1438             synset_offset lex_filenum ss_type w_cnt 'word_lex_id' p_cnt ptr | gloss
1439              
1440             =cut
1441              
1442             sub getDataInfo()
1443             {
1444             my $base = 0;
1445             if(scalar @_ == 3)
1446             {
1447             $base = 1;#checks if method entered by object.
1448             }
1449              
1450             my $synOffset = $_[$base];
1451             $base+=1;
1452             my $pos = $_[$base];
1453              
1454             my $dataFile = "$WNSEARCHDICT/data.$pos";
1455             my $dataInfoLine = "";
1456            
1457             open WNDATA, "$dataFile" or die $!;
1458            
1459             while()
1460             {
1461             for $tempIn (split("\n"))
1462             {
1463             if($tempIn =~ /^$synOffset\b/)
1464             {
1465             $dataInfoLine = $tempIn;
1466             close WNDATA;
1467             }
1468             }
1469             }
1470              
1471             my @data = split /\s/, $dataInfoLine;
1472              
1473             my %dataInfo;
1474             $dataInfo{'synset_offset'} = $data[0];
1475             $dataInfo{'lex_filenum'} = $data[1];
1476             $dataInfo{'ss_type'} = $data[2];
1477             $dataInfo{'w_cnt'} = $data[3];
1478              
1479             #we must consolidate the words and their lex ids into one string. it should be noted that
1480             # the lex ids for each word are stored within the string in the hash not separately.
1481             my $offset = 0;
1482             my $wcnt = $data[3];
1483             my $words = "";
1484             while($wcnt > 0)
1485             {
1486             my $wptr = 4 + $offset;
1487             $words = $words . " $data[$wptr]"; #appends word
1488             $wptr+=1;
1489             $words = $words . " $data[$wptr]"; #appends lex_id
1490             $wcnt-=1;
1491             if($wcnt > 0)
1492             {
1493             $offset+=2; #makes up for both the word and lex_id
1494             }
1495             }
1496             $words =~ s/^\s+//; #remove extra front whitespace
1497             $dataInfo{'word_lex_id'} = $words;
1498              
1499             my $dataPtr = 6 + $offset;
1500             $dataInfo{'p_cnt'} = $data[$dataPtr];
1501             $dataPtr+=1;
1502              
1503             #likewise, we consolidate all ptrs together into a single string.
1504             $offset = 0;
1505             my $pcnt = $dataInfo{'p_cnt'};;
1506             my $ptrs = "";
1507             while($pcnt > 0)
1508             {
1509             my $pptr = $dataPtr + $offset;
1510             $ptrs = $ptrs . " $data[$pptr]";#appends ptr symbol
1511             $pptr+=1;
1512             $ptrs = $ptrs . " $data[$pptr]";#appends synset offset
1513             $pptr+=1;
1514             $ptrs = $ptrs . " $data[$pptr]";#appends pos
1515             $pptr+=1;
1516             $ptrs = $ptrs . " $data[$pptr]";#appends source/target
1517             $pptr+=1;
1518             $pcnt-=1;
1519             $offset+=4;#makes up for all extracted data above.
1520             }
1521             $ptrs =~ s/^\s+//; #remove extra front whitespace
1522             $dataInfo{'ptr'} = $ptrs;
1523              
1524             $dataPtr = $dataPtr + $offset; #move ptr past retrieved info.
1525             $dataPtr+=1; #skip over '|' in file.
1526             my $size = scalar @data;
1527             my $gloss = "";
1528              
1529             #all the info that is left is the gloss, extract until no more info remains.
1530             while($dataPtr < $size)
1531             {
1532             $gloss = $gloss . " $data[$dataPtr]";
1533             $dataPtr+=1;
1534             }
1535             $gloss =~ s/^\s+//; #remove extra front whitespace
1536             $dataInfo{'gloss'} = $gloss;
1537              
1538             return \%dataInfo;
1539             }
1540              
1541             =item $obj->getSenseInfo($synsetOffset)
1542              
1543             Takes in a synset offset and returns the sense associated with the offset.
1544              
1545             Parameter: the synset offset of the desired lemma
1546              
1547             Returns: a hash offset info from index.sense with data:
1548             sense_key synset_offset sense_number tag_cnt
1549              
1550             =cut
1551              
1552             sub getSenseInfo()
1553             {
1554             my $base = 0;
1555             if(scalar @_ == 2)
1556             {
1557             $base = 1;#checks if method entered by object.
1558             }
1559              
1560             my $synOffset = $_[$base];
1561              
1562             my $senseFile = "$WNSEARCHDICT/index.sense";
1563             my $senseInfoLine = "";
1564            
1565             open WNSENSE, "$senseFile" or die $!;
1566            
1567             while()
1568             {
1569             for $tempIn (split("\n"))
1570             {
1571             if($tempIn =~ /\b$synOffset\b/)
1572             {
1573             $senseInfoLine = $tempIn;
1574             close WNSENSE;
1575             }
1576             }
1577             }
1578              
1579             my @sense = split /\s/, $senseInfoLine;
1580              
1581             my %senseInfo;
1582             $senseInfo{'sense_key'} = $sense[0];
1583             $senseInfo{'synset_offset'} = $sense[1];
1584             $senseInfo{'sense_number'} = $sense[2];
1585             $senseInfo{'tag_cnt'} = $sense[3];
1586              
1587             return \%senseInfo;
1588             }
1589              
1590             =item $obj->findNewOffset()
1591              
1592             Searches through and calculates the offset for inserting.
1593              
1594             Parameters: pos of new lemma
1595              
1596             Returns: new unused offset
1597              
1598             =cut
1599              
1600             sub findNewOffset()
1601             {
1602             my $offset = 0;
1603             my $base = 0;
1604             if(scalar @_ == 3)
1605             {
1606             $base = 1;#checks if method entered by object.
1607             }
1608              
1609             my $pos = $_[$base];
1610              
1611             my $dataFile = "$WNSEARCHDICT/data.$pos";
1612             my $dataLastLine = "";
1613            
1614             open WNDATA, "$dataFile" or die $!;
1615            
1616             while()
1617             {
1618             for $tempIn (split("\n"))
1619             {
1620             $dataLastLine = $tempIn;
1621             }
1622             }
1623              
1624             close WNDATA;
1625             my @data = split /\s/, $dataLastLine;
1626              
1627             $offset = $data[0] + length($dataLastLine) + 1;
1628            
1629             return $offset;
1630             }
1631              
1632             =item $obj->changeWNLocation()
1633              
1634             NOTE: Method not yet implemented, planned for next update.
1635              
1636             Allows the user to temporarily choose the location for WordNet
1637             which can be used to change between different WordNet
1638             dictionaries.
1639              
1640             Parameters: New location ex. "/usr/local/WordNet-3.0"
1641              
1642             Returns: nothing
1643              
1644             =cut
1645              
1646             #sub changeWNLocation()
1647             #{
1648             # my $base = 0;
1649             # if(scalar @_ == 2)
1650             # {
1651             # $base = 1;#checks if method entered by object.
1652             # }
1653             # my $newLocation = $_[$base];
1654             #
1655             # #check to see if /dict exists in the new WN location
1656             # $newLocation = "/home/csgrads/ruse0008/WordNet-Insert/WordNet-3.0";
1657             #
1658             # if(-d "$newLocation/dict")
1659             # {
1660             # $WNHOME = $newLocation;
1661             # }
1662             # else
1663             # {
1664             # my $self = shift;
1665             # $self->{error} = 2;
1666             # $self->{errorString} = "Desired WordNet location does not contain dict/.";
1667             # }
1668             #}
1669              
1670             #**************printHelp()**********************
1671             # Prints indepth help guide to screen.
1672             #***********************************************
1673             sub printHelp()
1674             {
1675             printUsage();
1676             print "Takes in lemmas from file and attempts to\n";
1677             print "insert them into WordNet by first finding\n";
1678             print "a hypernym, then either a) merging the \n";
1679             print "lemma with the hypernym or b) attaching \n";
1680             print "the lemma to the hypernym.\n";
1681             }
1682              
1683             1;