Thread Anfängerfrage.. (11 answers)
Opened by liam21c at 2007-09-11 16:21

renee
 2007-09-11 16:32
#99333 #99333
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Ein paar Anmerkungen:
Code (perl): (dl )
1
2
3
4
#!/usr/local/bin/perl -w

use strict;
use warnings;


vorbildlich... Aber das -w kannst Du weglassen (Du benutzt ja "use warnings")

Code (perl): (dl )
1
2
3
4
5
6
7
my $miniparDir = "home/sat/minipar";
my @dataArray;
my $parsedMiniparOutput;
my $searchWord;
my %dependentWord;
my @stopwords;
my %is_stopwords;
man sollte Variablen so spät wie möglich (wenn's geht erst bei der ersten Verwendung) deklarieren

Code (perl): (dl )
1
2
3
4
5
6
7
8
$searchWord = $ARGV[0];

open(OPENSTOPW,'stopwords.lst');
my $var = 0;
while(<OPENSTOPW>){
        chomp;
        push @stopwords, ($_);
}

Man sollte immer mit angeben, ob die Datei zum Lesen oder zum Schreiben geöffnet wird und Fehler sollten abgefangen werden.

Man muss nicht alles in ein Array "pushen", man kann gleich alles in ein Array lesen...
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
foreach(@stopwords){
        $is_stopwords{$_} = 1;
}
close(OPENSTOPW);
open(OPENMY,'minipar.txt');

while(<OPENMY>){
        ...
        my($firstWord,$relation, $secondWord)=split(/:/,$_);
        my($first,$firstPOS)=split(/\s/,$firstWord);
        my($secondPOS,$second)=split(/\t/,$secondWord);
        ...
        if($is_stopwords{$second}){
                next;
        }
        
        my $entry = "$relation $firstPOS $second $secondPOS";
        $dependentWord{$entry}++;
        
}
close(OPENMY);
print %is_stopwords, "\n";


Schon mal überprüft, ob in $second wirklich das drin steht was Du erwartest??

Mit meinen Anmerkungen:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/local/bin/perl -w

use strict;
use warnings;

my $miniparDir = "home/sat/minipar";
my @dataArray;
my $parsedMiniparOutput;
my %dependentWord;
my %is_stopwords;

my $searchWord = $ARGV[0];

open(my $fh_stop,'<','stopwords.lst') or die $!;
while(my $line = <$fh_stop>){
        chomp $line;
        $is_stopwords{$line} = 1;
}
close $fh_stop;

open(my $fh,'<','minipar.txt') or die $!;
while(my $line = <$fh>){
        ...
        my($firstWord,$relation, $secondWord)=split(/:/,$line);
        my($first,$firstPOS)=split(/\s/,$firstWord);
        my($secondPOS,$second)=split(/\t/,$secondWord);
        ...
        if($is_stopwords{$second}){
                next;
        }
        
        my $entry = "$relation $firstPOS $second $secondPOS";
        $dependentWord{$entry}++;
        
}
close($fh);
print %is_stopwords, "\n";
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Anfängerfrage..