一、运行下面代码里面的所有的宏
二、%cyjl(接龙用的成语,mode=接龙模式)
说明:
1、只有两种接龙模式可以用:1随机 2长龙
2、随机模式:使用IML模式完成,随机抽取接龙的词语,有可能重复
3、长龙模式:使用SQL方式完成,寻找最长的不重复的成语接龙方式
使用的成语词库是从搜狗输入法下载的成语大全:http://pinyin.sogou.com/dict/detail/index/931
代码如下:
- /********************************************************/
- /*程序说明:使用搜狗的成语词库做成语接龙程序 */
- /*程序编写时间:2014年9月12日 */
- /*程序修改时间:2014年9月14日 */
- /*作者:6皮皮9 */
- /********************************************************/
- libname cy 'D:\成语接龙测试\sas_file';
- /*导入成语列表*/
- proc import datafile="D:\成语接龙测试\成语大全.txt"
- out=cy.cy_list
- replace;
- guessingrows=20000;
- run;
- data cy.test;
- set cy.cy_list;
- length f e $2.;
- index=_n_;
- f=ksubstr(var1,1,1);/*第一个字符*/
- e=ksubstr(kreverse(strip(var1)),1,1);/*最后一个字符*/
- run;
- /*使用IML做的随机成语接龙*/
- %macro cy(beg_wd);
- data wrd_list;
- list='*********成语接龙列表*********';
- run;
- proc iml;
- use cy.test;
- read all var _num_ into index;
- read all var _char_ into wd;
- close test;
- initial_wd_pos=(loc(wd[,1]="&beg_wd"));
- if ncol(initial_wd_pos)=0 then do;
- i_wd='好像你输入的不是成语哦';
- edit wrd_list;
- append from i_wd;
- end;
- else do;
- initial_ewd=wd[initial_wd_pos,3];/*初始成语词尾*/
- i_wd=wd[initial_wd_pos,1];
- edit wrd_list;
- append from i_wd;
- cur_ewd=initial_ewd;/*迭代用的词尾*/
- counter=0;
- wrd_loop='A';
- do until(counter=200);
- counter=counter+1;
- curpos=(loc(wd[,2]=cur_ewd));/*查找词尾属于哪个词的词头*/
- if ncol(curpos)=0 then do;
- counter=200;
- wrd_loop='完蛋接不下去了!Game over';
- end;
- else if ncol(curpos)>1 then do;
- call randseed(0);
- u=j(1,1,.);
- call randgen(u,'uniform');
- a1=1; b1=ncol(curpos);
- x = a1+(b1-a1)*u;
- rand_pos=int(x);
- line_pos=curpos[,rand_pos];
- wrd_loop=wd[line_pos,1];
- cur_ewd=wd[line_pos,3];
- end;
- else do;
- wrd_loop=wd[curpos,1];
- cur_ewd=wd[curpos,3];
- end;
- edit wrd_list;
- append from wrd_loop;
- end;
- end;
- quit;
- proc print data=wrd_list;
- run;
- %mend;
- /*使用SQL找到最长的不重复成语接龙方式*/
- %macro cy_long(beg_wd);
- proc sql;
- create table cy.wd_num as
- select f as fwd,count(f) as ewd_num
- from cy.test
- group by f
- order by calculated ewd_num
- ;
- quit;
- proc sql;
- create table cy.jl as
- select t.*,numb.ewd_num
- from cy.test as t
- left join cy.wd_num as numb
- on t.e=numb.fwd
- ;
- quit;
- data cy.jl;
- set cy.jl;
- if ewd_num=. then ewd_num=0;
- run;
- data wrd_list;
- do i=1 to 2;
- if i=1 then list='*********成语接龙列表*********';
- if i=2 then list="&beg_wd";
- output;
- end;
- drop i;
- run;
- proc sql noprint;
- select f,e into:fc,:ec
- from cy.jl
- where var1="&beg_wd"
- ;
- quit;
- options nonotes;
- proc sql noprint;
- select ewd_num into:wrd_check
- from cy.jl
- where var1="&beg_wd"
- ;
- quit;
- %if &wrd_check=0 %then %do;
- data wrd_list;
- do i=1 to 3;
- if i=1 then list='*********成语接龙列表*********';
- if i=2 then list="&beg_wd";
- if i=3 then list="完蛋接不下去了!Game over";
- output;
- end;
- proc print data=wrd_list;
- run;
- %end;
- %else %do %until(&ewd_check=0);
- proc sql noprint;
- select var1,ewd_num into:wd_loop,:ewd
- from cy.jl
- where f="&ec"
- and ewd_num in (
- select max(ewd_num)
- from cy.jl
- where f="&ec"
- and var1 not in(
- select list
- from wrd_list))
- and var1 not in(
- select list
- from wrd_list);
- quit;
- %let ewd_check=&ewd;%put &ewd_check;
- data loop_wd;
- list="&wd_loop";
- run;
- proc append base=wrd_list data=loop_wd force;
- run;
- proc sql noprint;
- select e into:ec
- from cy.jl
- where var1="&wd_loop"
- ;
- quit;
- %end;
- options notes;
- proc print data=wrd_list;
- run;
- %mend cy_long;
- %macro cyjl(beg_wd,mode=随机);
- proc sql noprint;
- select count(var1) into:wrd_check
- from cy.test
- where var1="&beg_wd"
- ;
- quit;
- %if &wrd_check=0 %then %do;
- data warnning;
- Warning='亲,您输入的成语不在这个词库哦!玩不了诶~sorry';
- run;
- proc print data=warnning;
- run;
- %end;
- %else %do;
- %if &mode=随机 %then %do;
- %cy(&beg_wd);
- %end;
- %else %if &mode=长龙 %then %do;
- %cy_long(&beg_wd);
- %end;
- %else %do;
- data warnning;
- Warning='哎哟~亲~~,你的接龙模式选择有错哦!模式只能选择:随机,长龙';
- run;
- proc print data=warnning;
- run;
- %end;
- %end;
- %mend cyjl;
- %cyjl(各取所需,mode=长龙)