1

I'm learning Python and one of the labs requires me to import a list of words to serve as a dictionary, then compare that list of words to some text that is also imported. This isn't for a class, I'm just learning this on my own, or I'd ask the teacher. I've been hung up on how to covert that imported text to uppercase before making the comparision.

Here is the URL to the lab: http://programarcadegames.com/index.php?chapter=lab_spell_check

I've looked at the posts/answers below and some youtube videos and I still can't figure out how to do this. Any help would be appreciated.

Convert a Python list with strings all to lowercase or uppercase

How to convert upper case letters to lower case

Here is the code I have so far:

# Chapter 16 Lab 11

import re

# This function takes in a line of text and returns
# a list of words in the line.
def split_line(line):
    return re.findall('[A-Za-z]+(?:\'[A-Za-z]+)?',line)

dfile = open("dictionary.txt")

dictfile = []
for line in dfile:
    line = line.strip()
    dictfile.append(line)

dfile.close()

print ("--- Linear Search ---")

afile = open("AliceInWonderLand200.txt")

for line in afile:
    words = []
    line = split_line(line)
    words.append(line)
    for word in words:   
        lineNumber = 0
        lineNumber += 1
        if word != (dictfile):
            print ("Line ",(lineNumber)," possible misspelled word: ",(word))

afile.close()
Community
  • 1
  • 1
user30869
  • 19
  • 1
  • Like Peter Norvig's famous spell checker: http://norvig.com/spell-correct.html. I'd advise that you decompose the problem. Reading the dictionary is the least of your worries. It's what comes after that's the magical bit. – duffymo Jul 05 '13 at 14:23

1 Answers1

1

Like the lb says: You use .upper():

dictfile = []
for line in dfile:
    line = line.strip()
    dictfile.append(line.upper()) # <- here.
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • The dictionary words are already uppercase, it's the AliceInWonderland text that is not. I tried "words.append(line.upper())" and I get back the "AttributeError: 'list' object has no attribute 'upper'". I may have misunderstood your answer. – user30869 Jul 05 '13 at 14:33
  • @user30869: Yes, obviously you need to do `.upper()` there as well. – Lennart Regebro Jul 05 '13 at 14:34