• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: returning math answers given a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: returning math answers given a string


  • Subject: Re: returning math answers given a string
  • From: Kai <email@hidden>
  • Date: Sun, 26 Jan 2003 03:11:55 +0000

on Sat, 25 Jan 2003 11:41:46 -0700, Michael Glasser <email@hidden>
wrote:

> Is there any way to have AppleScipt read a text message such as "what
> is 3 + 3" and have it calculate the math so it can return the answer
> (6, I believe. :) )
>
> I would also like it to be able to do other math operations, and
> recognize other terms like "plus" instead of "+".
>
> Anyone ever do this before? Is there a semi-simple / efficient way?
>
> If I need to have it parse the text in some clever way that is fine, I
> do not need it to do large numbers... single digit would be good,
> double digit would be great, triple would be overkill.

Try something like this, Michael. It includes a handler to convert
pre-defined terms (w) to operators (o):

=============================================

property w : {"plus", "minus", "times", "multiplied by", "divided by"}
property o : {"+", "-", "*", "*", "/"}
property v : "0123456789.()+-*/"

to getAnswer to q
set {q, a} to {convertTerms from q, {}}
repeat with c in q
if c is in v then set a's end to c's contents
end repeat
run script (a as string)
end getAnswer

to convertTerms from q
set tid to text item delimiters
repeat with n from 1 to count w
set i to w's item n
if i is in q then
set text item delimiters to i
set {q, text item delimiters} to {q's text items, o's item n}
set q to q as string
end if
end repeat
set text item delimiters to tid
q
end convertTerms

getAnswer to "what is 3 + 3?"
--> 6

=============================================

--
Kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: returning math answers given a string
      • From: Kai <email@hidden>
  • Prev by Date: Re: Creating a list of alias from folder contents
  • Next by Date: Re: returning math answers given a string
  • Previous by thread: Re: returning math answers given a string
  • Next by thread: Re: returning math answers given a string
  • Index(es):
    • Date
    • Thread