Skip to content

Instantly share code, notes, and snippets.

@naoh16
Created February 20, 2017 04:28
Show Gist options
  • Save naoh16/e014ee9bafd4a3a12271a84b0e601e8e to your computer and use it in GitHub Desktop.
Save naoh16/e014ee9bafd4a3a12271a84b0e601e8e to your computer and use it in GitHub Desktop.
HTKで作ったGMM(中心1状態のHMM)をSPTKで読み込める形式に変換するスクリプト
#!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