Chinaunix

标题: GUI表格操作 [打印本页]

作者: perllover007    时间: 2014-05-23 08:44
标题: GUI表格操作
本帖最后由 perllover007 于 2014-05-23 08:45 编辑

写了一段代码,操作两个文本,想实现通过检索关键词在表格相邻两行显示搜索到的英汉语对应的句子,但是结果只显示汉语文本,请问什么原因?
use strict;
use warnings;
use UTF8;
use Win32::GUI qw(DT_RIGHT DT_LEFT DT_CENTER DT_VCENTER);
use Win32::GUI::Grid qw(GVS_DATA);
my $main = Win32::GUI::Window->new(
    -name => 'Main',
    -title => "arallel Corpus",
    -pos => [ 0, 0 ],
    -size  => [ 1200, 840 ],
);

$main->AddLabel(
    -text  => "lease fill the search word in the following blank",
        -top   => 20,
        -align => 'center',
        -left  => 35,
        width  => 300,
        height => 30,
);
my $f1=$main->AddTextfield(
    -name   => "searchword",
    -align => 'center',
        -left   => 20,
    -top    => 60,
    -width  => 160,
    -height => 30,
    );

$main->AddButton(
-name => "search",
-text => "GO",
-pos  => [ 190, 60 ],
-width  => 110,
-height => 30,
-ok  => 1,
);

my $grid=$main->AddGrid(
                                -name      => 'grid',
                                -left      => 20,
                                -top       => 90,
                                -width     => 1120,
                                -height    => 560,
                                -rows      => 1,
                                -columns   => 2,
                                -fixedrows => 1,
                                );
$grid->SetRows(1);
$grid->SetCellText(0, 0, 'No.');
$grid->SetCellFormat(0, 1, DT_CENTER);
$grid->SetCellText(0, 1, 'Result');

$grid->SetColumnWidth(0, 50);
$grid->SetListMode();
$grid->SetFixedColumnSelection(0);

$main->Show();
Win32::GUI:ialog();
exit(0);       
sub search_Click{
open(ZH,"ZH.txt";
my @zh= <ZH>;
chomp(@zh);
open(EN,"EN.txt";
my @en= <EN>;
chomp(@en);
close ZH;
close EN;
foreach my $i(0..$#zh){
        my $j=$i+1;
        my %match;
        $match{$zh[$i]}=$en[$i];
        my $node = $main->searchword->Text();
                if ( $en[$i] =~ /$node/i)

                                {
                     
                  my $row = $grid->GetRows();
                    $grid->InsertRow(-1);
                    $grid->SetCellText($j, 0, $j);
                    $grid->SetCellText($j, 1, $en[$i]);
                    
                    $grid->InsertRow(-2);
                    $grid->SetCellText($j, 0, $j);
                    $grid->SetCellText($j, 1, $zh[$i]);

                }

谢谢!

在线等您回复!

作者: perllover007    时间: 2014-05-23 14:35
本帖最后由 perllover007 于 2014-05-23 15:39 编辑

有坛友告诉我没有讲清楚目的,现在我用数据描述一下:

我现在两个文本,内容如下:
文本  ZH.txt;
一、过去五年工作回顾
第十一届全国人民代表大会第一次会议以来的五年,是我国发展进程中极不平凡的五年。
我们有效应对国际金融危机的严重冲击,保持经济平稳较快发展。
国内生产总值从26.6万亿元增加到51.9万亿元,跃升到世界第二位;
公共财政收入从5.1万亿元增加到11.7万亿元;
累计新增城镇就业5870万人,

文本   EN.txt:
I. Review of Work in the Past Five Years
The past five years since the First Session of the Eleventh National People's Congress were a truly extraordinary period of time in the course of China's development.
We effectively countered the severe impact of the global financial crisis and maintained steady and fast economic development.
China's GDP increased from 26.6 trillion yuan to 51.9 trillion yuan, and now ranks second in the world.
Government revenue went up from 5.1 trillion yuan to 11.7 trillion yuan.
A total of 58.7 million urban jobs were created.

我想利用win32::GUI的制作一个检索程序,通过检索关键词,如:五年,程序就把EN.txt 和 ZH.txt两个文本中第一行
同时提取出来,可以放到一行两列里,或者相邻的两行:

一、过去五年工作回顾
I. Review of Work in the Past Five Years

或者:
一、过去五年工作回顾        I. Review of Work in the Past Five Years

请教高人指点,thx!
作者: jason680    时间: 2014-05-23 15:14
回复 2# perllover007

There is the sample code for you that read two files data into one array

$ cat read_2f.pl

use strict;
use warnings;

my $sFzh = "ZH.txt";
my $sFen = "EN.txt";

open(FHzh, "<", $sFzh) or die "can't open $sFzh file\n";
open(FHen, "<", $sFen) or die "can't open $sFen file\n";

my @aData;
while(<FHzh>){
  # chomp;     # if you don't want the newline "\n"
  $_ .= <FHen>;
  push @aData, $_;

}

# now, you get the two files data on @aData array
my $sCnt = 0;
foreach(@aData){
  $sCnt++;
  print "Data $sCnt:\n$_\n";
}

$ perl read_2f.pl
Data 1:
一、过去五年工作回顾
I. Review of Work in the Past Five Years

Data 2:
第十一届全国人民代表大会第一次会议以来的五年,是我国发展进程中极不平凡的五年。
The past five years since the First Session of the Eleventh National People's Congress were a truly extraordinary period of time in the course of China's development.

Data 3:
我们有效应对国际金融危机的严重冲击,保持经济平稳较快发展。
We effectively countered the severe impact of the global financial crisis and maintained steady and fast economic development.

Data 4:
国内生产总值从26.6万亿元增加到51.9万亿元,跃升到世界第二位;
China's GDP increased from 26.6 trillion yuan to 51.9 trillion yuan, and now ranks second in the world.

Data 5:
公共财政收入从5.1万亿元增加到11.7万亿元;
Government revenue went up from 5.1 trillion yuan to 11.7 trillion yuan.

Data 6:
累计新增城镇就业5870万人,
A total of 58.7 million urban jobs were created.


作者: perllover007    时间: 2014-05-23 15:38
非常感谢细心的回复,不过我需要的是利用GUI,实现搜索功能!把搜索的结果填入表格。不知道这样说,够清楚吗?
作者: perllover007    时间: 2014-05-23 15:52
非常感谢!能否指教GUI代码?回复 3# jason680


   
作者: jason680    时间: 2014-05-23 16:00
回复 4# perllover007

>> ...但是结果只显示汉语文本...

you said: it already had searched function but need show more data ...

   
作者: perllover007    时间: 2014-05-23 16:38
的确是可以搜索了,但出不来我想要的结果,我也搞不清楚哪个地方出了问题,能帮忙给我审核一下我的代码吗?
sub search_Click{
open(ZH,"ZH.txt";
my @zh= <ZH>;
chomp(@zh);
open(EN,"EN.txt";
my @en= <EN>;
chomp(@en);
close ZH;
close EN;
foreach my $i(0..$#zh){
        my $j=$i+1;
        my %match;#设置哈希,建立中英文的对应;
        $match{$zh[$i]}=$en[$i];
        my $node = $main->searchword->Text();#读取搜索词;
                if ( $en[$i] =~ /$node/i)如果英文文本某一行匹配到搜索词;

                                {
                     
                  my $row = $grid->GetRows();
                    $grid->InsertRow(-1);
                    $grid->SetCellText($j, 0, $j);
                    $grid->SetCellText($j, 1, $en[$i]);#把保护搜索词的那一行英文句子填入表格最后一行的第二列;
                    
                    $grid->InsertRow(-2);
                    $grid->SetCellText($j, 0, $j);
                    $grid->SetCellText($j, 1, $zh[$i]);#把与英文句子对应的汉语句子填入表格倒数第二行的第二列;

                }

以上注释是我的思路,请你在你电脑上运行一下我的程序吧,麻烦看看哪儿出了问题?苦恼了我一天了!!谢谢!!!!
回复 6# jason680


   
作者: jason680    时间: 2014-05-23 16:39
回复 7# perllover007

it bad code to run ....

open(ZH,"ZH.txt";
   
作者: perllover007    时间: 2014-05-23 17:16
本帖最后由 perllover007 于 2014-05-23 17:17 编辑

这个是由于表情符号导致的,原来没有这个错误,麻烦在你自己电脑上修改一下,看看运行结果。谢谢!

在我电脑运行的时候,输入字母会显示中文句子,但是英文句子无法显示。同时还有空行,不知道什么原因?


回复 8# jason680


   




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