Chinaunix

标题: 单词查找问题o-o(续) [打印本页]

作者: mingoing    时间: 2014-01-10 16:18
标题: 单词查找问题o-o(续)
本帖最后由 mingoing 于 2014-01-13 13:46 编辑

谢谢@只是一个红薯,@pitonas,尤其谢谢@jzp520520

cidian.zip

27.56 KB, 下载次数: 12


作者: jzp520520    时间: 2014-01-10 16:18
#!/usr/bin/perl
use strict;
use warnings;
no  strict 'refs';

#####将多个字典(dict1.txt dict2.txt)文件合并#####
open (my $fh1,"dict1.txt");
open (my $fh2,"dict2.txt");
open (my $fh3,">","newdict.txt");
my %h1;
while (<$fh1>) {
        chomp;
        my ($k,$v)=split /\s+/,$_,2;
        push @{$h1{$k}},$v;
       
}
while (<$fh2>) {
        chomp;
        my ($k,$v)=split /\s+/,$_,2;
        next if  $v~~@{$h1{$k}};
        push @{$h1{$k}},$v;
       
}

map {my $k=$_;print $fh3 map{"$k\t$_\n"}@{$h1{$_}};}sort keys %h1;
close($fh1);
close($fh2);
close($fh3);


open Afile,"WordList.txt";
open OOV,">","oov.txt";
open OOV1,">","oov1.txt";
while(<Afile>){
        chomp;       
         if (exists $h1{$_}) {
                 my $k=$_;
        print OOV1 map {"$k=>$_ \n"} @{$h1{$_}};
        }else{
        print OOV "$_\n";
        }
}
close(Afile);
close(OOV);
close(OOV1);

open Bfile,"tp6-6.txt";
open OOV2,">","oov2.txt";
open OOV3,">","oov3.txt";
while (<Bfile>) {
        while (/([a-zA-Z\']+)/g) {
                my $w=$1;
                print OOV2 "$w\t";
                 if (exists $h1{$w}) {
                print OOV3 map {"$w=>$_ \n"} @{$h1{$w}};
                }
        }
}

作者: jzp520520    时间: 2014-01-10 23:16
楼主的思维比较混乱,多理一下!
作者: pitonas    时间: 2014-01-11 16:28
真正动手搞起来又感觉特别吃力。{:2_169:}
作者: pitonas    时间: 2014-01-12 17:36
呵呵,暂时还未看懂,慢慢理解…{:2_169:}
作者: mingoing    时间: 2014-01-13 09:13
谢谢您,我仔细研究一下。

回复 2# jzp520520


   
作者: mingoing    时间: 2014-01-13 09:14
回复 5# pitonas


   
作者: mingoing    时间: 2014-01-13 09:54
这一行“next if  $v~~@{$h1{$k}};”报错,说是“语法错误”。回复 2# jzp520520


   
作者: 只是一个红薯    时间: 2014-01-13 10:28
perl 5.10.0可用智能匹配,不过有一些bug,perl 5.10.1修复了绝大多少bug,使用use 5.010001,或更高版本啊回复 8# mingoing


   
作者: mingoing    时间: 2014-01-13 13:07
我用的文本编辑器回复 9# 只是一个红薯


   
作者: mingoing    时间: 2014-01-13 13:42
弄好了,同时也谢谢您。回复 9# 只是一个红薯


   
作者: pitonas    时间: 2014-01-13 20:47
谢谢!
比较茫然 {:2_175:}





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2