Human word reader
CM
cmpython at gmail.com
Sun May 16 14:57:30 EDT 2010
> > I need help with getting the useful information how do I get the place
> > if I don't now how long the string is?
>
> And is it supposed to handle
>
> for london give the weather to me
> for the london weather give me
>
> ...
>
> Do a search on "natural language processing"... You are at the level
> of algorithms, and algorithms are not limited to Python...
Yes, this is a major field of research. For basic purposes in Python,
maybe just try to trigger off the presence of the word "weather" and
the presence of a particular city's name. It will not work if the
user tries to trick it ("Don't give me the weather in London"), but,
like a search engine, it will more or less give what people want for
common queries. Like:
list_of_cities = ['london', 'moscow', 'new york', 'paris']
user_string = 'Give me the weather for London please.'
user_word_list = user_string.split() #if they put a comma after city,
this will be a problem
for word in user_word_list:
period_free_word = word.rstrip('.') #strips trailing period for
final word, in case there
lowered_word = period_free_word.lower() #makes it case
insensitive
if lowered_word in list_of_cities:
print 'The city is: ' + lowered_word
DoSomething(lowered_word) #go and get the weather data for
that city I guess
Che
More information about the Python-list
mailing list