Human word reader

T

timo verbeek

I'm planning to create a human word program
A human inputs a string
"Give me the weather for London please."
Then I will strip the string.
"weather for london"
Then I get the useful information.
what:"weather" where:"london"
After that I use the info.

I need help with getting the useful information how do I get the place
if I don't now how long the string is?
 
D

David Zaslavsky

Here's my take on that:

loc = re.search('for\s+(\w+)', string).group(1)

Not much different, really, but it does allow for multiple spaces (\s+) as
well as requiring at least one character in the word (\w+), and I use a
matching group to extract the location directly instead of splitting the
string "by hand".

:) David

Place starts always with for

Okay, much better.

Given that constraint, it looks like regular expression can do the job. I'm
not very experienced with regex, though.

\w* matches a whole word composed of letters and numbers by default.

'for London'
result.group().split()[1]

'London'

Cheers,
Xav

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)

iEYEABECAAYFAkvvDmYACgkQFmx38lx22rIszACgjwUu66R2y6cJi3ed9kVSnJ1Q
hYIAoL/kAf9AdX/9EiYXxsu831ZsjAnm
=Uzll
-----END PGP SIGNATURE-----
 
D

Dennis Lee Bieber

I'm planning to create a human word program
A human inputs a string
"Give me the weather for London please."
Then I will strip the string.
"weather for london"
Then I get the useful information.
what:"weather" where:"london"
After that I use the info.

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...
 
C

CM

I need help with getting the useful information how do I get the place
        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
 
C

CM

        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

I forgot to split on two delimiters (a space and a comma). See here:
http://mail.python.org/pipermail/tutor/2008-August/063570.html

Anyway, you get the idea...
 
G

Gregory Ewing

Dennis said:
And is it supposed to handle

for london give the weather to me
for the london weather give me

Or

Where can I buy some new weather boarding for my
house in London?

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,314
Latest member
HugoKeogh

Latest Threads

Top