Perl:如何从字典文件中打印随机部分(字定义)

时间:2016-06-05 15:06:46

标签: perl

我想在Perl终端Unix的字典文件中打印一个随机的新单词English。我想选择并打印一条随机线和2条跟随线。

但是我的代码没有完成这项工作。

请帮助我改进它。

我希望的输出示例:

@inspire: ....

ghk

lko...

字典文件:

@inspiration: mean....

abc def...

ghk lmn
...

@inspire: ....

ghk

lko...

@people: ...

...

完整的字典文件在这里anhviet109K.txt。它大概是14MB

我的代码:

use strict;
use warnings;

use File::Copy qw(copy move);

my $files = 'anhviet109K.txt';
my $fh;
my $linewanted = 16 + int( rand( 513796 - 16 ) );

# 513796: number of lines of file dic.txt

open( $fh, "<", $files ) or die "cannot open < $fh: $!";

my $del   = " {2,}";
my $temp  = 0;
my $count = 0;

while ( my $line = <$fh> ) {

    if ( ( $line =~ "@" ) && ( $. > $linewanted ) ) {
        $count = 4;
    }
    else {
        next;
    }

    if ( $count > 0 ) {
        print $line;
        $count--;
    }
    else {
        last;
    }
}

close $fh;

2 个答案:

答案 0 :(得分:4)

也许是这样的事情?

您的数据帮助我排除了词典文件中的标题条目

此程序在文件中找到所有条目的位置(以@开头的行),然后随机选择一个并打印出来

TốthọctiếngAnhmaymắn

use strict;
use warnings 'all';

use Fcntl ':seek';

use constant FILE => 'anhviet109K.txt';

open my $fh, '<', FILE or die qq{Unable to open "@{[FILE]}" for input: $!};

my @seek;  # Locations of all the definitions

my $addr = tell $fh;
while ( <$fh> ) {
    push @seek, $addr if /^\@(?!00-)/;  
    $addr = tell $fh;
}

my $choice = $seek[rand @seek];

seek $fh, $choice, SEEK_SET;
print scalar <$fh>;
while ( <$fh> ) {
    last if /^\@/;
    print;
}

输出

@finesse /fi'nes/
*  danh từ
- sự khéo léo, sự phân biệt tế nhị
- mưu mẹo, mánh khoé
*  động từ
- dùng mưu đoạt (cái gì); dùng mưu đẩy (ai) làm gì; dùng mưu, dùng kế
=to finesse something away+ dùng mưu đoạt cái gì

答案 1 :(得分:2)

单程方式:

use strict;
use warnings;
use autodie;

open my $fh, '<:utf8', 'anhviet109K.txt';

my $definition = '';
my $count;
my $select;

while (my $line = <$fh>) {
    if ($line =~ /^@(?!00-)/) {
        ++$count;
        $select = rand($count) < 1;
        if ($select) {
            $definition = $line;
        }
    }
    elsif ($select) {
        $definition .= $line;
    }
}

# remove blank line that some entries have
$definition =~ s/^\s+\z//m;

binmode STDOUT, ':utf8';
print $definition;

这个迭代随机选择总是选择第一个项目,有1/2的机会用第二个项目替换它,1/3用于第三个项目,依此类推。