У меня есть код, который принимает текстовый файл и анализирует его, чтобы получить частоту каждого слова и сохранить его в словаре.
# Load a corpus and build a language model
def load_model(filename):
"""Loads a corpus file and builds a language model consisting of word:frequency.
Converts all words to lowercase, strips punctuation, and ignores any non-alphabetic characters."""
dictionary = {}
f = open(filename)
for line in f:
words = line.split(' ') # Split each word and iterate through.
for string in words:
for c in string: # Check each character value for punctuation or numeric type.
if c in Punct:
string = string.replace(c,"")
if c.isdigit()
print String + ' is not formattable.\n'
break
string = string.lower()
if string in dictionary:
dictionary[string] = dictionary[string] + 1
else:
dictionary[string] = 1
f.close()
return dictionary
Мой вопрос: мне нужен перерыв, чтобы закончить проверку всей строки, а не только для завершения проверки символов.
Завершает ли break цикл, в котором он находится, или первый цикл: ("for line in f")
И continue просто завершит этот конкретный цикл.
Я хочу, чтобы он заканчивал проверку всей строки и переходил к следующей строке в словах.