2012-04-13 19 views
1
.i 1 
.t 
effici machineindepend procedur 
garbag collect variou list structur 
.w 
method return regist free 
list essenti part list process 
system. paper past solut recoveri 
problem review compar. new algorithm 
present offer signific advantag speed 
storag util. routin implement 
algorithm written list languag 
     insur degre 
machin independ. final applic 
algorithm number differ list structur 
appear literatur indic. 
.b 
cacm august 1967 
.a 
schorr h. 
wait w. m. 
.n 
ca670806 jb februari 27 1978 428 pm 
.x 
1024 4 1549 

1024 4 1549 

1050 4 1549 

.i 2 
.t 
comparison batch process instant turnaround 
.w 
studi program effort student 
    introductori program cours present 
    effect have instant turnaround minut 
oppos convent batch process 
turnaround time hour examin. 
item compar number comput 
run trip comput center program prepar 
time keypunch time debug time 
number run elaps time run 
    run problem. 
result influenc fact bonu point 
given complet program problem 
    specifi number run 
evid support instant batch. 
.b 
cacm august 1967 
.a 
smith l. b. 
.n 
ca670805 jb februari 27 1978 432 pm 
.x 
1550 4 1550 

1550 4 1550 

1304 5 1550 

1472 5 1550 

現在,上面的文字是2個文件,這是雙方停止,朵朵的內容,新的文件從.I(後跟一個數字)開始的話需要做的在.t & .b,.b & .a,.a & .n,.n & .x之間的文本中索引文本,並忽略.x和新文檔開始之間的所有文本。即I(後跟一個數字)如何索引的所有獨特的語料用perl

所有文件的內容都存儲在一個文件中,稱爲「語料庫」。需要對它們出現在語料庫和每個文檔中的次數進行索引,可能是文檔中的哪些位置。

open FILE, '<', 'sometext.txt' or die $!; 
my @texts = <FILE>; 
foreach my $text(@texts) { 
     my @lines = split ("\n",$text); 
     foreach my $line(@lines) { 
      my @words = split (" ",$text); 
      foreach my $word(@words) { 
       $word = trim($word); 
       my $match = qr/$word/i; 

       open STFILE, '<', 'sometext.txt' or die $!; 
       my $count=0; 

       while (<STFILE>) { 
        if ($_ =~ $match) { 
         my @mword = split /\s+/, $_; 
         $_ =~ s/[A-Za-z0-9_ ]//g; 
         for my $i (0..$#mword) { 
          if ($mword[$i] =~ $match) { 
           #print "match found on line $. word ", $i+1,"\n"; 
           $count++ 
          } 
         } 
        } 
       } 
       print "$word appears $count times \n"; 
       close(STFILE) or die "Couldn't close $file: $!\n\n"; 
      } 
     } 
    } 


    close(FILE) or die "Couldn't close $file: $!\n\n"; 

    sub trim($) 
{ 
    my $string = shift; 
    $string =~ s/^\s+//; 
    $string =~ s/\s+$//; 
    return $string; 
} 

上述代碼計算語料庫中每個詞的出現次數。 如何更改它,以便它也計算單個文檔中的單詞的發生。

+0

'$ ++計數{$詞}' – ikegami 2012-04-13 21:31:49

回答

2

如何:

編輯添加不同的計數器對每個文檔:

#!/usr/bin/perl 
use strict; 
use warnings; 
use Data::Dumper; 

my $words; 
my $doc; 
my $file = 'path/to/file'; 
open my $fh, '<', $file or die "unable to open '$file' for reading:$!" 
while(<$fh>) { 
    chomp; 
    $doc = $_ if /^\.i/; 
    next if (/^\.x\b/ .. /^\.i\b/); 
    next if /^\./; 
    my @words = split; 
    for(@words) { 
     $words->{$_}{$doc}++; 
    } 
} 
close $fh; 
print Dumper $words; 
+0

上面的代碼工作正常計算的話的頻率,但我需要計算每個文檔中單詞的頻率。例如: - 「apple」在doc1中出現3次,在doc5中出現4次,在doc8中出現7次。等等... – lolla 2012-04-14 15:10:51

+0

@ArpitaDuppala:看我的編輯。 – Toto 2012-04-15 09:01:48

+0

我拿出了上面的代碼在文件中。但我想使用自卸車中的值,如何提取/訪問它? – lolla 2012-04-17 03:18:26

1

使用散列,散列值包含每個單詞的當前計數。循環所有的行和所有的單詞。使用啞(基於標誌變量)的狀態機忽略.t和.b之間的文本

如果您在編寫上述任何代碼時遇到困難,請發佈有關您卡住的具體問題。

相關問題