本文整理汇总了Python中dictionary.Dictionary.update方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.update方法的具体用法?Python Dictionary.update怎么用?Python Dictionary.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dictionary.Dictionary
的用法示例。
在下文中一共展示了Dictionary.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: checkFile
# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import update [as 别名]
def checkFile(file_name, dictionary_file="words.dat"):
# Set up dictionary based on words.dat
d = Dictionary(file_name=dictionary_file)
file_in = open(file_name, 'r')
file_out = open("{}.out".format(file_name), 'w')
current_word = ""
while True:
# Read one character at a time from the input file
next_char = file_in.read(1)
# Exit the loop when there's nothing else to read
if not next_char:
break
if next_char in d.ALLOWED_LETTERS:
current_word += next_char
else:
# Verify the current_word with the dictionary
resp, current_word = d.verify(current_word)
if not resp: # Word was not found in dictionary
resp, new_word = getUserResponse(current_word)
d.update(resp, current_word, new_word)
current_word = new_word
file_out.write(current_word)
current_word = ""
file_out.write(next_char)
file_in.close()
file_out.close()
print("Spellchecked file written to {}.out.".format(file_name))
示例2: PinyinLookup
# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import update [as 别名]
class PinyinLookup() :
def __init__( self ) :
self.dict = Dictionary()
self.spliter = PinyinSpliter()
self.fitter = Fitter()
self.picker = Picker( self.dict )
#self.picker.set( [], [], True )
self.cache = [ [ 0, [], "" ] ]
self.candCacheIndex = 0
self.candStartIndex = 0
self.candList = []
def load( self, filePath ) :
newKeys = self.dict.load( filePath )
print "start build index"
newPinyinSet = set()
for key in newKeys :
if key.count( "'" ) <= 0 :
self.fitter.pinyinSet.add( key )
newPinyinSet.add( key )
self.fitter.dictTree.addKey( key )
for pinyin in newPinyinSet :
self.spliter.beginCharSet.add( pinyin[0] )
self.spliter.pinyinTree.addPath( pinyin )
print "built"
def update( self, key, word, freq ) :
newKey = self.dict.update( key, word, freq )
if newKey :
if newKey.count( "'" ) <= 0 :
self.fitter.pinyinSet.add( newKey )
self.spliter.beginCharSet.add( newKey[0] )
self.spliter.pinyinTree.addPath( newKey )
self.fitter.dictTree.addKey( newKey )
def subFit( self, fitList, pinyinStringList ) :
subFitPoint = -999
#for key in fitList :
for i in range( len( fitList ) ) :
key = fitList[i]
#currentSubFitPoint = len( key ) - key.count( "'" ) - len( self.spliter.code )
currentSubFitPoint = key.count( "'" ) + 1 - len( pinyinStringList[i].string )
#print key, pinyinStringList[i].string, currentSubFitPoint
if currentSubFitPoint > 0 :
currentSubFitPoint = -998
if currentSubFitPoint > subFitPoint :
subFitPoint = currentSubFitPoint
#print key, currentSubFitPoint, subFitPoint
newFitList = []
preeditList = []
for i in range( len( fitList ) ) :
key = fitList[i]
currentSubFitPoint = key.count( "'" ) + 1 - len( pinyinStringList[i].string )
if currentSubFitPoint >= subFitPoint :
newFitList.append( key )
preeditList.append( str( pinyinStringList[i] ) )
#print newFitList, newPreeditList
return newFitList, preeditList
def append( self, code ) :
#print "append", code
self.spliter.append( code )
fitList = []
#preeditList = []
pinyinStringList = []
fitPoint = -999
for pinyinString in self.spliter.stack :
#print pinyinString
if pinyinString.length < len( self.spliter.code ) :
pass
else :
currentFitPoint, keys = self.fitter.fit( pinyinString.string )
#print currentFitPoint, keys
if currentFitPoint > fitPoint :
fitPoint = currentFitPoint
fitList = []
preeditList = []
fitList.extend( keys )
#preeditList.extend( [ str( pinyinString ) ] * len( keys ) )
pinyinStringList.extend( [ pinyinString ] * len( keys ) )
elif currentFitPoint == fitPoint :
fitList.extend( keys )
#preeditList.extend( [ str( pinyinString ) ] * len( keys ) )
pinyinStringList.extend( [ pinyinString ] * len( keys ) )
fitList, preeditList = self.subFit( fitList, pinyinStringList )
#print fitList
self.picker.set( fitList, preeditList, True )
cache = [ fitPoint, fitList, preeditList ]
self.cache.append( cache )
self.candList = []
self.candCacheIndex = len( self.cache ) - 1
self.candStartIndex = 0
def pop( self ) :
if len( self.cache ) > 1 :
self.spliter.pop()
self.cache = self.cache[:-1]
cache = self.cache[-1]
fitList = cache[1]
preeditList = cache[2]
self.picker.set( fitList, preeditList, True )
self.candList = []
self.candCacheIndex = len( self.cache ) - 1
self.candStartIndex = 0
#.........这里部分代码省略.........