function word_hmms = convert_phone_to_word_hmms(phone_hmms, dict);
% word_hmms = convert_phone_to_word_hmms(phone_hmms, dict)
%
% 2007-01-27 ronw@ee.columbia.edu

% Copyright (C) 2007 Ron J. Weiss
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

phones = {phone_hmms(:).name};

% dict is a cell array.  each entry looks like this:
% word list of of phones 
% each token is separated by whitespace.  e.g.:
%PLACE                   p l ey s

for n = 1:length(dict)
  [word, remain] = strtok(dict{n});

  p = 1;  idx = [];
  while length(remain > 0)
    [tmp, remain] = strtok(remain);
    idx(p) = strmatch(tmp, phones, 'exact');
    p = p + 1;
  end
  nphones = length(idx);

  fsm.name = word;
  fsm.nstates = nphones;
  fsm.labels = phones(idx);
  fsm.start_prob = [0, repmat(-Inf, [1, nphones-1])];
  fsm.end_prob = [repmat(-Inf, [1, nphones-1]), 0];
  fsm.transmat = zeros(nphones) - Inf;

  for s = 1:nphones-1
    fsm.transmat(s,s+1) = 0;
  end

  % word_hmms(n) = compose_hmms(phone_hmms(idx), fsm);
  word_hmms(n) = compose_hmms(phone_hmms, fsm);
end
