时间:2021-05-22
正向最大匹配
# -*- 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()以上这篇python正向最大匹配分词和逆向最大匹配分词的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
逆向最大匹配方法有正即有负,正向最大匹配算法大家可以参阅逆向最大匹配分词是中文分词基本算法之一,因为是机械切分,所以它也有分词速度快的优点,且逆向最大匹配分词比
1、分词技巧中文分词是一门搜索引擎学问,无论是正向逆向最大最小匹配,还是理解、统计匹配都需要我们针对搜索引擎观察,如果您细心,百度的分词算法又改变了,正向逆向匹
本文实例讲述了PHP实现的最大正向匹配算法。分享给大家供大家参考,具体如下:正向最大匹配算法:从左到右将待分词文本中的几个连续字符与词表匹配,如果匹配上,则切分
近来因为工作原来,研究了一下中文分词,也就写了一个中文分词的程序.采用的是逆向最大匹配算算法.使用示例:
前言大家都知道,英文的分词由于单词间是以空格进行分隔的,所以分词要相对的容易些,而中文就不同了,中文中一个句子的分隔就是以字为单位的了,而所谓的正向最大匹配和逆