| 
 | 
 
今天在使用mobile-net时,由于显示的识别结果全是英文,突然有一个想把这些显示的识别结果改成中文冲动。打开label文件一看,1000个英文单词,手动操作不可能了,于是google查询matlab自动翻译的资料,历经一个小时终于搞定。废话不多说,上源码。 
脚本函数如下: 
 
clear all 
close all 
clc 
warning off%关掉警告 
 
readFid = fopen('elabels.txt','r+');%需要翻译的英语句子或单词 
writeFid = fopen('clabels.txt','w+');%存储翻译后的结果 
while ~feof(readFid) 
    s = fgetl(readFid);%读取翻译的英语句子或单词 
    tmpChiWord = e2cTranslate(s); 
    fprintf(writeFid,'%s\n',tmpChiWord);%保存翻译后的结果 
end 
 
fclose(readFid); 
fclose(writeFid); 
 
 
 
调用的子函数如下: 
function chineseWord = e2cTranslate(englishWord) 
%翻译函数 
 
preTranslateURL = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=zh&dt=t&q='; 
wordURL = sprintf('%s%s', preTranslateURL, englishWord);%将URL跟需要翻译的句子合并 
HTMLCodes = urlread(wordURL);%翻译过程 
 
%取出翻译的结果 
preLabel = '[[["'; 
postLabel = sprintf('","%s', englishWord); 
prePosition = findstr(HTMLCodes, preLabel) + length(preLabel); 
postPosition = findstr(HTMLCodes, postLabel) - 1; 
chineseWord = HTMLCodes(prePosition:postPosition); 
 
end 
 
 
 
其中elabel.txt文件中存储的字符格式如下: 
background 
tench 
goldfish 
great white shark 
tiger shark 
hammerhead 
electric ray 
stingray 
 
... 
elabel.txt文件可以自动生成。 
关于汉译英有兴趣的朋友可以仿照程序自己试试。。。 
 
 |   
 
 
 
 |