MATLAB 自制有道词典 GUI

这个工具的诞生,是因为当时想学习一下爬虫,也是我第一次尝试调用API来实现功能,它自身可以导入已有的词典,支持Excel和mat格式,然后在里面没有查询到的单词就会联网查询。对于单词库中没有收入的单词,可以保存到已有的数据库中。

这个GUI相对于上一个计算器已经有了一定的提升,代码简洁一些,也包含有从有道翻译中获取翻译的函数,可以直接使用。
效果如下:
在这里插入图片描述


function dictionary()
warning off
global  GUI;
global load
global youdao
youdao=0;
load=0;
global database;
GUI.dictionary=figure('units','pixels','numbertitle','off',...
    'name','Dictionary','menubar','none','DeleteFcn',@del,...
    'position',[900 200 250 315],'visible','on',...
    'color',[217/256 228/256 241/256]);
try
 figFrame=get(GUI.dictionary,'JavaFrame');  %  设置图标
  newIcon=javax.swing.ImageIcon('logo.jpg');
 figFrame.setFigureIcon(newIcon);
end
GUI.dtime=uicontrol('parent',GUI.dictionary,'style','text',...
    'String','','position',[60 270 180 30],...
    'backgroundcolor',[231/256 237/256 245/256],...
    'foregroundcolor',[0 0 0],'fontsize',8,...
    'fontsize',12);
GUI.load=uicontrol('parent',GUI.dictionary,'style','pushbutton',...
    'String','Load Files','position',[10 230 50 30 ],...
    'backgroundcolor',[231/256 237/256 245/256],...
    'foregroundcolor',[0 0 0],'fontsize',7,...
    'Fontweight','bold',...
    'callback',@load_file);
GUI.search=uicontrol('parent',GUI.dictionary,'style','pushbutton',...
    'String','Search','position',[190 230 50 30 ],...
    'backgroundcolor',[231/256 237/256 245/256],...
    'foregroundcolor',[0 0 0],'fontsize',7,...
    'Fontweight','bold',...
    'callback',@Search);
GUI.search_interface=uicontrol('parent',GUI.dictionary,'style','edit',...
    'String',' ','position',[65 230 120 30 ],...
    'backgroundcolor',[231/256 237/256 245/256],...
    'foregroundcolor',[0 0 0],'fontsize',8,...
'fontweight','bold');
GUI.answer_interface=uicontrol('parent',GUI.dictionary,'style','text',...
    'String','Please load files to continue... ','position',[50 100 165 70 ],...
    'backgroundcolor',[231/256 237/256 245/256],...
    'foregroundcolor',[0 0 0],'fontsize',10,...
'fontweight','bold');
GUI.dic_save=uicontrol('parent',GUI.dictionary,'style','pushbutton',...
    'String','save','position',[160 20 60 20 ],...
    'backgroundcolor',[231/256 237/256 245/256],...
    'foregroundcolor',[0 0 0],'fontsize',10,...
'fontweight','bold','callback',@save_word);
GUI.youdao=uicontrol('parent',GUI.dictionary,'style','text',...
    'String','from youdao','position',[160 70 60 20 ],...
    'backgroundcolor',[217/256 228/256 241/256],...
    'fontsize',7,...
'fontweight','bold','visible','on');
GUI.timer=timer('StartDelay',1,'TimerFcn',@ddisptime,'Period',1,...
'ExecutionMode','fixedRate');
start(GUI.timer);
 end
function load_file(~,~)
global database
global load
global GUI
global file 
global path
temp_path=path;
temp_file=file;
[file, path]=uigetfile({'*.xlsx'},'选择加载的文件');
if isequal(file,0)||isequal(path,0)
    if isempty(database)
    msgbox('加载失败!请重试','错误','warn');
    file=temp_file;
    path=temp_path;
    return
    end
    return
end
[~,~,database]=xlsread(fullfile(path,file));
load=1;
set(GUI.answer_interface,'string','Dictionary is ready!');
end
function Search(~,~)
global database
global GUI
global load
if load==0
  msgbox('请先加载文件!','错误','warn');
  return
end
word=get(GUI.search_interface,'string');
word=strtrim(word);
if isempty(word)
    con='你要我找什么呀~';
    set(GUI.answer_interface,'string',con);
    return
end
[a,~]=find(strcmpi(word,database));
if isempty(a)
    con={'没找到哟~','我有道翻译一下'};
    set(GUI.answer_interface,'string',con);
    pause(1.5);
    str=lookup(word);
    set(GUI.answer_interface,'string',str);
    youdao=1;
else 
    youdao=0;
    word=database(a,2);
    set(GUI.answer_interface,'string',word);
    set(GUI.answer_interface, 'HorizontalAlignment', 'center');   
end
if youdao==1
    set(GUI.youdao,'visible','on')
end
end
function save_word(~,~)
global database
global load
global GUI
global path
global file
if load==0
  msgbox('请先加载文件!','错误','warn');
  return
end
temp_word=get(GUI.search_interface,'string');
temp_word=strtrim(temp_word);
[a,~]=find(strcmpi(temp_word,database));
if isempty(a)
    explains=get(GUI.answer_interface,'string');
    explains=explains';
    num=size(explains);
    explains=reshape(explains,1,num(1)*num(2));
   saved_word={temp_word,explains};
   anss=[database ;saved_word];
   [status]=xlswrite(fullfile(path,file),anss);
   [~,~,database]=xlsread(fullfile(path,file));
else
    msgbox('该单词已经保存!','提示','warn');
end
end
function del(~,~)
a = timerfind();
try
    stop(a);
    delete(a);
end
end
function ddisptime(~,~)
global GUI
set(GUI.dtime,'String',datestr(now));   % 将edit控件的内容改成当前时间
end
function str=lookup(word)
word=urlencode(word);
website=['http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=' word '&only=translate" '];
[sourcefile status]=urlread(website);
expr1='<explains>';
expr2='</explains>';
aa=strfind(sourcefile,expr1)+11;
bb=strfind(sourcefile,expr2)-1;
str=sourcefile(aa:bb);
str=strrep(str,'<ex><![CDATA[','');
str=strrep(str,']]></ex>','');
str=strrep(str,'  ','');
str=strtrim(str);
end
发布了58 篇原创文章 · 获赞 69 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43157190/article/details/104743743