Python中文糾錯(cuò)的簡(jiǎn)單實(shí)現(xiàn)
介紹
這篇文章主要是用 Python 實(shí)現(xiàn)了簡(jiǎn)單的中文分詞的同音字糾錯(cuò),目前的案例中只允許錯(cuò)一個(gè)字,自己如果有興趣可以繼續(xù)優(yōu)化下去。具體步驟如下所示:
- 先準(zhǔn)備一個(gè)文件,里面每一行中放一個(gè)中文分詞,我這里的文件是下面代碼中的 /Users/wys/Desktop/token.txt ,你們可以改成自己,再運(yùn)行代碼
- 將構(gòu)建一個(gè)前綴樹(shù)類(lèi),實(shí)現(xiàn)插入功能,將所有的標(biāo)準(zhǔn)分詞都插入到前綴樹(shù)中,另外實(shí)現(xiàn)一個(gè)搜索功能,用來(lái)搜索分詞
- 將輸入的錯(cuò)誤分詞中的每個(gè)字都找出 10 個(gè)同音字,將每個(gè)字都用 10 個(gè)同音字替換,結(jié)果可以最多得到 n*10 個(gè)分詞,n 為分詞的長(zhǎng)度,因?yàn)橛械囊艨赡軟](méi)有 10 個(gè)同音字。
- 將這些分詞都經(jīng)過(guò)前綴樹(shù)的查找,如果能搜到,將其作為正確糾正就過(guò)返回
代碼
import re,pinyin from Pinyin2Hanzi import DefaultDagParams from Pinyin2Hanzi import dag class corrector(): def __init__(self): self.re_compile = re.compile(r'[\u4e00-\u9fff]') self.DAG = DefaultDagParams() # 將文件中的詞讀取 def getData(self): words = [] with open("/Users/wys/Desktop/token.txt") as f: for line in f.readlines(): word = line.split(" ")[0] if word and len(word) > 2: res = self.re_compile.findall(word) if len(res) == len(word): ## 保證都是漢字組成的分詞 words.append(word) return words # 將每個(gè)拼音轉(zhuǎn)換成同音的 10 個(gè)候選漢字, def pinyin_2_hanzi(self, pinyinList): result = [] words = dag(self.DAG, pinyinList, path_num=10) for item in words: res = item.path # 轉(zhuǎn)換結(jié)果 result.append(res[0]) return result # 獲得詞經(jīng)過(guò)轉(zhuǎn)換的候選結(jié)結(jié)果 def getCandidates(self, phrase): chars = {} for c in phrase: chars[c] = self.pinyin_2_hanzi(pinyin.get(c, format='strip', delimiter=',').split(',')) replaces = [] for c in phrase: for x in chars[c]: replaces.append(phrase.replace(c, x)) return set(replaces) # 獲得糾錯(cuò)之后的正確結(jié)果 def getCorrection(self, words): result = [] for word in words: for word in self.getCandidates(word): if Tree.search(word): result.append(word) break return result class Node: def __init__(self): self.word = False self.child = {} class Trie(object): def __init__(self): self.root = Node() def insert(self, words): for word in words: cur = self.root for w in word: if w not in cur.child: cur.child[w] = Node() cur = cur.child[w] cur.word = True def search(self, word): cur = self.root for w in word: if w not in cur.child: return False cur = cur.child[w] if cur.word == False: return False return True if __name__ == '__main__': # 初始化糾正器 c = corrector() # 獲得單詞 words = c.getData() # 初始化前綴樹(shù) Tree = Trie() # 將所有的單詞都插入到前綴樹(shù)中 Tree.insert(words) # 測(cè)試 print(c.getCorrection(['專塘街道','轉(zhuǎn)塘姐道','轉(zhuǎn)塘街到']))
結(jié)果
打印結(jié)果為:
['轉(zhuǎn)塘街道', '轉(zhuǎn)塘街道', '轉(zhuǎn)塘街道']
可以看出都糾正成功了,有一定的效果 ,之后會(huì)繼續(xù)優(yōu)化。
到此這篇關(guān)于Python中文糾錯(cuò)的簡(jiǎn)單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python中文糾錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pandas中apply和transform方法的性能比較及區(qū)別介紹
這篇文章主要介紹了pandas中apply和transform方法的性能比較,在文中給大家講解了apply() 與transform()的相同點(diǎn)與不同點(diǎn),需要的朋友可以參考下2018-10-10Python?中的json常見(jiàn)用法實(shí)例詳解
這篇文章主要介紹了Python?中的json常見(jiàn)用法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12python Tornado異步使用場(chǎng)景源碼解析
這篇文章主要為大家介紹了python Tornado異步使用場(chǎng)景源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09恢復(fù)百度云盤(pán)本地誤刪的文件腳本(簡(jiǎn)單方法)
下面小編就為大家?guī)?lái)一篇恢復(fù)百度云盤(pán)本地誤刪的文件腳本(簡(jiǎn)單方法)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10樹(shù)莓派與PC端在局域網(wǎng)內(nèi)運(yùn)用python實(shí)現(xiàn)即時(shí)通訊
這篇文章主要為大家詳細(xì)介紹了樹(shù)莓派與PC端在局域網(wǎng)內(nèi)運(yùn)用python實(shí)現(xiàn)即時(shí)通訊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06