本文整理匯總了Python中Dictionary.Dictionary.add_word方法的典型用法代碼示例。如果您正苦於以下問題:Python Dictionary.add_word方法的具體用法?Python Dictionary.add_word怎麽用?Python Dictionary.add_word使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Dictionary.Dictionary
的用法示例。
在下文中一共展示了Dictionary.add_word方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_dictionary
# 需要導入模塊: from Dictionary import Dictionary [as 別名]
# 或者: from Dictionary.Dictionary import add_word [as 別名]
def get_dictionary( ):
""" Build a Dictionary based on the Diceware data. """
dicto = Dictionary()
print 'Parsing Diceware data...'
i = 0;
nLines = 7780
# open file for reading
with open(Diceware.fname, 'r') as fid:
for line in fid:
tokens = Diceware.parse_line(line)
if tokens is None:
continue
# save data to list
word = Word(tokens['word'], -1, -1, i);
dicto.add_word(word)
# increment counter and show progress
i = i + 1;
progress = float(i) / float(nLines)
if (progress % 0.05) < 1e-4:
sys.stdout.write("\r%2.2f%%" %(progress*100))
sys.stdout.flush()
print '\nDone.'
return dicto
示例2: preproc
# 需要導入模塊: from Dictionary import Dictionary [as 別名]
# 或者: from Dictionary.Dictionary import add_word [as 別名]
def preproc(self):
""" normalize the data (clean/remove problematic characters) for processing """
print 'Pre-processing dictionary...'
# remove words that contain non-alphanumeric characters
dicto_new = Dictionary()
alpha = re.compile('[\W]')
num = re.compile('[0-9]')
for word in self.dicto.get_words_iter():
word_str = word.string
# remove numbers from words
word_str = num.sub('', word_str)
if len(word_str) <= 0:
continue
if not alpha.search(word_str):
word.set_string(word_str)
dicto_new.add_word(word)
print 'Done.'
self.dicto = dicto_new