Hello all,

I've been set a lab assignment and am really struggling to get to grips with it. The aim to to create a "grass-roots" program that doesn't make heavy use of built in functions. Namely "string.replace()" in this case!

Any ideas as to why the below code doesn't work? I've been playing around with it for a while now to no avail.. Just some background though, I only started Python a couple of weeks ago so please be gentle!

def removeStopWords(text_input):
    line_stop_words = []
    stop_words = "a","It","i","it","am","at","on","in","of","to","is","so","too","my","The","the","and","but","are","very","here","even","from","them","then","than","this","that","though"
    word = "" 
    for word in text_input:
        word_list = string.split(text_input)
        new_string = ""
        for word in word_list:
            if word not in stop_words:
                new_string = new_string + word + " "
        new_string = string.split(new_string)
        line_stop_words = line_stop_words + [new_string]
    return(line_stop_words)

Cheers,

I would suggest a few print statements to see what the program is doing. Other than that, you did not say what you wanted the program to do and what the problem is (and I have learned that you are always wrong if you try to guess). Also, are you allowed to use append (to the list directly) instead of using "new-string" and what happens if "text_input" contains two words that are the same i.e. the word will be added twice.

## add/uncomment the commented print statements once you
## have fixed the first part of the program
##
def removeStopWords(text_input):
    line_stop_words = []
    stop_words = "a","It","i","it","am","at","on","in","of","to","is","so","too","my","The","the","and","but","are","very","here","even","from","them","then","than","this","that","though"
    print stop_words

    word = "" 
    for word in text_input:
        print "Word in text_input", word
        word_list = string.split(text_input)
        new_string = ""
        for word in word_list:
##            print "word in word_list", word  ## not the same "Word" as the above
            if word not in stop_words:
                new_string = new_string + word + " "
##                print "new string is now", new_string  
        new_string = string.split(new_string)
##        print "new_string after split", new_string
        line_stop_words = line_stop_words + [new_string]
##        print "line stop words is now", line_stop_words
    return(line_stop_words)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.