Hello again,
thanks a lot for the recommendation with the threads... I still have one problem. I am more or less a Newbi on Perl and therefore I have no idea how to solve this problem: The app we have vectorize words from textfiles. The problem is that we get too much words. So the idea is (the app count the words within an file) just to take 10 % of the most frequent words of a file.
But how to do??
The part of interest of the code is:
and the whole code is:
Thanks a lot for helping and best wishes,
Stephan
thanks a lot for the recommendation with the threads... I still have one problem. I am more or less a Newbi on Perl and therefore I have no idea how to solve this problem: The app we have vectorize words from textfiles. The problem is that we get too much words. So the idea is (the app count the words within an file) just to take 10 % of the most frequent words of a file.
But how to do??
The part of interest of the code is:
Code:
$textcnt = 0; #Zaehlvariable fuer Name der Ausgabedatei
####################################
# absolute Wortliste wird erstellt #
####################################
foreach $datei (@dir){
next if (($datei eq ".") || ($datei eq "..") || ($datei eq ".directory"));
chomp $datei;
open (F, "AusgabenTagger/$datei") || die ("cant open $*datei"); #hier muss das Directory angegeben werden!
while ($line = <F>) {
&lemmabereinigen;
$wortliste{$lemma} = 0;
}
}
$xcnt = 0;
foreach $wort(keys(%wortliste)){
$absowortliste[$xcnt] = $wort; #erstellt Wort-array aus Wort-hash zum Vergleichen
$xcnt++;
}
open (WORTLISTE,">Wortliste.txt");
chomp (@absowortliste);
@sortwortliste = sort(@absowortliste);
foreach $wort (@sortwortliste){ #schreibt die Wortliste in Wortliste.txt
print WORTLISTE "$wort\n";
}
###############################################
# Wortliste fuer jede Textdatei wird erstellt #
###############################################
mkdir ("Vergleichshashes");
foreach $datei (@dir){
next if (($datei eq ".") || ($datei eq "..") || ($datei eq ".directory"));
chomp $datei;
open (F, "AusgabenTagger/$datei") || die ("cant open $datei"); #hier muss das Directory angegeben werden!
open (AUSGABE1,">>Ausgabevektoren");
open (AUSGABE2,">Vergleichshashes/$datei");
$lcnt = 0;
while ($line = <F>) {
&lemmabereinigen;
$lemmaliste[$lcnt] = $lemma;
$lcnt++;
}
$lcnt = 0;
unshift @lemmaliste, @absowortliste;
@sortlemmaliste = sort (@lemmaliste);
$vergleichslemma = $lemma;
$wortanzahl = -1;
$vcnt = 0;
undef(%vergleichshash);
$rcnt = 0;
foreach $lemma (@sortlemmaliste){
$vergleichshash{$lemma}++;
if ($lemma eq $vergleichslemma){
$wortvektor[$vcnt] = ++$wortanzahl;
if ($wortanzahl == 1){
if ($labelhash{$lemma}){
$relliste[$rcnt] = $lemma;
$rcnt++;
}
}
}
else {
$vcnt++;
$wortanzahl = -1;
$wortvektor[$vcnt] = ++$wortanzahl;
$vergleichslemma = $lemma;
}
}
foreach $anzahl (@wortvektor){
print AUSGABE1 "$anzahl "; #schreibt den Vektor
}
foreach $rellemma(@relliste){
print AUSGABE1 "$rellemma "; #schreibt als Label die relevanten Woerter
}
print AUSGABE1 "$datei\n";
foreach $haeufigkeit(sort keys(%vergleichshash)){
$vergleichshash{$haeufigkeit}-=1;
print AUSGABE2 "$vergleichshash{$haeufigkeit}\;"; #schreibt Haeufigkeitsliste in jeweilige Datei
}
undef(@lemmaliste);
undef(@sortlemmaliste);
undef(%vergleichshash);
undef(@wortvektor);
undef(@relliste);
and the whole code is:
Code:
#! /usr/bin/perl
opendir (DR,"Dia-o-Kom") || die ("cant open Texte");
mkdir ("AusgabenTagger");
chdir ("Dia-o-Kom");
foreach (<*>){
$textdatei = $_;
next if (($textdatei eq ".") || ($textdatei eq "..") || ($textdatei eq ".directory"));
$tmp=join('_', split(/ /,$textdatei));
rename($textdatei, $tmp);
$textdatei=$tmp;
open (T,"$textdatei") || die ("cant open $textdatei");
system ("../Tagger/cmd/tree-tagger-english $textdatei > ../AusgabenTagger/$textdatei");
print "$textdatei\n";
}
chdir ("..");
#opendir (DIR, $eingabe) || die ("cant open indirectory");
opendir (DIR,"AusgabenTagger") || die ("cant open AusgabenTagger");
while ($datei = readdir(DIR)){ #erstellt Array aus Directory-eintraegen
$datei = $datei;
$dir[$cnt] = $datei;
$cnt++;
}
chomp @dir;
open(R, "<Relevante2") || die ("Cannot open Relevante\n");
@labelliste = <R>;
chomp (@labelliste);
foreach $label (@labelliste) {
$labelhash{$label} = 1;
}
open(S, "<stoppwort.txt") || die ("Cannot open stoppwort_txt\n"); #erstellt Stoppwortliste aus stoppwort.txt
@stoppwortliste = <S>;
chomp (@stoppwortliste);
foreach $stoppwort (@stoppwortliste) {
$stoppworthash{$stoppwort} = 1;
}
$textcnt = 0; #Zaehlvariable fuer Name der Ausgabedatei
####################################
# absolute Wortliste wird erstellt #
####################################
foreach $datei (@dir){
next if (($datei eq ".") || ($datei eq "..") || ($datei eq ".directory"));
chomp $datei;
open (F, "AusgabenTagger/$datei") || die ("cant open $*datei"); #hier muss das Directory angegeben werden!
while ($line = <F>) {
&lemmabereinigen;
$wortliste{$lemma} = 0;
}
}
$xcnt = 0;
foreach $wort(keys(%wortliste)){
$absowortliste[$xcnt] = $wort; #erstellt Wort-array aus Wort-hash zum Vergleichen
$xcnt++;
}
open (WORTLISTE,">Wortliste.txt");
chomp (@absowortliste);
@sortwortliste = sort(@absowortliste);
foreach $wort (@sortwortliste){ #schreibt die Wortliste in Wortliste.txt
print WORTLISTE "$wort\n";
}
###############################################
# Wortliste fuer jede Textdatei wird erstellt #
###############################################
mkdir ("Vergleichshashes");
foreach $datei (@dir){
next if (($datei eq ".") || ($datei eq "..") || ($datei eq ".directory"));
chomp $datei;
open (F, "AusgabenTagger/$datei") || die ("cant open $datei"); #hier muss das Directory angegeben werden!
open (AUSGABE1,">>Ausgabevektoren");
open (AUSGABE2,">Vergleichshashes/$datei");
$lcnt = 0;
while ($line = <F>) {
&lemmabereinigen;
$lemmaliste[$lcnt] = $lemma;
$lcnt++;
}
$lcnt = 0;
unshift @lemmaliste, @absowortliste;
@sortlemmaliste = sort (@lemmaliste);
$vergleichslemma = $lemma;
$wortanzahl = -1;
$vcnt = 0;
undef(%vergleichshash);
$rcnt = 0;
foreach $lemma (@sortlemmaliste){
$vergleichshash{$lemma}++;
if ($lemma eq $vergleichslemma){
$wortvektor[$vcnt] = ++$wortanzahl;
if ($wortanzahl == 1){
if ($labelhash{$lemma}){
$relliste[$rcnt] = $lemma;
$rcnt++;
}
}
}
else {
$vcnt++;
$wortanzahl = -1;
$wortvektor[$vcnt] = ++$wortanzahl;
$vergleichslemma = $lemma;
}
}
foreach $anzahl (@wortvektor){
print AUSGABE1 "$anzahl "; #schreibt den Vektor
}
foreach $rellemma(@relliste){
print AUSGABE1 "$rellemma "; #schreibt als Label die relevanten Woerter
}
print AUSGABE1 "$datei\n";
foreach $haeufigkeit(sort keys(%vergleichshash)){
$vergleichshash{$haeufigkeit}-=1;
print AUSGABE2 "$vergleichshash{$haeufigkeit}\;"; #schreibt Haeufigkeitsliste in jeweilige Datei
}
undef(@lemmaliste);
undef(@sortlemmaliste);
undef(%vergleichshash);
undef(@wortvektor);
undef(@relliste);
}
sub lemmabereinigen {
if ($line =~ /(<.*>)/){
$line =~ s/<.*>//g; #loescht sgml-tags
}
if ($line =~ /\[.*\]/){
$line =~ s/\[.*\]//g; #loescht trl-tags
}
if ($line =~ /[^\d\w"\+¸ˆ‰‹÷ƒ?\s]/){
$line =~ s/[^\d\w"\+¸ˆ‰‹÷ƒ?\s]//g; #loescht Sonderzeichen
}
if ($line =~ /[\w"+¸?‰ˆ‹ƒ÷\d-]*[\s]*[\w"+¸¸ˆ‰?‹÷ƒ\d-]*[\s]*(.*)/ ){ #matcht nur das Lemma
$lemma = $1;
chomp ($lemma);
next if (length("".$lemma."") == 1); #entfernt einzelne Buchstaben
next if (($lemma =~ /\d/) && ((length("".$lemma."") != 4)));#entfernt Zahlen ausser Jahreszahlen
# next if ($labelhash{$lemma} != 1);
next if ($stoppworthash{$lemma} == 1);
}
}
Thanks a lot for helping and best wishes,
Stephan