0001 function txt2fsmLmFile(txtFile, fsmLmFile)
0002
0003
0004 if nargin<1, selfdemo; return; end
0005
0006 contents = textread(txtFile, '%s', 'delimiter', '\n', 'whitespace', '');
0007 fid=fopen(fsmLmFile, 'w');
0008 startNode=0;
0009 endNode=length(contents{1});
0010
0011
0012
0013 i=1;
0014 sentence=contents{i};
0015 for j=1:length(sentence)
0016 fprintf(fid, '%d\t%d\t%s\n', j-1, j, sentence(j));
0017 end
0018
0019 runningIndex=endNode+1;
0020 for i=2:length(contents)
0021 sentence=contents{i};
0022 for j=1:length(sentence)
0023 if j==1
0024 fprintf(fid, '%d\t%d\t%s\n', startNode, runningIndex, sentence(j));
0025 elseif j==length(sentence)
0026 fprintf(fid, '%d\t%d\t%s\n', runningIndex, endNode, sentence(j));
0027 runningIndex=runningIndex+1;
0028 else
0029 fprintf(fid, '%d\t%d\t%s\n', runningIndex, runningIndex+1, sentence(j));
0030 runningIndex=runningIndex+1;
0031 end
0032 end
0033 end
0034 fprintf(fid, '%d\n', endNode);
0035 fclose(fid);
0036
0037
0038 function selfdemo
0039 txtFile='test.txt';
0040 lmFile='test.lm';
0041 txt2fsmLmFile(txtFile, lmFile);
0042 fprintf('Input file:\n');
0043 type(txtFile);
0044 fprintf('Output file:\n');
0045 type(lmFile);