FMM算法 分词
来源:互联网 发布:淘宝店如何加入村淘 编辑:程序博客网 时间:2024/06/07 10:14
FMM算法的最简单思想是使用贪心算法向前找n个,如果这n个组成的词在词典中出现,就ok,如果没有出现,那么找n-1个...然后继续下去。假如n个词在词典中出现,那么从n+1位置继续找下去,知道句子结束。代码片段(3)
[代码] [Python]代码
01
import
re
02
def
PreProcess(sentence,edcode
=
"utf-8"
):
03
sentence
=
sentence.decode(edcode)
04
sentence
=
re.sub(u
"[。,,!……!《》<>/"'::?/?、/|“”‘’;]"
,
" "
,sentence)
05
return
sentence
06
07
def
FMM(sentence,diction,result
=
[],maxwordLength
=
4
,edcode
=
"utf-8"
):
08
i
=
0
09
sentence
=
PreProcess(sentence,edcode)
10
length
=
len
(sentence)
11
while
i < length:
12
# find the ascii word
13
tempi
=
i
14
tok
=
sentence[i:i
+
1
]
15
while
re.search(
"[0-9A-Za-z/-/+#@_/.]{1}"
,tok)<>
None
:
16
i
=
i
+
1
17
tok
=
sentence[i:i
+
1
]
18
if
i
-
tempi>
0
:
19
result.append(sentence[tempi:i].lower().encode(edcode))
20
# find chinese word
21
left
=
len
(sentence[i:])
22
if
left
=
=
1
:
23
"""go to 4 step over the FMM"""
24
"""should we add the last one? Yes, if not blank"""
25
if
sentence[i:] <>
" "
:
26
result.append(sentence[i:].encode(edcode))
27
return
result
28
m
=
min
(left,maxwordLength)
29
30
for
j
in
xrange
(m,
0
,
-
1
):
31
leftword
=
sentence[i:j
+
i].encode(edcode)
32
# print leftword.decode(edcode)
33
if
LookUp(leftword,diction):
34
# find the left word in dictionary
35
# it's the right one
36
i
=
j
+
i
37
result.append(leftword)
38
break
39
elif
j
=
=
1
:
40
"""only one word, add into result, if not blank"""
41
if
leftword.decode(edcode) <>
" "
:
42
result.append(leftword)
43
i
=
i
+
1
44
else
:
45
continue
46
return
result
47
def
LookUp(word,dictionary):
48
if
dictionary.has_key(word):
49
return
True
50
return
False
51
def
ConvertGBKtoUTF(sentence):
52
return
sentence.decode(
'gbk'
).encode(
'utf-8'
)
[代码] [Python]代码
01
dictions
=
{}
02
dictions[
"ab"
]
=
1
03
dictions[
"cd"
]
=
2
04
dictions[
"abc"
]
=
1
05
dictions[
"ss"
]
=
1
06
dictions[ConvertGBKtoUTF(
"好的"
)]
=
1
07
dictions[ConvertGBKtoUTF(
"真的"
)]
=
1
08
sentence
=
"asdfa好的是这样吗vasdiw呀真的daf dasfiw asid是吗?"
09
s
=
FMM(ConvertGBKtoUTF(sentence),dictions)
10
for
i
in
s:
11
print
i.decode(
"utf-8"
)
[代码] [Python]代码
1
test
=
open
(
"test.txt"
,
"r"
)
2
for
line
in
test:
3
s
=
FMM(CovertGBKtoUTF(line),dictions)
4
for
i
in
s:
5
print
i.decode(
"utf-8"
)
- FMM算法 分词
- python 中文分词——FMM 算法
- 中文分词的python实现-基于FMM算法
- FMM和BMM分词题目
- 算法分析与设计的作业:“基于FMM的分词系统”
- SIGHAN2005分词baseline的perl程序FMM
- 前向最大匹配分词FMM
- 中文分词的python实现----HMM、FMM
- opencv图像修复算法cvInpaint(Telea的FMM算法)
- 分词算法
- 分词算法
- 分词算法
- 分词算法
- 分词算法
- 搜索引擎分词算法介绍
- baidu分词算法分析
- baidu分词算法分析
- 中文分词算法
- 你的for循环真的高效吗——优化for循环第一章
- ajax学习笔记(1)
- word 简历 横线 黑线 (类似下划线) 添加 删除
- Installing swftools and pdf2swf on Ubuntu Linux
- oracle10g 安装时错误:将Microsoft LoopBack Adapter配置为系统的主网络适配器。
- FMM算法 分词
- 不简单的手机定位
- apache setup
- c++语言之覆盖与隐藏
- HBase二级索引与Join
- ubuntu下解决字体问题的完美方法
- mysql setup
- SETUP PHP
- SETUP&CONFIG Samba