Home > asr > mlfRead_old.m

mlfRead_old

PURPOSE ^

mlfRead: Read the MLF file generated from Viterbi forced alignment

SYNOPSIS ^

function mlf=mlfRead(mlfFile, keepSil)

DESCRIPTION ^

 mlfRead: Read the MLF file generated from Viterbi forced alignment
    Usage: mlf=mlfRead(mlfFile, keepSil)
    (只能讀HTK的mlf格式)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function mlf=mlfRead(mlfFile, keepSil)
0002 % mlfRead: Read the MLF file generated from Viterbi forced alignment
0003 %    Usage: mlf=mlfRead(mlfFile, keepSil)
0004 %    (只能讀HTK的mlf格式)
0005 
0006 %    Roger Jang, 20041005
0007 
0008 if nargin<1, selfdemo; return; end
0009 if nargin<2, keepSil=0; end
0010 
0011 content = textread(mlfFile, '%s', 'delimiter', '\n', 'whitespace', '');
0012 if length(content)<=3
0013     mlf.word=[];
0014     return;
0015 end
0016 % 讀出總分
0017 items=split(content{2}, '#');
0018 mlf.score=eval(items{2});
0019 content(1:2)=[];    % 跳掉前兩列
0020 content(end)=[];    % 跳掉最後一列
0021 
0022 wordIndex=0;
0023 for i=1:length(content)
0024     line=content{i};
0025 %    [sTime, eTime, phoneName, logProb, wordName, unknown]=strread(line, '%d%d%s%f%s%f');    % 16200000 16600000 sil+d#-1 -9605.09 di#87.391
0026     items=split(line, 9);
0027     sTime=eval(items{1});
0028     eTime=eval(items{2});
0029     phoneNameScore=items{3};
0030     temp=split(phoneNameScore, '#'); phoneName=temp{1}; phoneScore=eval(temp{2});
0031     logProb=eval(items{4});
0032     if length(items)>4    % Found a new word
0033         wordNameScore=items{5};
0034         temp=split(wordNameScore, '#'); wordName=temp{1}; wordScore=eval(temp{2});
0035         wordIndex=wordIndex+1;
0036         word(wordIndex).name=wordName;
0037         word(wordIndex).score=wordScore;
0038         phoneIndex=1;
0039     end
0040     word(wordIndex).phone(phoneIndex).name=phoneName;        % A new phone
0041     word(wordIndex).phone(phoneIndex).score=phoneScore;
0042     word(wordIndex).phone(phoneIndex).time=[sTime; eTime];
0043     word(wordIndex).phone(phoneIndex).logProb=logProb;
0044     phoneIndex=phoneIndex+1;
0045 end
0046 
0047 % Record the duration of each word
0048 for i=1:length(word)
0049     word(i).time=[word(i).phone(1).time(1); word(i).phone(end).time(2)];
0050 end
0051 
0052 % Get rid of 'sil' and 'sp' (for English)
0053 if ~keepSil
0054     silIndex=[];
0055     for i=1:length(word)
0056         if strcmp(word(i).name, 'sil') | strcmp(word(i).name, 'sp')
0057             silIndex=[silIndex, i];
0058         end
0059     end
0060     word(silIndex)=[];
0061 end
0062 % Get rid of 'sil+*' (for Chinese)
0063 if ~keepSil
0064     deleteIndex=[];
0065     for i=1:length(word);
0066         nameLen=length(word(i).phone(1).name);
0067         if strcmp(word(i).phone(1).name(1:min(nameLen,4)), 'sil+')
0068             if length(word(i).phone)>1    % 刪除這個 phone
0069                 word(i).time(1)=word(i).phone(2).time(1);    % 修正 word 的時間
0070                 word(i).phone(1)=[];
0071             else    % 只有一個 phone,紀錄 index,準備砍 word
0072                 deleteIndex=[deleteIndex, i];
0073             end
0074         end
0075     end
0076     word(deleteIndex)=[];
0077 end
0078 mlf.word=word;
0079 
0080 % ======
0081 function selfdemo
0082 wavFile='但使龍城飛將在.wav';
0083 mlfFile='但使龍城飛將在.mlf';
0084 mlf=feval(mfilename, mlfFile);
0085 [y, fs, nbits]=wavread(wavFile);
0086 for i=1:length(mlf.word)
0087     index=ceil(mlf.word(i).time/10000000*fs);    % Time unit in MLF is 100 ns (= 1/10000000 sec)
0088     fprintf('Hit return to play "%s"...\n', mlf.word(i).name); pause
0089 %    fprintf('Hit return to play...\n'); pause
0090     sound(y(index(1):index(2)), fs);
0091 end

Generated on Tue 01-Jun-2010 09:50:19 by m2html © 2003