Last active
August 29, 2015 14:03
-
-
Save drdrang/4adb7445f9f90b387f5b to your computer and use it in GitHub Desktop.
Evaluate an expression with a numerical answer using Wolfram Alpha and return the result as an equation. Also open the Alpha page in a browser (Mac only). Intended to be used as a BBEdit Text Filter but can be used from the command line. See http://www.leancrew.com/all-this/2014/07/evaluating-latex-with-eddie-and-alpha/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from urllib import quote_plus | |
from sys import stdin | |
from subprocess import call | |
import requests | |
import xml.etree.ElementTree as ET | |
appID = 'XXXXXX-YYYYYYYYYY' # Placeholder. You'll need to get an ID from http://products.wolframalpha.com/api/ | |
def get_plaintext_query(latex): | |
r = requests.get('http://api.wolframalpha.com/v2/query?input=%s&appid=%s' % (quote_plus(latex), appID)) | |
root = ET.fromstring(r.text.encode('utf8')) | |
for pod in root: | |
if pod.attrib.get('title', '') in ['Decimal approximation', 'Definite integral', 'Decimal form', 'Result']: | |
subpod = pod.find('subpod') | |
result = subpod.find('plaintext').text | |
if pod.attrib.get('title', '') == 'Definite integral': | |
return result.split('~~')[1] | |
else: | |
return result | |
if __name__ == '__main__': | |
latex = stdin.read() | |
alphaURL = "http://www.wolframalpha.com/input/?i=%s" % quote_plus(latex) | |
print latex + ' = ' + get_plaintext_query(latex) | |
call(['open', alphaURL]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment