function fsm = read_htk_word_net(filename)
%  fsm = read_htk_word_net(filename)

% Read the M-file into a cell array of strings: 
[fid, message] = fopen(filename, 'rt');
warning(message)
file = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '', 'bufSize', 16000);
fclose(fid);

file = file{1};
% Remove any empty lines
file = file(cellfun('length', file) > 0);

wdnet = strvcat(file);

tok = regexpi(wdnet, 'VERSION=(\S+)', 'tokens');
if ~strcmp(tok{1}, '1.0')
  error('This function only supports HTK SLF files version 1.0');
end

% get number of states
tok = regexpi(wdnet, '^N=(\d+)\s+L=(\d+)', 'tokens');
nstates = tok{1};
narcs = tok{2};

% get state labels
names = regexpi(wdnet, '^I=(?<index>\d+)\s+W=(?<label>\S+)', 'names');
labels = names.label;
null_states = strmatch('!NULL', strvcat(labels));

% get transition matrix
transmat = regexpi(wdnet, , '^J=(?<arc_num>\d+)\s+S=(?<start>\d+)\s+E=(?<end>\d)', 'names');

for x = 1:narcs
  t = transmat(x);
  fsm.transmat(t.start+1, t.end+1) = 1;
end

% need to toss away non-emitting null states
% null states with no incoming arcs are start states
% null states with no outgoing arcs are end states


fsm.labels = labels;
fsm.nstates = nstates;
