首页 > 开发 > 综合 > 正文

用Foxpro编写Windows输入法码表

2020-03-10 00:00:04
字体:
来源:转载
供稿:网友

    windows的码表生成器为人们提供了自制汉字输入法的手段,但在编译前需做大量的预备工作,在实际操作过程中存在着以下三方面的问题,现以制作五笔字型输入法为例加以分析:

1.格式问题 我们虽然能从许多汉字系统中取得五笔字型码表,但这些码表格式很少与windows码表格式相同,因此在用码表生成器编译前需进行格式调整;

2.编码问题 从任何一个汉字系统都只能得到数量有限的词条,因此我们有必要根据需要自建词组,或把非五笔字型输入法(如windows3.2中的郑码)中大量的词组增加到五笔字型输入法中,而为这些词组逐一人工编码既费时又易错;

3.编译限制 windows中的码表生成器在对没有按编码排序的码表文件编译时需要排序,但这时它能实际接受的最大编码数为16000条左右,超过的部分则无法处理,而且排序时间较长。针对上述问题,我用foxpro编制了一段程序,能较满意地解决以上问题,其特点为:

自动生成编码。只需从任一汉字输入法中提取词组并舍去其编码,再不需做任何格式调整,这样我们就会有众多词组源可以利用。

自动生成windows的码表文件。由于在生成码表文件时就已对编码做了排序处理,因而无需windows的码表编译器再排序(sort=0),从而不但省去了费时的排序过程,而且使编码数目几乎没有限制。
一、程序说明
本程序涉及的库文件结构为:

fieldfieldnametypewidthdec
1codecharacter4
2wordcharacter22

程序运行之前,需制作两个库文件:"单字.dbf"和"字码.dbf"。"单字.dbf"存放每个汉字的一、二、三、四级五笔字型编码。"单字.dbf"可以从其他汉字系统的五笔字型码表文件中取得。将取得的单个汉字及其编码添加到"单字.dbf"即可。

"字码.dbf"中的word字段是6850个汉字,code字段是每个汉字五笔字型编码的前二码。"字码.dbf"可以利用"单字.dbf"制作:先删除其中的一级简码,再取得每个汉字和其编码的前两码,最后删除相同的记录。

二、运行过程
1.运行程序的第一步,将要增加的词组文本文件&appfile..txt增补到库文件&appdbfile..dbf的word字段中;

2.调用过程delexistword删除库文件&appdbfile..dbf中与词组库文件"词组库.dbf"重复的记录,以保证增加时不会重复加入"词组库.dbf"中已有的词组;

3.调用过程generate_wbcode生成库文件&appdbfile..dbf中code字段的五笔字型编码。首次运行时,本程序将生成库文件"词组库.dbf",以后运行时,会把欲增加的词组及其编码增补到库文件"词组库.dbf"中。在生成编码时要用到库文件"字码.dbf"及其关于word字段的索引文件"字码.cdx";

4.调用过程towinform生成windows码表文件。运行中用"单字.dbf"和"词组库.dbf"合成所有汉字、词组的编码库文件mbase.dbf并生成其索引文件mbase.cdx。最后由mbase.bdf生成windows的码表文件"五笔字型.txt"。

三、程序清单
settalkoff
setnotifyoff
clear
*将此程序所在路径设为默认路径
progpath=substr(sys(16),1,rat(’/’,sys(16))-1)
setdefaultto&progpath
appfile=getfile(’txt’,’要增加的词组文件.txt:’,’选择’)
appdbfile=stuff(appfile,at(’txt’,appfile),3,’dbf’)
iffile(appdbfile)
deletefile&appdbfile
endif
create&appdbfilefromstruc
use&appdbfilealiasapp
appendfrom&appfilefieldsworddeli
deleteallforlen(alltrim(word))=0
pack
iffile(’词组库.dbf’)
setmessageto"删除与’词组库.dbf’重复的词组"
dodelexistword
endif
setmessageto’正在生成编码’
dogenerate_wbcode
iffile(’词组库.dbf’)
setmessageto’增补到词组库并重新索引’
use词组库
appendfrom&appdbfile
else
setmessageto’建立词组库和其索引’
copyfile&appdbfileto词组库.dbf
use词组库
indexoncodetagcode
indexonwordtagwordof词组库
endif
use
deletefile&appdbfile
setmessageto’正在生成windows的码表文件’
dotowinform
setmessageto’windows格式码表文件生成完毕!’
waitwindowtimeout5
settalkon
setnotifyon
setmessageto
return
*********子过程***********
&&检查是否和’词组库.dbf’有重码
proceduredelexistword
select0
use词组库
setordertoword
selectapp
setrelationtowordinto词组库
gotop
dowhile.not.eof(’app’)
if.not.eof(’词组库’)
delete
endif
skip
enddo
pack
select词组库
use
return

&&生成五笔字型编码
proceduregenerate_wbcode
privatewbcode,wordstring,wordlength,i
select0
use字码ordertagwordof字码
selectapp
gototop
dowhile.not.eof(’app’)
wbcode=’’
wordstring=word
wordlength=len(alltrim(wordstring))
select字码
docase
casewordlength=4
fori=1to2
zi=substr(wordstring,2*i-1,2)
find&zi
wbcode=wbcode+code
next
casewordlength=6
fori=1to3
zi=substr(wordstring,2*i-1,2)
find&zi
docase
casei=1.or.i=2
wbcode=wbcode+substr(code,1,1)
casei=3
wbcode=wbcode+code
endcase
next
casewordlength=8
fori=1to4
zi=substr(wordstring,2*i-1,2)
find&zi
wbcode=wbcode+substr(code,1,1)
next
casewordlength>8
fori=1to3
zi=substr(wordstring,2*i-1,2)
find&zi
wbcode=wbcode+substr(code,1,1)
next
zi=substr(wordstring,wordlength-1,2)
find&zi
wbcode=wbcode+substr(code,1,1)
endcase
selectapp
replacecodewithwbcode
skip
enddo
closedatabases
return

&&生成windows格式码表文件
proceduretowinform
privatehead,word_code,txt,i

iffile(’mbase.dbf’)
deletefilembase.dbf
deletefilembase.cdx
endif
creatembasefromstruc
appendfrom单字
appendfrom词组库
indexoncodetagcodeofmbase

dimensionmbhead[7]
dimensionword_code[2]
mbhead[1]=’[description]’
mbhead[2]=’name=五笔字型’
mbhead[3]=’maxcodes=4’
mbhead[4]="usedcodes=’abcdefghijklmnopqrstuvwxy"
mbhead[5]=’wildchar=z’
mbhead[6]=’sort=0’
mbhead[7]=’[text]’

mbtext=’五笔字型.txt’
txt=fcreate(mbtext)
fori=1to7
=fputs(txt,mbhead[i])
next
gototop
dowhile.not.eof()
scattertoword_code
skip
=fputs(txt,alltrim(word_code[2])+alltrim(word_code[1]))
enddo
use
=fclose(txt)
deletefilembase.dbf
deletefilembase.cdx
return
该程序使用的机器为386dx33,4m,在foxproforwindows2.5b环境下运行通过。

技术交流 永无止境

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表