批处理的效率虽然是比不上其它的XX,但是,也因为批处理的特点,往往是可以取巧的!!
下面的代码,实现词拆分查表功能,可以在一秒内完成:
注意:dictionary.txt 词库,需以较长的词在前,短在后的顺序排列- @echo off&setlocal enabledelayedexpansion
-
- :lp
- set str=
- echo;
- echo;直接回车退出测试
- set /p str=输入一个字符串:
- if "!str!" equ "" goto :eof
- setlocal enabledelayedexpansion
-
-
- set/a z=8180,x=0&for /l %%a in (1,1,14) do (set/a "y=(z-x)/2+x"
- for %%b in (!y!) do if "!str:~%%b,1!" equ "" (set/a z=y) else (set/a x=y)
- )
- ::计算输入串的长度
-
- ::sap 存入同等长度的空格串
- set sap=
- (for /l %%i in (0,1,!z!) do (
- set "spa= !spa!"
- set/a x=z-%%i
- for /l %%j in (1,1,!x!) do (
- echo;!str:~%%i,%%j!
- set/a y=%%i+%%j
- for %%a in ("!str:~%%i,%%j!") do (
- set ##%%~a=%%i-!y!-%%j !##%%~a!
- )
- )
- ))>fen.tmp
- ::进行排列拆分到临时文件fen.tmp,同时取各分词的长度,及在原句中的起址位置,并按一定的规则保存
-
- set var=!str!
-
-
- for /f "tokens=*" %%a in ('findstr /x /i /g:fen.tmp dictionary.txt') do (
- for %%k in (!##%%a!) do (
- if "!var:%%a=!" neq "!var!" (
- for /f "tokens=1,2,3 delims=-" %%b in ("%%k") do (
- if "!var:~%%b,1!" neq " " (
- echo;截取过程:[!var!] %%a
- set var=!var:~0,%%b!!spa:~,%%d!!var:~%%c!
- set $$%%b=%%a
- )
- )
- )
- )
- )
- ::搜索词库,并做已经截取过记号,同时为恢复分词在原句中的位置做准备
-
-
- echo;
- if "!var: =!" neq "" (
- echo;词库中没有的:
- for %%a in (!var!) do (
- for %%k in (!##%%a!) do (
- if "!var:%%a=!" neq "!var!" (
- for /f "tokens=1,2,3 delims=-" %%b in ("%%k") do (
- if "!var:~%%b,1!" neq " " (
- echo;截取过程:[!var!] %%a
- set var=!var:~0,%%b!!spa:~,%%d!!var:~%%c!
- set $$%%b=%%a
- )
- )
- )
- )
- )
- ) else (echo;所有字符均可在词库中到)
- ::对在词库中找不到的分词,也进行同样的处理
-
-
- echo;
- set dest=
- for /l %%a in (0,1,!z!) do set dest=!dest! !$$%%a!
- echo;最终拆分:!dest!
-
- endlocal&goto :lp
复制代码
[ 本帖最后由 netbenton 于 2010-9-15 03:32 编辑 ] |