def look_up_dictionary(text, dictionary): cut_point = 0 words = [] text_length = len(text) while cut_point < text_length: step = 0 i = 0 text_remain_length = len(text[cut_point:]) while i < text_remain_length: i += 1 word = text[cut_point:cut_point+i] ifword in dictionary: step = i
# 字典里没有或者就是单字词 ifstep == 0: step = 1
words.append(text[cut_point:cut_point+step]) cut_point += step return words
defget_dictionary(filename): dictionary = [] with open(filename) as f: for line in f: if line.startswith('#') or line.startswith('%'): continue word = line.split()[1] dictionary.append(word) return dictionary