#!/bin/perl
my($ansFile ,$result) = @ARGV;
if($#ARGV < 0 )
{
print "usage:perl count_time.pl time.txt \n";
exit(-1);
}
open(frecOut, ">result_time.txt") or die "3\n";
open (ansf1, "$ansFile") or die "8\n";
#以下代码统计答案中的语音总时长
$answerflag=0;
$position1=0;
$dataflag1=0;
$answer_total=0; #单个文件统计时间
$answer_total_per=0; #单个文件统计时间
$count=0;
$pre1;
$pre21;
$answer_time1;
$answer_time2;
$position_change1;
print "initialization position is $position1\n";
while($ansfline1=<ansf1>) #读完一行位置已经是下一行的开始
{
#$pre1=$position1;
#$position1 = tell(ansf1);
chomp($ansfline1);
if ($ansfline1 eq "\"data\"")
{
$dataflag1=1;
#print "ansfline1 is $ansfline1\n";
$position1 = tell(ansf1);
print "begin position is $position1\n";
next;
}
$pre1=$position1;
$position1 = tell(ansf1);
if($dataflag1==1)
{
print "position is $position1\n";
#print "dataflag1 is $dataflag1 and line is $ansfline1\n";
if($answerflag==0)
{
if ( !( $ansfline1 =~ /\"/ ))
{
$answerflag=1;
$answer_time1=$ansfline1;
$pre21=tell(ansf1);
}
}
elsif($answerflag==1)
{
if ( ! ( $ansfline1=~/\"/ ))
{
$answerflag=2;
$answer_time2=$ansfline1;
}
else
{
#$position_minus=$position-$pre;
seek(ansf1, $pre1, 0); #回退1行
$position_change1 = tell(ansf1);
$position = tell(ansf1);
print "position minus 1 is $position_change1\n";
$answerflag=0;
}
}
elsif($answerflag==2)
{
if($ansfline1=~ /[\x80-\xFF]+?/)
#if($ansfline1=~/\"/)
{
#print "compute\n";
$count++;
#print "$count $ansfline1\n";
$answer_total+=$answer_time2-$answer_time1;
#print(frecOut "find $resfline in $resFile is not match in $ansFile\n");
}
else
{
seek(ansf1, $pre21, 0); #回退两行
$position_change1 = tell(ansf1);
$position = tell(ansf1);
print "position minus 2 is $position_change1\n";
}
$answerflag=0;
}
#if( !( $ansfline=~/"/ ) )
#{
# $answer_time1=$ansfline;
#}
}
}
$answer_total_per=$answer_total;
print "times is $count\n";
print(frecOut "times is $count\n");
print "answer_total_per time is $answer_total_per\n";
print(frecOut "answer_total_per time is $answer_total_per\n");
close(ansf1);
my($ansFile ,$result) = @ARGV;
if($#ARGV < 0 )
{
print "usage:perl count_time.pl time.txt \n";
exit(-1);
}
open(frecOut, ">result_time.txt") or die "3\n";
open (ansf1, "$ansFile") or die "8\n";
#以下代码统计答案中的语音总时长
$answerflag=0;
$position1=0;
$dataflag1=0;
$answer_total=0; #单个文件统计时间
$answer_total_per=0; #单个文件统计时间
$count=0;
$pre1;
$pre21;
$answer_time1;
$answer_time2;
$position_change1;
print "initialization position is $position1\n";
while($ansfline1=<ansf1>) #读完一行位置已经是下一行的开始
{
#$pre1=$position1;
#$position1 = tell(ansf1);
chomp($ansfline1);
if ($ansfline1 eq "\"data\"")
{
$dataflag1=1;
#print "ansfline1 is $ansfline1\n";
$position1 = tell(ansf1);
print "begin position is $position1\n";
next;
}
$pre1=$position1;
$position1 = tell(ansf1);
if($dataflag1==1)
{
print "position is $position1\n";
#print "dataflag1 is $dataflag1 and line is $ansfline1\n";
if($answerflag==0)
{
if ( !( $ansfline1 =~ /\"/ ))
{
$answerflag=1;
$answer_time1=$ansfline1;
$pre21=tell(ansf1);
}
}
elsif($answerflag==1)
{
if ( ! ( $ansfline1=~/\"/ ))
{
$answerflag=2;
$answer_time2=$ansfline1;
}
else
{
#$position_minus=$position-$pre;
seek(ansf1, $pre1, 0); #回退1行
$position_change1 = tell(ansf1);
$position = tell(ansf1);
print "position minus 1 is $position_change1\n";
$answerflag=0;
}
}
elsif($answerflag==2)
{
if($ansfline1=~ /[\x80-\xFF]+?/)
#if($ansfline1=~/\"/)
{
#print "compute\n";
$count++;
#print "$count $ansfline1\n";
$answer_total+=$answer_time2-$answer_time1;
#print(frecOut "find $resfline in $resFile is not match in $ansFile\n");
}
else
{
seek(ansf1, $pre21, 0); #回退两行
$position_change1 = tell(ansf1);
$position = tell(ansf1);
print "position minus 2 is $position_change1\n";
}
$answerflag=0;
}
#if( !( $ansfline=~/"/ ) )
#{
# $answer_time1=$ansfline;
#}
}
}
$answer_total_per=$answer_total;
print "times is $count\n";
print(frecOut "times is $count\n");
print "answer_total_per time is $answer_total_per\n";
print(frecOut "answer_total_per time is $answer_total_per\n");
close(ansf1);
close(frecOut);
test.txt 文本
"data"
0
80.384
25
0
10.477281604089555
"*"
10.477281604089555
11.25975
"喂"
11.25975
13.572678224864765
"*"
13.572678224864765
14.642124999999998
"啊什么事啊"
14.642124999999998
24.293374999999998
75.759
76.746
"哦好谢谢啊"
76.746
78.403625
"嗯那就不打扰了再见"
78.403625
79.2495
"再见"
79.2495
80.384
"*"