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 =~ [...]
需事先安裝 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, ">:utf8", 'out_simp.txt');
open($fh_trad, ">:utf8", 'out_trad.txt');
print $simp . "\n";
print $trad . "\n";
print $fh_simp $simp;
print $fh_trad $trad;
範例包含 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->new(
methods => {
sum => \&sum,
},
LocalAddr => '127.0.0.1',
LocalPort => 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->new( [...]
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( <> )
{
#The $& variable holds the portion of [...]
此 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(<>)
{
#Y:\cdrom\spelled_spoken\speech\alphabet\23\call2316.alphabet.wav
chomp;
$source=$_;
$hdr=44;#&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,">$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 [...]