- 论坛徽章:
- 5
|
也许明天, try grep
now 试试这个脚本? 小伙伴们, 如果你不满意,请告诉我- #!/usr/bin/perl
- # README! newword.txt 每行字後面没有空格
- ####################################
- my $Dir = '/ok/xyz';
- my $save = '/ok/abc/myok.txt';
- my $newword = '/ok/abc/newword.txt';
- my $phone = '/ok/abc/phonetic.txt';
- #####################################
- open my $dic, $newword or die "$newword:\t$!";
- open my $pho, $phone or die "$phone:\t$!";
- my %dic = map { chomp; $_, [] } <$dic>;
- my @word = keys %dic;
- my %phone = map @$_, grep $dic{ $_->[0] }, map [split], <$pho>;
- sub findtxt {
- my $dir = shift;
- map { -d $_ ? findtxt($_) : /\.txt$/ ? $_ : () } glob "$dir/*";
- }
- for my $file ( findtxt $Dir) {
- open my $f, $file or die "$file:\t$!";
- print $file, "\n";
- my $data = do { local $/; <$f> };
- $data =~ s/\s+/ /g;
- my @data = split /(?<=,|\.|\?)/, $data;
- for my $w (@word) {
- for my $i ( 0 .. $#data ) {
- if ( $data[$i] =~ /\b$w/i ) {
- my ( $u, $d ) = ( $i - 1, $i + 1 );
- $u = $i if $u < 0;
- $d = $i if $d > $#data;
- my $s = join '', @data[ $u .. $d ];
- $s =~ s/\b$w/[ $w ]/ig;
- push @{ $dic{$w} }, "$w $phone{$w}\n$s\n# $file";
- }
- }
- }
- }
- open my $S, '>', $save or die "$save:\t$!";
- for my $k ( sort keys %dic ) {
- next unless @{ $dic{$k} };
- print $S join( "\n\n", @{ $dic{$k} } ), "\n\n";
- }
复制代码 回复 13# blackantt
|
|