Re: Speech Recognition (Stock price project)
Re: Speech Recognition (Stock price project)
- Subject: Re: Speech Recognition (Stock price project)
- From: Paul Skinner <email@hidden>
- Date: Tue, 14 Aug 2001 23:35:57 -0400
on 8/13/01 10:50 PM, Dennis Wurster wrote:
>
First off, thanks to the list about my 'speech impediment' question.
>
You are all on the ball! I even got some responses that included some
>
very clever scripts! What a great way to learn! :-D
>
>
So now that my 3-year-old is into knock-knock jokes, I've installed
>
Speech Recognition ("Computer, Tell me a joke." ;-) and I'm
>
interested in knowing more about how to format scripts so that they
>
work will with SR. I've attempted to open the included 'Speakable
>
Items' with Script Debugger, but it was a no go.
>
>
I want to be able to say "Computer, Apple Stock Price", have the
>
script get the price, and then speak it back to me, while posting
>
something like "AAPL 19.29 +.29" to the little text window with the
>
caricature.
>
>
The thing is, I've already written the script, but I need to know
>
more about how to 'mold' it to fit the interface of SR nicely? I hope
>
I'm being clear...
>
>
Here's the script... it borrows heavily from one that I found on the
>
web, and requires the RegEx OSAX. (I really need to get around to
>
re-writing it without the OSAX...)
SNIP!
Here's my take on it. I use only the name of the company as the matching
phrase to listen for. The accuracy on matching seems to go down on longer
strings.
You could store a list of common requests and query a URL that can take
the company name and looks up the symbol, returning the price. Storing all
past data would allow you to have the change in price for any previous
request.
I replaced the RegEx call with a TID based parsing of the returned data.
I don't hate OSAX, I just like vanilla.
Since the stock symbols are hard coded I removed the call to the
capitalization routine, but I left a TIDs based uppercase routine at the
bottom. This routine will uppercase any length text in 26 loops.
--begin script
property lastdata : {price:18.7, symbol:"AAPL"}
----------------------------------------------------------------------------
set lastsymbol to symbol of lastdata
set lastPrice to price of lastdata
----------
tell application "Speech Listener"
set symbolname to ""
repeat while symbolname is ""
try
set choices to {"apple", "micro soft"}
listen for choices with prompt "Name a company to get a stock
quote for" giving up after 60
set symbolname to the result
if symbolname is "apple" then set daSymbol to "AAPL"
if symbolname is "micro soft" then set daSymbol to "MSFT"
end try
if symbolname is "" then say "I'm sorry, I didn't understand that."
end repeat
end tell
----------
set myFile to (((path to temporary items) as string) & "Stock Quote Script
Temp")
tell application "URL Access Scripting"
set x to download "
http://finance.yahoo.com/q" to file myFile replacing
yes form data "s=" & daSymbol & "&d=t"
quit
end tell
open for access file myFile
set quoteHTML to (read file myFile to (get eof file myFile))
close access file myFile
----------Use TIDs to parse the data and get the price.
set AppleScript's text item delimiters to "Last Trade"
set quoteHTML to (item 2 of (every text item of quoteHTML)) as text
set AppleScript's text item delimiters to "<b>"
set quoteHTML to (item 2 of (every text item of quoteHTML)) as text
set AppleScript's text item delimiters to "</b>"
set price to item 1 of (every text item of quoteHTML)
----------
if lastsymbol is daSymbol then
set difference to price - lastPrice
if difference is not 0 then
set thePoints to " points."
if difference is greater than 0 then
set updown to ". up by "
else
set updown to ". down by "
end if
else
set updown to ""
set difference to ""
set thePoints to ""
end if
else
set updown to ""
set difference to ""
set thePoints to ""
end if
----------
set theMessage to "The latest stock price for " & symbolname & " shares is "
& price & updown & difference & thePoints
say theMessage displaying theMessage
tell application "Finder"
move file myFile to the trash
end tell
set lastdata to {price:price, symbol:daSymbol}
--end script
on uppercase(thetext)
if class of thetext is string then
set desiredCase to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set currentCase to "abcdefghijklmnopqrstuvwxyz"
----------
repeat with thisCharacter from 1 to 26
set AppleScript's text item delimiters to (item thisCharacter of
currentCase)
set thetext to every text item of thetext as list
set AppleScript's text item delimiters to (item thisCharacter of
desiredCase)
set thetext to every text item of thetext as text
end repeat
----------
return thetext
else
return false
end if
end uppercase
--
Paul Skinner