我一直在 Matlab 中进行字素到音素的转换,并尝试生成一个更通用的代码,以首先将单词分解为特定的辅音、二合字母及其相关元音,并将每个输入的字符串(单词)分割成它的字素形式,以便它可以在以后产生适当的语音合成。然而,由于存在大量规则,因此存在大量 if-elseif-else,循环遍历每个字母并存在不断增长的数组,时间复杂度增加了很多,并且没有给出任何结果(它显示繁忙,每次我通过控制台输入字符串时)。所以如果你能帮我简化代码,下面是:
prompt='Enter a string: ';
str=input(prompt,'s');
l=length(str);
grapheme=[];
i=1;
while (i<=l)
if strcmpi(str(i),'b')
grapheme=[grapheme;{'b'}];
elseif strcmpi(str(i:i+1),'bb')
grapheme=[grapheme;{'b'}];
i=i+1;
elseif strcmpi(str(i),'d')
grapheme=[grapheme;{'d'}];
elseif strcmpi(str(i:i+1),'dd')||strcmpi(str(i:i+1),'ed')
grapheme=[grapheme;{'d'}];
i=i+1;
elseif strcmpi(str(i),'f')
grapheme=[grapheme;{'f'}];
elseif strcmpi(str(i:i+1),'ff')||strcmpi(str(i:i+1),'ph')||strcmpi(str(i:i+1),'gh')
grapheme=[grapheme;{'f'}];
i=i+1;
elseif strcmpi(str(i),'g')
grapheme=[grapheme;{'g'}];
elseif strcmpi(str(i:i+1),'gg')%||strcmpi(str(i:i+2),'gue')
grapheme=[grapheme;{'g'}];
i=i+1;
elseif strcmpi(str(i),'h')
grapheme=[grapheme;{'h'}];
elseif strcmpi(str(i:i+1),'wh') && strcmpi(str(i+2),'o')
grapheme=[grapheme;{'h'}];
i=i+1;
elseif strcmpi(str(i),'j')
grapheme=[grapheme;{'j'}];
elseif strcmpi(str(i:i+1),'ge')||strcmpi(str(i:i+1),'jj')
grapheme=[grapheme;{'j'}];
i=i+1;
% elseif strcmpi(str(i:i+2),'dge')
% grapheme=[grapheme;{'j'}];
% i=i+2;
elseif strcmpi(str(i),'k')||strcmpi(str(i),'c')
grapheme=[grapheme;{'k'}];
elseif strcmpi(str(i),'ck')||strcmpi(str(i),'cc')||strcmpi(str(i),'qu')
grapheme=[grapheme;{'k'}];
i=i+1;
elseif strcmpi(str(i),'l')
grapheme=[grapheme,{'l'}];
elseif strcmpi(str(i),'ll')||strcmpi(str(i),'le')
grapheme=[grapheme,{'l'}];
i=i+1;
elseif strcmpi(str(i),'m')
grapheme=[grapheme,{'m'}];
elseif strcmpi(str(i),'mm')||strcmpi(str(i),'lm')||strcmpi(str(i),'mn')
grapheme=[grapheme;{'m'}];
i=i+1;
elseif strcmpi(str(i),'n')
grapheme=[grapheme,{'n'}];
elseif strcmpi(str(i),'nn')||strcmpi(str(i),'kn')||strcmpi(str(i),'pn')||strcmpi(str(i),'gn')
grapheme=[grapheme;{'n'}];
i=i+1;
elseif strcmpi(str(i),'p')
grapheme=[grapheme;{'p'}];
elseif strcmpi(str(i),'pp')
grapheme=[grapheme;{'p'}];
i=i+1;
elseif strcmpi(str(i),'r')
grapheme=[grapheme;{'r'}];
elseif strcmpi(str(i),'rr')||strcmpi(str(i),'wr')||strcmpi(str(i),'rh')
grapheme=[grapheme;{'r'}];
i=i+1;
elseif strcmpi (str(i),'s')
grapheme=[grapheme;{'s'}];
elseif strcmpi(str(i),'ss')||strcmpi(str(i),'sc')||strcmpi(str(i),'ce')||strcmpi(str(i),'se')||strcmpi(str(i),'ps')
grapheme=[grapheme;{'s'}];
i=i+1;
elseif strcmpi (str(i),'t')
grapheme=[grapheme;{'t'}];
elseif strcmpi(str(i),'tt')|| strcmpi(str(i),'te')
grapheme=[grapheme;{'t'}];
i=i+1;
elseif strcmpi (str(i),'v')
grapheme=[grapheme;{'v'}];
elseif strcmpi(str(i),'ve')
grapheme=[grapheme;{'v'}];
i=i+1;
elseif strcmpi (str(i),'v')
grapheme=[grapheme;{'v'}];
elseif strcmpi(str(i),'ve')
grapheme=[grapheme;{'v'}];
i=i+1;
elseif strcmpi (str(i),'w')
grapheme=[grapheme;{'w'}];
elseif strcmpi(str(i),'wh')
grapheme=[grapheme;{'w'}];
i=i+1;
elseif strcmpi(str(i),'x')
grapheme=[grapheme;{'x'}];
elseif strcmpi (str(i),'y') && strcmp('I')
grapheme=[grapheme;{'y'}];
elseif strcmpi(str(i),'z')
grapheme=[grapheme;{'z'}];
elseif strcmpi(str(i),'zz')||strcmpi(str(i),'ze')
grapheme=[grapheme;{'z'}];
i=i+1;
elseif strcmpi(str(i),'sh')
grapheme=[grapheme;{'sh'}];
i=i+1;
elseif strcmpi(str(i),'zh')
grapheme=[grapheme;{'z'}];
i=i+1;
elseif strcmpi(str(i),'ch')
grapheme=[grapheme;{'ch'}];
i=i+1;
elseif strcmpi(str(i),'th')
grapheme=[grapheme;{'th'}];
i=i+1;
elseif strcmpi(str(i),'a')
grapheme=[grapheme;{'a'}];
elseif strcmpi(str(i),'e')
grapheme=[grapheme;{'e'}];
elseif strcmpi(str(i),'i')
grapheme=[grapheme;{'i'}];
elseif strcmpi(str(i),'o')
grapheme=[grapheme;{'o'}];
elseif strcmpi(str(i),'u')
grapheme=[grapheme;{'u'}];
end
end
display(grapheme);