You are not logged in.

#1 2008-07-07 01:55:37

czechman86
Member
From: In the Zaporozhian Host
Registered: 2008-05-26
Posts: 41
Website

[Solved] Python Help Word Search Program

One user accurately predicted that I would be using the forum for homework questions sooner or later and I am pleased to say that person was correct!

Basically what I am trying to do with this program is to create a dictionary that all words from a literary classic, in this case a Jane Austin book, are stored and sorted in. After the words are stored and sorted the most common ones will appear in the output. I beleive that the number that I want to appear is up to my descretion. I am however stuck with how to put the words into the dictionary. Here is my code, although it may be a bit messy:

#This is my second attempt at my assignment 4 and also the first time I have tried writing one on Arch Linux (http://www.archlinux.org) with KDEMod (http://kdemod.ath.cx/).
#I have consulted some online documentation and like the functions which I found in there, and thus will be attempting them with the functions. Links to this documentation will be featured at the bottom of my program.

#Initial variables and dictionaries

#Functions will be defined after initial variables
def cleanLine():
    lines = readBook.readlines()
    for line in lines:
        line = line.strip()
        line = line.lower()
        line = line.replace('.', ' ')
        line = line.replace('?', ' ')
        line = line.replace('!', ' ')
        line = line.replace(',', ' ')
        line = line.replace('"', ' ')
        line = line.replace("'", " ")
        line = line.replace(':', ' ')
        line = line.replace(';', ' ')
        line = line.replace('(', ' ')
        line = line.replace(')', ' ')
        return line

def sortWordDictionary():
    wordFreq = {}
    for word in readBook:
        if word in wordFreq:
                wordFreq[word] = wordFreq[word] + 1
            else:
            wordFreq[word] = 1

#This is where the action starts!
userInput = raw_input("Which text do you wish to analyze(Type 'PAP' for Pride and Prejudice or type 'MP' for Mansfield Park) ? ")

if userInput == 'PAP':
    readBook = open('PrideAndPrejudice.txt', 'r')
    readBook = readBook.split()
    print 'Processing PrideAndPrejudice.txt'
    print 'Done processing PrideAndPrejudice.txt'
    cleanLine()
    sortWordDictionary()
else:
    readBook = open('MansfieldPark.txt', 'r')
    print 'Processing MansfieldPark.txt'
    print 'Done processing MansfieldPark.txt'
    cleanLine()

Also, everytime I attempt to run this program, I recieve this error and it is driving me nuts:

[czechman86@aBox Python]$ python assignment41.py
Which text do you wish to analyze(Type 'PAP' for Pride and Prejudice or type 'MP' for Mansfield Park) ? PAP
Traceback (most recent call last):
  File "assignment41.py", line 37, in <module>
    readBook = readBook.split()
AttributeError: 'file' object has no attribute 'split'

Thanks in advance for anyhelp that you might be able to offer. Also, sorry if there are any spelling errors, as for some reason firefox 3 doesnt want to recognize bad spelling.

EDIT: Once the final program is complete, I will psot my solution

Last edited by czechman86 (2008-07-07 03:03:59)


Мой адрес не дом и не улица Мой адрес Советский Союз

Offline

#2 2008-07-07 02:34:45

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,384
Website

Re: [Solved] Python Help Word Search Program

czechman86 wrote:

Thanks in advance for anyhelp that you might be able to offer. Also, sorry if there are any spelling errors, as for some reason firefox 3 doesnt want to recognize bad spelling.

I don't help with homework, but you need to install a dictionary add-on for your language for firefox to do spell checking.

Offline

#3 2008-07-07 03:01:31

czechman86
Member
From: In the Zaporozhian Host
Registered: 2008-05-26
Posts: 41
Website

Re: [Solved] Python Help Word Search Program

Thanks Allan! those language packs sure did it. and as far as help, I think that I have somewhat got this and will save the rest of my questions for the tutor in class. Thanks again to everyone who might have taken a look!


Мой адрес не дом и не улица Мой адрес Советский Союз

Offline

Board footer

Powered by FluxBB