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

Karjer Jdfjdf karper12345 at yahoo.com
Mon Mar 8 19:12:38 CET 2010


I brew this up. It works, but I think it will be slow with a long text and a big dictionary

def searchWord(text, dictionary):
    '''search for terms in dictionary(key) and retrieve value(keywords)'''
    text = text.split()
    w = []
    for word in text:
        if word in dictionary:
            print word
            l = dictionary[str(word)]
            for i in l:
                w = w + [i]               
        else:
            print "can't find anything"

    return w


dict1 = { 'had': ['1', '2'], 'little': ['a'] }
text = 'Mary had a little lamb'

w = searchWord(text, dict1)
print w





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


More information about the Tutor mailing list