我目前正在研究將某些單詞改爲莎士比亞單詞的代碼。我必須提取包含單詞的句子並將它們打印到另一個文件中。我必須從每個文件的開頭刪除.START。如何使用計數器查找單詞的位置?
首先我用空格分隔文本文件,所以現在我有文字。接下來,我通過散列迭代單詞。哈希鍵和值來自制表符分隔的文件,其結構如下OldEng/ModernEng(lc_Shakespeare_lexicon.txt
)。現在,我試圖找出如何找到每個現代英語單詞的確切位置,將其改爲莎士比亞;然後用改變詞找到句子並將它們打印到不同的文件中。除了最後一部分外,大部分代碼都已完成。這是我到目前爲止的代碼:
#!/usr/bin/perl -w
use diagnostics;
use strict;
#Declare variables
my $counter=();
my %hash=();
my $conv1=();
my $conv2=();
my $ssph=();
my @text=();
my $key=();
my $value=();
my $conversion=();
my @rmv=();
my $splits=();
my $words=();
my @word=();
my $vals=();
my $existingdir='/home/nelly/Desktop';
my @file='Sentences.txt';
my $eng_words=();
my $results=();
my $storage=();
#Open file to tab delimited words
open (FILE,"<", "lc_shakespeare_lexicon.txt") or die "could not open lc_shakespeare_lexicon.txt\n";
#split words by tabs
while (<FILE>){
chomp($_);
($value, $key)= (split(/\t/), $_);
$hash{$value}=$key;
}
#open directory to Shakespearean files
my $dir="/home/nelly/Desktop/input";
opendir(DIR,$dir) or die "can't opendir Shakespeare_input.tar.gz";
#Use grep to get WSJ file and store into an array
my @array= grep {/WSJ/} readdir(DIR);
#store file in a scalar
foreach my $file(@array){
#open files inside of input
open (DATA,"<", "/home/nelly/Desktop/input/$file") or die "could not open $file\n";
#loop through each file
while (<DATA>){
@text=$_;
chomp(@text);
#Remove .START
@rmv=grep(!/.START/, @text);
foreach $splits(@rmv){
#split data into separate words
@word=(split(/ /, $splits));
#Loop through each word and replace with Shakespearean word that exists
$counter=0;
foreach $words(@word){
if (exists $hash{$words}){
$eng_words= $hash{$words};
$results=$counter;
print "$counter\n";
$counter++;
#create a new directory and store senteces with Shakespearean words in new file called "Sentences.txt"
mkdir $existingdir unless -d $existingdir;
open my $FILE, ">>", "$existingdir/@file", or die "Can't open $existingdir/conversion.txt'\n";
#print $FILE "@words\n";
close ($FILE);
}
}
}
}
}
close (FILE);
close (DIR);
你可以發佈一些輸入數據嗎? – fugu
通過在變量被需要之前聲明變量,你會失去'my'的一些好處。此外,所有這些作業(除了'my $ existingdir ='/ home/nelly/Desktop'; my @ file ='Sentences.txt';')都沒用。 – ikegami
最有可能你會使用'索引''pos'等 - 就像這個[類似的SO問題(看看答案)](http://stackoverflow.com/a/4856558/2019415) 。我不知道你是否正確設置了查找'%hash'。嘗試使用['Data :: Dumper'](https://metacpan.org/pod/Data::Dumper)或['Data :: Printer'](https://metacpan.org/release/Data-Printer)看看它是如何填寫的。 –