用perl读取txt文件(英文诗) 统计单词数量 以及频率 并且指出出现位置(第几行 几个)
发布网友
发布时间:2022-05-19 23:12
我来回答
共1个回答
热心网友
时间:2023-10-19 23:54
这问题让人很纠结... 看上去很有意思, 但写了出来又怕你看不懂... 只管写出来给你参巧下吧 , 顺便推介一下, Katie Melua 的 A Happy Place 真的很好听, MV 也很有感觉...
use strict;
use warnings;
my ($lineCount, $totalWords, %map);
while ( my $line = <DATA> ) {
chomp $line;
$lineCount++;
$line =~ s/[^\w\d' ]//g;
my $wordPos;
foreach my $word ( split /\s+/, $line ) {
$word = lc $word;
$wordPos++;
$totalWords++;
$map{$word} = [] unless ( defined $map{$word} ) ;
my $this = $map{$word};
push @{$this}, "$lineCount-$wordPos" ;
}
}
print "There are total $totalWords in this context$/";
foreach my $word ( sort keys %map ) {
my $count = scalar ( @{$map{$word}} );
print "$word existed $count times, where they are at$/";
foreach my $index ( @{$map{$word}} ) {
my ( $ln, $po ) = split /-/, $index;
print "\tLine: $ln, Word position: $po$/";
}
print "$/";
}
__DATA__
"A Happy Place"
I'm going to find a happy
I'm going to find a happy
I'm going to find a happy place
Seven thousand eyes are watching
Marching home and no one's touching
Army of the city workers
Secretaries, lawyers, brokers
Heading for a London station
Heading for a quick salvation
Oblivious to cherry cola
Trying to sell to every stranger
Stuck here it's cold I'm standing
Hoping for some understanding
Only way to go is inside
I'm going to find a happy
I'm going to find a happy
I'm going to find a happy place
Seven thousand light years travel
Let my sense of time unravel
Trapped feelings are never ending
Send those weaknesses descending
Towards the earth's red centre
Let the fire rise and enter
Tap into the primal power
Rising like a giant tower
The energy receiving
Cut a hole right through the ceiling
People getting smaller as you fly
I'm going to find a happy
I'm going to find a happy
I'm going to find a happy place
(I'm gonna find a happy place) A happy Place
(I'm gonna find a happy place) A happy place
(I'm gonna find a happy place) A happy place
Find a star, send down a beam to where you are
An elevator of light taking you somewhere
Where you'll always be loved
I have found that stress and nonsense
Puts me in zone of avoidance
Could my mind be moving faster
Pulling like a super cluster
Can be hard to trust a feeling
But believing ends in seeing
We're going to find a happy
We're going to find a happy
We're going to find a happy place
(I'm gonna find a happy place) A happy place
(I'm gonna find a happy place) A happy place
(I'm gonna find a happy place) A happy place
追问很谢谢你的程序和你的推荐~~~不过说真的 我真的没有很看懂 不过我在努力看 非常感谢你 我也会去听你推荐的歌曲~~~~
追答
我这里是将文本和代码合二为一, 旨在给你看到我的思路和流程, 所以你会见到我 read 的只是 <DATA> 而 DATA 就是这代码最後的部份 ( __DATA__ ). 在实际操作里, 你或者会写成
my ($lineCount, $totalWords, %map);
my $file = 'D:/Poem.txt" ; # 将这里改成你档案的位置
open Src, $file or die "Can't open file at '$file'"; # 加了这句
while ( my $line = <Src> ) { # 由 DATA 改成 Src
chomp $line;
....
....
}
close Src; # 别忘了
print "There are total $totalWords in this context$/";
...
...
# __DATA__ 和之後的那些就可不要了
还有不明白再私讯我吧... 你也可以看看我之前回答过的问题, 某些地方或会有点近似...