EvoTalk

Archive for the ‘Perl’ Category

Make the Same Dir Structure From Src to Dest
若有一來源路徑列表為
c:\corpus\maggie\123\01.pcm
c:\corpus\maggie\456\02.pcm
c:\corpus\maggie\789\03.pcm
想在 z:\speech 目錄下複製來源路徑相同的目錄結構,如
z:\speech\corpus\maggie\123\01.ulaw
z:\speech\corpus\maggie\456\02.ulaw
z:\speech\corpus\maggie\789\03.ulaw
view plain

PERL:

use File::Path;

#use File::Basename;

my $prefix = 'z:\corpus';

my $sublayer = 1;

open(F, shift) || die $!;

while(<F>)

{

    chomp;

    my @dirs = split(/\\/);

    @dirs = @dirs[$sublayer..$#dirs];

    my $pcm = pop @dirs;

    $dir = $prefix . "\\" . join("\\", @dirs);

    $pcm =~ [...]

24 十二月, 2007

Perl unicode 簡繁轉換

Posted by: asd In: Code Snippet| Perl| 程式設計

需事先安裝 Encode-HanConvert
view plain

PERL:

use utf8;

use Encode::HanConvert;

 

my ($simp, $trad, $fh_simp, $fh_trad);

 

$trad = simp_to_trad('简繁转换');

$simp = trad_to_simp($trad);

 

open($fh_simp, "&gt;:utf8", 'out_simp.txt');

open($fh_trad, "&gt;:utf8", 'out_trad.txt');

 

print $simp . "\n";

print $trad . "\n";

print $fh_simp $simp;

print $fh_trad $trad;

06 十二月, 2007

Web Service: Use Perl XML-RPC

Posted by: asd In: Code Snippet| Perl| 程式設計

範例包含 server、client,執行前先使用 ppm 安裝「Frontier-RPC」、「Frontier-Daemon-Forking」modules。
參考「用 XML-RPC 開發 Web 服務: 針對 Perl 的 XML-RPC 入門」
Server
view plain

PERL:

#!/usr/bin/perl

# sum() server

 

use strict;

use warnings;

use Frontier::Daemon;

 

my $d = Frontier::Daemon-&gt;new(

methods =&gt; {

sum =&gt; \&amp;sum,

},

LocalAddr =&gt; '127.0.0.1',

LocalPort =&gt; 1234,

);

 

sub sum {

my ($arg1, $arg2) = @_;

 

return $arg1 + $arg2;

}

Client
view plain

PERL:

#!/usr/bin/perl

# Testing sum()

 

use strict;

use warnings;

use Frontier::Client;

 

my $url  = "http://127.0.0.1:1234/RPC2";

my @args = (2,5);

 

my $client = Frontier::Client-&gt;new( [...]

Tags: ,

07 十一月, 2007

perl regex : grep

Posted by: asd In: Code Snippet| Perl

view plain

PERL:

#!/usr/bin/perl

# perl-grep3.pl

my $pattern = shift @ARGV;

#qr// is a regex quoting operator that stores my regex in a scalar

#qr// compiles the pattern so it ready to use

#eval operator around the qr// to catch the error

my $regex = eval { qr/$pattern/ };

die "Check your pattern! $@" if $@;

while( &lt;&gt; )

{

#The $&amp; variable holds the portion of [...]

Tags:

03 八月, 2007

Remove WAV File Header

Posted by: asd In: Code Snippet| Perl| 程式設計

此 script 的作用:批次移除 wav 檔的檔頭,產生 pcm 檔
使用方法
BatchRipWavHdr.pl WavLst.txt
WavLst.txt 為來源wav檔的路徑列表,使用前需先設定產生的 pcm 檔要放在哪個目錄「$dest_prefix」,若要依照來源目錄層次第 n 層來存放,可設定「$sublayer」
view plain

PERL:

#!/usr/bin/perl -w

#

#動態的為Wav檔求算出其Header的size

#

use File::Path;

if(scalar @ARGV!=1)

{

print "Usage: BatchRipWavHdr.pl WavLst.txt" ;

exit;

}

 

$dest_prefix='C:\Tel100_PCM';

#$dest_prefix='H:\Spelled_Spoken\BatchRipWavHeader';

$sublayer='1';

#$hdr='48'; #bytes

$number=0;

while(&lt;&gt;)

{

#Y:\cdrom\spelled_spoken\speech\alphabet\23\call2316.alphabet.wav

chomp;

$source=$_;

$hdr=44;#&amp;CheckHeaderByte($source);

@dir=split /\\/,$_;

@dir=@dir[$sublayer..$#dir];

$pcm=pop @dir;

$pcm=~s/wav$/pcm/i;

$dir=join '\\',@dir;

$dir.='\\';

$dir=$dest_prefix.'\\'.$dir if($dest_prefix ne '');

 

eval { mkpath($dir) };

if ($@) {

print "Couldn't create $dir: $@";

}

 

$dest=$dir.$pcm;

 

open SOURCE,"$source" or die "$!";

open DEST,"&gt;$dest" or die "$!";

seek SOURCE,$hdr,0;

while(sysread SOURCE,$buf,4096)

{

syswrite DEST,$buf,4096;

}

 

print ++$number,"\r";

}

 

sub CheckHeaderByte

{

my ($wav)=@_;

my($out,$buf,$data,$hdr);

open WAV,"$wav" or die "WAV:$!";

binmode [...]

Tags:

Page 2 of 612345...Last »