這篇文章將為大家詳細(xì)講解有關(guān)python正向大匹配分詞和逆向大匹配分詞的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有羅莊免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。正向大匹配
# -*- coding:utf-8 -*- CODEC='utf-8' def u(s, encoding): 'converted other encoding to unicode encoding' if isinstance(s, unicode): return s else: return unicode(s, encoding) def fwd_mm_seg(wordDict, maxLen, str): 'forward max match segment' wordList = [] segStr = str segStrLen = len(segStr) for word in wordDict: print 'word: ', word print "\n" while segStrLen > 0: if segStrLen > maxLen: wordLen = maxLen else: wordLen = segStrLen subStr = segStr[0:wordLen] print "subStr: ", subStr while wordLen > 1: if subStr in wordDict: print "subStr1: %r" % subStr break else: print "subStr2: %r" % subStr wordLen = wordLen - 1 subStr = subStr[0:wordLen] # print "subStr3: ", subStr wordList.append(subStr) segStr = segStr[wordLen:] segStrLen = segStrLen - wordLen for wordstr in wordList: print "wordstr: ", wordstr return wordList def main(): fp_dict = open('words.dic') wordDict = {} for eachWord in fp_dict: wordDict[u(eachWord.strip(), 'utf-8')] = 1 segStr = u'你好世界hello world' print segStr wordList = fwd_mm_seg(wordDict, 10, segStr) print "==".join(wordList) if __name__ == '__main__': main()
逆向大匹配
# -*- coding:utf-8 -*- def u(s, encoding): 'converted other encoding to unicode encoding' if isinstance(s, unicode): return s else: return unicode(s, encoding) CODEC='utf-8' def bwd_mm_seg(wordDict, maxLen, str): 'forward max match segment' wordList = [] segStr = str segStrLen = len(segStr) for word in wordDict: print 'word: ', word print "\n" while segStrLen > 0: if segStrLen > maxLen: wordLen = maxLen else: wordLen = segStrLen subStr = segStr[-wordLen:None] print "subStr: ", subStr while wordLen > 1: if subStr in wordDict: print "subStr1: %r" % subStr break else: print "subStr2: %r" % subStr wordLen = wordLen - 1 subStr = subStr[-wordLen:None] # print "subStr3: ", subStr wordList.append(subStr) segStr = segStr[0: -wordLen] segStrLen = segStrLen - wordLen wordList.reverse() for wordstr in wordList: print "wordstr: ", wordstr return wordList def main(): fp_dict = open('words.dic') wordDict = {} for eachWord in fp_dict: wordDict[u(eachWord.strip(), 'utf-8')] = 1 segStr = ur'你好世界hello world' print segStr wordList = bwd_mm_seg(wordDict, 10, segStr) print "==".join(wordList) if __name__ == '__main__': main()
關(guān)于“python正向大匹配分詞和逆向大匹配分詞的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文標(biāo)題:python正向最大匹配分詞和逆向最大匹配分詞的示例分析-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://muchs.cn/article26/dgcicg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、外貿(mào)建站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容