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

Karjer Jdfjdf karper12345 at yahoo.com
Mon Mar 8 18:14:29 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 ] ... }
>Please give the actual dictionary, not something that it 'looks like' - an
>actual dictionary would never 'look like' this: it has commas between the
>elements, and quotes around anything that is a word.

Sorry, I was a bit quick typing it, forgetting the proper format. The dictionary looks something like this (simplified):

{ 'chinese': ['china', '17', '3'], 'vietnamese': ['vietnam', '89'] ... }

A text might be something like this and should get 2 matches:
 'A vietnamese farmer ate a chinese noodle soup, but it was not made in china'

>> 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
>That's quite a clear statement: If this is indeed caused by the function you
>show here, then the only explanation is that 'dictionary' is not a
>dictionary at all, but a list.

Actually the values are lists.

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

More information about the Tutor mailing list