Created
February 20, 2017 04:28
-
-
Save naoh16/e014ee9bafd4a3a12271a84b0e601e8e to your computer and use it in GitHub Desktop.
HTKで作ったGMM(中心1状態のHMM)をSPTKで読み込める形式に変換するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!env perl | |
use strict; | |
use warnings; | |
use Getopt::Long; | |
my $opt_outdir = "."; | |
GetOptions('dir=s' => \$opt_outdir); | |
my @mixtures = (); | |
my @means = (); | |
my @variances = (); | |
my $fp; | |
my $cur_model = "NULL"; | |
while(<>) { | |
s/[\r\n]+$//; | |
if(/^~h "([^"]+)"$/) { | |
$cur_model = $1; | |
} | |
if(/<BEGINHMM>/) { | |
@mixtures = (); | |
@means = (); | |
@variances = (); | |
} | |
if(/<ENDHMM>/) { | |
my $out_filename = $opt_outdir . "/" . $cur_model . ".gmm"; | |
open $fp, ">", $out_filename or die "Error: OpenW: $out_filename"; | |
binmode $fp; | |
output_model(); | |
close $fp; | |
} | |
if(/<MIXTURE> (\d+) ([0-9\.e\+\-]+)/) { | |
#print $1 . " -> " . $2 . "\n"; | |
push @mixtures, $2; | |
next; | |
} | |
if(/<MEAN>/) { | |
my $next_line = <>; | |
$next_line =~ s/^\s|\s$//g; | |
my @new_means = split(/\s/, $next_line); | |
push @means, [@new_means]; | |
next; | |
} | |
if(/<VARIANCE>/) { | |
my $next_line = <>; | |
$next_line =~ s/^\s|\s$//g; | |
my @new_variances = split(/\s/, $next_line); | |
push @variances, [@new_variances]; | |
next; | |
} | |
} | |
sub output_model { | |
#print STDERR ">> MIXTURES\n"; | |
for(my $i=0; $i<=$#mixtures; ++$i) { | |
#print $mixtures[$i] . "\n"; | |
print $fp pack("f", 0.0+$mixtures[$i]); | |
} | |
for(my $i=0; $i<=$#mixtures; ++$i) { | |
#print STDERR ">> MEANS AND VARIANCES $i\n"; | |
my $cur_means = $means[$i]; | |
for(my $m=0; $m<=$#{$cur_means}; ++$m) { | |
#print $cur_means->[$m] . "\n"; | |
print $fp pack("f", 0.0+$cur_means->[$m]); | |
} | |
my $cur_vars = $variances[$i]; | |
for(my $m=0; $m<=$#{$cur_vars}; ++$m) { | |
#print $cur_vars->[$m] . "\n"; | |
print $fp pack("f", 0.0+$cur_vars->[$m]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment