Question

I have code that takes in a text file and parses it to get the frequency of each word and store it in a dictionary.

# 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

My question is I need break to end checking the entire string, not just to end checking a characters.

Does break end the loop it is located in or does it end the first loop: ("for line in f")

And, continue would simply end that particular loop.

I want it so that it ends checking the entire string and it moves on to the next string in words.

Was it helpful?

Solution

From the documentation:

The break statement, like in C, breaks out of the smallest enclosing for or while loop.

According to this question, Python does not support a construct like break 2, as PHP does.

OTHER TIPS

break will break out of the innermost loop it's in.

You can break out of an outer loop using what @farmerjoe suggested.

But I don't see why you want to do that. The break you have there seems fine as it is: it will stop processing the characters of the current string and move on to checking the next string.

There are some errors in your code. I fixed them for you and some style issues:

def load_model(filename):
    dictionary = {}
    with open(filename) as f:
        for line in f:
            words = line.split(' ')  # Split each word and iterate through.
            for word in words:
                for c in word:  # Check each character value for punctuation or numeric type.
                    if c in Punct:
                        word = word.replace(c, "")
                    if c.isdigit():
                        print word + ' is not formattable.\n'
                        break

                word = word.lower()
                if word in dictionary:
                    dictionary[word] += 1
                else:
                    dictionary[word] = 1
    return dictionary

break ends the innermost/immediate loop it is included in, i.e. the one it is directly in the scope of.

for x in X:
    for y in Y:
        break

The x loop will run to completion, the y loop will break.

You can cause a break on the outer loop by maybe setting a variable as a flag:

break_outer = False
for x in X:
    for y in Y:
        if condition:
            break_outer = True
            break
    if break_outer:
        break

Example:

for x in range(3):
    for y in range(2):
        if x == 2:
            break
        print "x =",x,"y =",y

output:

>>> x = 0 y = 0
>>> x = 0 y = 1
>>> x = 2 y = 0
>>> x = 2 y = 1

And to break an outer loop you would pass a variable up:

break_outer = False
for x in range(3):
    for y in range(2):
        if x == 2:
            break_outer = True
            break
        print "x =",x,"y =",y
    if break_outer:
        break

output:

>>> x = 0 y = 0
>>> x = 0 y = 1

continue skips the rest of the code remaining in the loop and continues to the next iteration in the for loop:

for i in range(3):
    if i == 1:
        continue
    print i

output:

>>> 0
>>> 2

Your code seems to do what you are asking though, breaking and moving onto the next word...was there something else about the code that was producing an undesired result?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top