本文整理汇总了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