自然语言处理——分词系统(双向最大匹配)
生活随笔
收集整理的這篇文章主要介紹了
自然语言处理——分词系统(双向最大匹配)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
算法分析
雙向最大匹配,顧名思義就是將正向最大匹配和逆向最大匹配結合在一起
具體算法,詳見正向最大匹配
將而二者得到的結果進行比較
1.如果正反向分詞結果詞數不同,則取分詞數量較少的那個。
2.如果分詞結果詞數相同
.a.分詞結果相同,就說明沒有歧義,可返回任意一個。
.b.分詞結果不同,返回其中單字較少的那個。
代碼實現
BMM.py
#實現切詞方法 def cut_words(raw_sentence, words_dic):#找到最大詞的長度max_length = max(len(word) for word in words_dic)sentence = raw_sentence.strip()words_length = len(sentence)#存儲切分好的分詞cut_words = []#開始切分while words_length > 0:#求算每次切分的長度max_cut_length = min(words_length, max_length)#切分出一個子串,從右側切分subSentence = sentence[-max_cut_length:]#切分一輪,在左側刪去一個字符 每成功匹配一次就進行breakwhile max_cut_length > 0:#成功匹配一個分詞if subSentence in words_dic:cut_words.append(subSentence)break#只剩下一個字elif max_cut_length == 1:cut_words.append(subSentence)break#都不符合,從左側去掉一個詞,長度減一,繼續循環else:max_cut_length -= 1subSentence = subSentence[-max_cut_length:]#將切掉的單詞(后側)刪去,將切掉的長度減去sentence = sentence[0:-max_cut_length]words_length -= max_cut_lengthcut_words.reverse()#words = '/'.join(cut_words)return cut_wordsFMM.py
#實現正向最大匹配法 def cut_words(raw_sentence, words_dic):#統計詞典種最長的詞(若小于待切分總長度,則每次從未匹配處找這么長的字符串開始匹配)max_length = max(1, 3) #len(word) for word in words_dicsentence = raw_sentence.strip() #移除字符串內的空格#統計序列長度words_length = len(sentence)#存儲切分好的詞語cut_word_list = []while words_length > 0:max_cut_length = min(max_length, words_length)subSentence = sentence[0 : max_cut_length]#進行一輪分詞,在左側切出一個詞while max_cut_length > 0:if subSentence in words_dic: #若待切分的詞在詞典中,則將其加入已分列表,跳出循環cut_word_list.append(subSentence)breakelif max_cut_length == 1: #剩下單個字,將其切分,并跳出循環cut_word_list.append(subSentence)breakelse: #都不符合則從右側去掉一個詞,重新分詞max_cut_length = max_cut_length - 1subSentence = subSentence[0 : max_cut_length]#將切掉的單詞刪去sentence = sentence[max_cut_length:]words_length = words_length - max_cut_length#words = "/".join(cut_word_list)#為了配合雙向匹配return cut_word_listBIMM.py
import FMM import BMMwords_dic = []def init():"""讀取詞典文件載入詞典:return:"""#with as 的用法噶with open("dict.txt", encoding="utf8") as dic_input:for word in dic_input:words_dic.append(word.strip())#實現雙向最大匹配法 正反相同返回分次數少的那個 def cut_words(raw_sentence, word_dic):bmm_word_list = BMM.cut_words(raw_sentence, word_dic)fmm_word_list = FMM.cut_words(raw_sentence, word_dic)bmm_size = len(bmm_word_list)fmm_size = len(fmm_word_list)#分詞結果數不同if bmm_size != fmm_size:if bmm_size < fmm_size:return bmm_word_listelse:return fmm_size#分詞結果數相同,1.結果相同,無歧義,返回任意一個 2。結果不同,返回單字少的那個else:Fsingle = 0Bsingle = 0isSame = Truefor i in range(bmm_size):#分詞結果不同if bmm_word_list[i] != fmm_word_list[i]: # not in也可以isSame = False#統計單個詞的數量if len(bmm_word_list[i]) == 1:Bsingle += 1if len(fmm_word_list[i]) == 1:Fsingle += 1if isSame == True:return bmm_word_list#分詞結果不同選詞數少的一個else:if Fsingle >= Bsingle:return bmm_word_listelse:return fmm_word_listdef main():"""于用戶交互接口:return:"""init()while True:print("請輸入您要分詞的序列")input_str = input()if not input_str:breakresult = cut_words(input_str, words_dic)result = '/'.join(result)print("分詞結果:")print(result) if __name__ == '__main__':main()總結
以上是生活随笔為你收集整理的自然语言处理——分词系统(双向最大匹配)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用户账号系统(python)
- 下一篇: 大学《数据库系统》课程设计报告