0

So I'm supposed to write some code that takes in some lines of text, reworks them by removing all non-key words, punctuation, etc. and finally prints each keyword and which lines it appears on. At one point my code was working then, seemingly without changing anything I started getting this error and I have no idea why. I'm not looking for ways to make the code shorter or anything, I realise it's far from ideal. I would just like to know how to fix this error.

EDIT- Error is on line 58, in createindex if word not in index:

Here is the code:

from string import *

# Program to index sentences

stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
              "of", "from", "here", "even", "the", "but", "and", "is", "my", \
              "them", "then", "this", "that", "than", "though", "so", "are" ]

punctuation = [".",",",":",";","!","?","&","'"]
stemming=["s","es","ed","er","ly","ing"]
text={}
reworkedtext={}

def inserttext(text):    #Function to insert lines of text
    linecount=1
    print "Please insert text here:"
    line = ""
    while line!=".":
        line = raw_input()
        text[linecount]=line
        linecount+=1

def reworktext(text):   #Reworks the text by removing punctuation and making everything lowercase
    for line in text:
        reworkedtext[line]=""
        for character in range(0,len(text[line])):
            if text[line][character] not in punctuation:
                reworkedtext[line]=reworkedtext[line]+lower(text[line][character])

def removestopwords(reworkedtext):   #Removes stopwords
    for line in reworkedtext:
        wordcount=0
        reworkedtext[line]=split(reworkedtext[line])
        for word in range(0,len(reworkedtext[line])):
            if reworkedtext[line][wordcount] in stopWords:
                del(reworkedtext[line][wordcount])
            else:
                wordcount+=1

def stemwords(reworkedtext):  #Stems all words
    for line in reworkedtext:
        for word in range(0,len(reworkedtext[line])):
            if reworkedtext[line][word][-2:] in stemming:
                reworkedtext[line][word]=reworkedtext[line][word][:-2]
            if reworkedtext[line][word][-3:] in stemming:
                reworkedtext[line][word]=reworkedtext[line][word][:-3]
            if reworkedtext[line][word][-1:] in stemming:
                reworkedtext[line][word]=reworkedtext[line][word][:-1]

def createindex(reworkedtext): #creates index and prints it
    linecount=1
    for line in reworkedtext:
        for word in range(0,len(reworkedtext[line])):
            if  word not in index:
                index[word]=""
                index[word]=str(line)
                linecount+=1
            elif index[word]!=str(line):
                index[word]=index[word]+", "+str(line)
    for words in index:
        print words, index[words]

inserttext(text)

reworktext(text)

removestopwords(reworkedtext)

stemwords(reworkedtext)

createindex(reworkedtext)
2
  • Seriously? You're not even going to tell us which line it's on? Commented Nov 27, 2013 at 21:55
  • Crap, sorry! It's File "C:\Program Files (x86)\Python\indexer.py", line 75, in <module> createindex(reworkedtext) File "C:\Program Files (x86)\Python\indexer.py", line 58, in createindex if word not in index: Commented Nov 27, 2013 at 21:59

1 Answer 1

1

Seems like you forgot to initialze your index dictionary

def createindex(reworkedtext): #creates index and prints it
    linecount=1
    index = {} # <----------- add this line and see what's what :)
    for line in reworkedtext:
        for word in range(0,len(reworkedtext[line])):
            if  word not in index:
                index[word]=""
                index[word]=str(line)
                linecount+=1
            elif index[word]!=str(line):
                index[word]=index[word]+", "+str(line)
    for words in index:
        print words, index[words]
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's it. I must have accidentally deleted it when I was removing some test code, but with such an error code the thought would have never even crossed my mind! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.