本帖最后由 happy886rr 于 2016-10-19 08:41 编辑
很多汉字转拼音工具都携带太大的字典,而且转换的样式比较单调。因此pin.exe利用读音界点去压缩字典数据,采用辗转移位加速。支持文本或管道流、格式化输出拼音、支持简繁体转化、支持大小写转化。
[2016/10/19]version 1.3 修复数个漏洞,增加大小写转化,改进开关识别,功能增但体积减,仅9kb大小.
下载文件:把下面这张图片另存为pin.7z直接解压即是。
具体用法- PIN.EXE
- __________________________________________________________________________
-
- 汉字转拼音工具, 版本 1.3
- COPYRIGHT@2016~2018 BY HAPPY
- 使用:
- pin [file|-p] [(-d num&delims)|-l|-u|-s|-t]
- __________________________________________________________________________
- 选项:
- -h 显示帮助信息
- -p 从管道读取信息流
- -d 格式化输出: num为拼音样式编号, delims为间隔符(串)
- -l 大写转小写
- -u 小写转大写
- -s 将信息流转为简体
- -t 将信息流转为繁体
- __________________________________________________________________________
- num :
- 0 小写全拼
- 1 大写全拼
- 2 首字母大写全拼
- 3 首字母大写缩拼
- 4 首字母小写缩拼
- __________________________________________________________________________
- 示例:
- pin test.txt //将test.txt中汉字转为拼音
- pin test.txt -d0""" //以 " 号为间隔符,转为拼音
- pin test.txt -u //将test.txt中字母转为大写
- dir|pin -p -t //将dir 显示的信息流转为繁体
- echo 簡體轉換|pin -p -s //将管道信息流强制转化为简体
- echo 簡體轉換|pin -p -d3" " //首字母大写缩拼,以空格为间隔(串)
- __________________________________________________________________________
复制代码 核心代码:- #include <stdio.h>
- #include <windows.h>
-
- //读音数组
- const char* Pinyin_Code[]={"a","ai","an","ang","ao","ba","bai","ban","bang","bao","bei","ben","beng","bi","bian","biao","bie","bin","bing","bo","bu","ca","cai","can","cang","cao","ce","ceng","cha","chai","chan","chang","chao","che","chen","cheng","chi","chong","chou","chu","chuai","chuan","chuang","chui","chun","chuo","ci","cong","cou","cu","cuan","cui","cun","cuo","da","dai","dan","dang","dao","de","deng","di","dian","diao","die","ding","diu","dong","dou","du","duan","dui","dun","duo","e","en","er","fa","fan","fang","fei","fen","feng","fo","fou","fu","ga","gai","gan","gang","gao","ge","gei","gen","geng","gong","gou","gu","gua","guai","guan","guang","gui","gun","guo","ha","hai","han","hang","hao","he","hei","hen","heng","hong","hou","hu","hua","huai","huan","huang","hui","hun","huo","ji","jia","jian","jiang","jiao","jie","jin","jing","jiong","jiu","ju","juan","jue","jun","ka","kai","kan","kang","kao","ke","ken","keng","kong","kou","ku","kua","kuai","kuan","kuang","kui","kun","kuo","la","lai","lan","lang","lao","le","lei","leng","li","lia","lian","liang","liao","lie","lin","ling","liu","long","lou","lu","lv","luan","lue","lun","luo","ma","mai","man","mang","mao","me","mei","men","meng","mi","mian","miao","mie","min","ming","miu","mo","mou","mu","na","nai","nan","nang","nao","ne","nei","nen","neng","ni","nian","niang","niao","nie","nin","ning","niu","nong","nu","nv","nuan","nue","nuo","o","ou","pa","pai","pan","pang","pao","pei","pen","peng","pi","pian","piao","pie","pin","ping","po","pu","qi","qia","qian","qiang","qiao","qie","qin","qing","qiong","qiu","qu","quan","que","qun","ran","rang","rao","re","ren","reng","ri","rong","rou","ru","ruan","rui","run","ruo","sa","sai","san","sang","sao","se","sen","seng","sha","shai","shan","shang","shao","she","shen","sheng","shi","shou","shu","shua","shuai","shuan","shuang","shui","shun","shuo","si","song","sou","su","suan","sui","sun","suo","ta","tai","tan","tang","tao","te","teng","ti","tian","tiao","tie","ting","tong","tou","tu","tuan","tui","tun","tuo","wa","wai","wan","wang","wei","wen","weng","wo","wu","xi","xia","xian","xiang","xiao","xie","xin","xing","xiong","xiu","xu","xuan","xue","xun","ya","yan","yang","yao","ye","yi","yin","ying","yo","yong","you","yu","yuan","yue","yun","za","zai","zan","zang","zao","ze","zei","zen","zeng","zha","zhai","zhan","zhang","zhao","zhe","zhen","zheng","zhi","zhong","zhou","zhu","zhua","zhuai","zhuan","zhuang","zhui","zhun","zhuo","zi","zong","zou","zu","zuan","zui","zun","zuo"};
- //读音界点
- const unsigned short Pinyin_Side[]={0xB0A1,0xB0A3,0xB0B0,0xB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BD1,0xCBD4,0xCBE1,0xCBE4,0xCBEF,0xCBF2,0xCBFA,0xCCA5,0xCCAE,0xCCC0,0xCCCD,0xCCD8,0xCCD9,0xCCDD,0xCCEC,0xCCF4,0xCCF9,0xCCFC,0xCDA8,0xCDB5,0xCDB9,0xCDC4,0xCDC6,0xCDCC,0xCDCF,0xCDDA,0xCDE1,0xCDE3,0xCDF4,0xCDFE,0xCEC1,0xCECB,0xCECE,0xCED7,0xCEF4,0xCFB9,0xCFC6,0xCFE0,0xCFF4,0xD0A8,0xD0BD,0xD0C7,0xD0D6,0xD0DD,0xD0E6,0xD0F9,0xD1A5,0xD1AB,0xD1B9,0xD1C9,0xD1EA,0xD1FB,0xD2AC,0xD2BB,0xD2F0,0xD3A2,0xD3B4,0xD3B5,0xD3C4,0xD3D9,0xD4A7,0xD4BB,0xD4C5,0xD4D1,0xD4D4,0xD4DB,0xD4DF,0xD4E2,0xD4F0,0xD4F4,0xD4F5,0xD4F6,0xD4FA,0xD5AA,0xD5B0,0xD5C1,0xD5D0,0xD5DA,0xD5E4,0xD5F4,0xD6A5,0xD6D0,0xD6DB,0xD6E9,0xD7A5,0xD7A7,0xD7A8,0xD7AE,0xD7B5,0xD7BB,0xD7BD,0xD7C8,0xD7D7,0xD7DE,0xD7E2,0xD7EA,0xD7EC,0xD7F0,0xD7F2};
- //缓存大小
- #define BUFF_SIZE 4096
- //全局数组
- int flag[2]={1,1};
- char* delims="0 ";
-
- /*拼音函数*/
- inline void Pinyin(unsigned short S)
- {
- unsigned short i, N, L=0, R=395;
- const char* tmp;
- while(R-L>1){
- N=(R+L)>>1;
- if(Pinyin_Side[N]==S){R=N;break;}
- if(Pinyin_Side[N] <S){L=N;}else{R=N;}
- }
- tmp=Pinyin_Code[(Pinyin_Side[R]<=S)?R:L];
- switch(delims[0]){
- case '0':
- fprintf(stdout,"%s",tmp);
- break;
- case '1':
- for(i=0; tmp[i]!='\0'; i++){
- fprintf(stdout,"%c",tmp[i]-32);
- }
- break;
- case '2':
- fprintf(stdout,"%c",tmp[0]-32);
- for(i=1; tmp[i]!='\0'; i++){
- fprintf(stdout,"%c",tmp[i]);
- }
- break;
- case '3':
- fprintf(stdout,"%c",tmp[0]-32);
- break;
- case '4':
- fprintf(stdout,"%c",tmp[0]);
- break;
- }
- }
-
- /*串化拼音*/
- inline void String2Pinyin(const unsigned char* Str)
- {
- int i=0,L=strlen(Str);
- while(i<L){
- if(Str[i]<0x80){
- fprintf(stdout,"%c",Str[i]);
- }else if((0xB0<=Str[i]) && (Str[i]<=0xD7) && (0xA1<=Str[i+1]) && (Str[i+1]<=0xFE)){
- if(i>0 && Str[i-1]<0x80){fprintf(stdout,"%s",(delims+1));}
- Pinyin(Str[i]<<8|Str[i+1]);
- fprintf(stdout,"%s",(delims+1));
- i++;
- }else{
- fprintf(stdout,"%c%c",Str[i],Str[i+1]);
- i++;
- }
- i++;
- }
- }
-
- /*简繁体互转*/
- inline char* TraTTSim(const char* Str, int FLAG)
- {
- int L=LCMapString(LOCALE_SYSTEM_DEFAULT, FLAG, Str, -1, NULL, 0);
- char* Out=(char *)calloc(L+1, sizeof(char));
- LCMapString(LOCALE_SYSTEM_DEFAULT, FLAG, Str, -1, Out, L);
- return Out;
- }
-
- /*帮助信息*/
- void Help(FILE* stream, int Exit_Code)
- {
- fprintf(stream,
- "--------------------------------------------------------------------------\n"
- "CHINESE CHARACTERS TO PINYIN, VERSION 1.3\n"
- "COPYRIGHT@2016~2018 BY HAPPY\n"
- "USAGE:\n"
- " pin [file|-p] [(-d num&delims)|-l|-u|-s|-t]\n"
- "--------------------------------------------------------------------------\n"
- "OPTIONS:\n"
- " -h show pin's help information\n"
- " -p read stream from pipe\n"
- " -d pinyin format and delims\n"
- " -l to Lowercase\n"
- " -u to Uppercase\n"
- " -s to Simplified\n"
- " -t to Traditional\n"
- "--------------------------------------------------------------------------\n"
- "The value of 'num' is zero to four,the corresponding actions are:\n"
- " 0 lowercase full pinyin;\n"
- " 1 uppercase full pinyin;\n"
- " 2 initial capitalization of full pinyin;\n"
- " 3 pinyin initials capitalized;\n"
- " 4 pinyin initials lowercase.\n"
- "--------------------------------------------------------------------------\n"
- " 10/18/2016\n"
- );
- exit(Exit_Code);
- }
-
- /*行显函数*/
- void DisplayLine(FILE* stream)
- {
- char* Line=(char *)malloc(BUFF_SIZE*sizeof(char));
- while(!feof(stream)){
- memset(Line, 0, BUFF_SIZE*sizeof(char));
- fgets(Line, BUFF_SIZE, stream);
- switch(flag[1]){
- case 1:
- String2Pinyin(TraTTSim(Line,LCMAP_SIMPLIFIED_CHINESE));
- break;
- case 2:
- fputs(strlwr(Line), stdout);
- break;
- case 3:
- fputs(strupr(Line), stdout);
- break;
- case 4:
- fputs(TraTTSim(Line,LCMAP_SIMPLIFIED_CHINESE ), stdout);
- break;
- case 5:
- fputs(TraTTSim(Line,LCMAP_TRADITIONAL_CHINESE), stdout);
- break;
- }
- }
- }
-
- /* Main主函数入口 */
- int main(int argc, char** argv)
- {
- if(argc<=3 && !strcasecmp(argv[1],"-p")){
- flag[0]=0;
- }
- if(argc==3 && argv[2][0]=='-'){
-
- switch(argv[2][1]){
- case 'H':
- case 'h':
- Help(stdout, 0);
- return 0;
- case 'P':
- case 'p':
- flag[0]=0;
- break;
- case 'D':
- case 'd':
- delims=(argv[2]+2);
- if((delims[0]<'0') || ('4'<delims[0])){delims[0]='0';}
- break;
- case 'L':
- case 'l':
- flag[1]=2;
- break;
- case 'U':
- case 'u':
- flag[1]=3;
- break;
- case 'S':
- case 's':
-
- flag[1]=4;
- break;
- case 'T':
- case 't':
-
- flag[1]=5;
- break;
- default:
- Help(stderr, 1);
- return 1;
- }
- }
- if(flag[0]==1){
- FILE* fp=fopen(argv[1], "rb");
- if(fp==NULL){
- Help(stderr, 1);
- return 1;
- }
- DisplayLine(fp);
- fclose(fp);
- return 0;
- }
- if(flag[0]==0){
- DisplayLine(stdin);
- }
- return 0;
- }
复制代码
|