Download this file.
Feb 16, 17:28 - Hunter: What's nice about this program is that it is laid out clearly -- every section of it makes sense. There are a number of things that could be done to improve the austerity of the code, but they tend to rely on a more involved knowledge of the language, and aren't necessary in themselves. Hence the do_nothing function could be replaced by pass, the assignment of mispell[current_word] could be done without a temporary variable in the middle, the keys function could be used to get a list of words out of the mispell dictionary, and the reading of the file could be done with xreadlines for faster input. The only two things that will really need to be fixed will be the way the input file is read, and the way punctuation and capitals are dealt with.
Feb 22, 18:03 - Ping (line 1): There's no need to define this function (see line 19).
Feb 22, 18:04 - Ping (line 19): You could simply write pass instead of calling do_nothing . But even better, since there's no first clause for this if statement, just write if not test_word(current_word) .
Feb 22, 18:05 - Ping (line 15): The correct keyword for logical OR is or . The vertical bar is used for bitwise mask operations.
Feb 22, 18:05 - Ping (line 16): No need to say _len_ () . Just call len(current_word) . But there's no need for that either, since you can jsut write current_word[:-1] . A negative number as an index automatically counts back from the end of the word.
Feb 22, 18:06 - Ping (line 10): No need to call _len_ () . Just write len(word_list) .
Feb 22, 18:07 - Ping (line 13): No need to call __getitem__ () . Just write word_list[i] .
Please log in if you would like to add comments. | |
1 | def do_nothing(): | 2 | return 5 | 3 | | 4 | file = open("input.txt") | 5 | | 6 | mispell = {} ##### Dictionary for holding the mispelt words and the number of times they appear | 7 | | 8 | for current_line in file: | 9 | word_list = current_line.split() | 10 | list_len = word_list.__len__() | 11 | for i in range(list_len): ##### for each word in the line | 12 | | 13 | current_word = word_list.__getitem__(i) | 14 | | 15 | if current_word.endswith('.') | current_word.endswith(','): ##### for removing punctuation marks | 16 | current_word = current_word[0:current_word.__len__()-1] | 17 | | 18 | if test_word(current_word): ###### Using Scott's function for checking if the word exists. | 19 | do_nothing() | 20 | else: ###### if the word does not exist then put in in the mispell dictionary | 21 | if current_word in mispell: ###### if the word is already in the dictionary, simply increment counter | 22 | temp = mispell[current_word] | 23 | mispell[current_word] = temp + 1 | 24 | else: | 25 | mispell[current_word] = 1 | 26 | | 27 | ##### Move the words into a list, and then alphabetically sort the list | 28 | | 29 | word_list2 = [] | 30 | for i in mispell: | 31 | word_list2.append(i) | 32 | | 33 | word_list2.sort() | 34 | | 35 | ##### Write the output into a file | 36 | | 37 | output_file = open("output.txt", 'w'); | 38 | | 39 | for i in word_list2: | 40 | x = i + " " + "(" + str(mispell[i]) + ")" + "\n" | 41 | output_file.write(x) | 42 | output_file.flush() | 43 | | 44 | output_file.close() |
|