W|A integration and Chem

General Discussions about the API

W|A integration and Chem

Postby oliver » Tue Jul 03, 2012 12:01 am

An upshot of the integration with Wolfram|Alpha into MorphEngine is that data types can make easy use of it.

For some examples, here're calls added to the ChemFormula custom data type, which is available through the Chem downloadable folder. (If you update it, remember to tap Install again to inject the latest code.)

Code: Select all
   "what": function(a) { var result = calculator.callWA("chemical name", a.stringValue); return ME.string.toString(result == a.stringValue ? "unknown" : result); },
   "melt": function(a) { var temp = parseFloat(calculator.callWA("melting point", a.stringValue).match(/[-]?\d.+/)); calculator.push(isNaN(temp) ? '"unknown"' : temp); return "'\u00b0C'"; },
   "boil": function(a) { var temp = parseFloat(calculator.callWA("boiling point", a.stringValue).match(/[-]?\d.+/)); calculator.push(isNaN(temp) ? '"unknown"' : temp); return "'\u00b0C'"; },


/NaCl what will return sodium chloride
/NH3 boil will return -33.33 '°C' (ready for use in conversions)

The following would be an easier-to-read alternative implementation:
Code: Select all
   "boil": function(a) { var arr = calculator.callWA("boiling point", a.stringValue).split(" "); calculator.push(Number(arr[0])); return calculator.quote(arr[1]); },

The Wolfram|Alpha return string (something like "-33.33 °C (degrees Celsius)") is split at the space characters into an array. The first item (the temperature) is pushed onto the stack. The second item (the unit) is returned as function result. This code, however, does not deal with some exceptional output that can occur ("oligonucleotide melting temperature | 58.7 °C (degrees Celsius)\n(based on Wetmur-Sninsky nearest-neighbor base interaction model)"), whereas the less readable version above does.

There's many more W|A queries one could wrap like that in function calls that would lend more abilities to this type.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron