[Tutor] matching words from a text to keys in a dictionary

Karjer Jdfjdf karper12345 at yahoo.com
Mon Mar 8 17:05:54 CET 2010


I want to compare words in a text to a dictionary with values attached to the words.

The dictionary  looks like:
{ word1: [1,2,3] word2: [2,3,4,a,b ] ... }

I'm trying to find a way to achieve this, but I'm having trouble getting corrects results.

If I do the def below, nothing is matched.

def searchWord(text, dictionary):
    text = text.split()
    for word in text:
        print word
        if word in dictionary:
            value = dictionary[str(word)]
        else:
            value = None
        return w

If I try another way, I keep getting errors:

def searchWord(text, dictionary):
    for key in dictionary:
        value = dictionary[key]
        if re.search(key, text):
            w = value
    else:
        w = None
    return w


TypeError: list indices must be integers, not str

I'm probably overlooking something and suspect that I'm doing it the slow way, so any advice is welcome. 



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100308/80ebaa1c/attachment.html>


More information about the Tutor mailing list