Skip to content

Instantly share code, notes, and snippets.

@Di10n
Created May 25, 2024 05:54
Show Gist options
  • Save Di10n/f718b507ece023a93bbbab2ae7ebf670 to your computer and use it in GitHub Desktop.
Save Di10n/f718b507ece023a93bbbab2ae7ebf670 to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as ET
from urllib.request import urlopen
import urllib.parse, os
os.system('clear')
appid = "[redacted]"
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
blacklist = ["Input", "Continued fraction", "Property", "Number name", "Percent increase", "Constant name", "Rational form", "Repeating decimal", "Egyptian fraction expansion", "Percentage", "Geometric figure", "Approximations about x = 0 up to order 4", "Implicit derivatives", "Polynomial discriminant", "Properties as a real function", "Partial derivatives"]
library = {}
def clean(item):
return item.replace("...", "")
def eqclean(item):
if "=" in item:
return item.split("=")[1][1:]
elif "≈" in item:
return item.split("≈")[1]
else:
return item
def brackets(string, index1, index2):
string = string[:index1] + "[" + string[index1:]
string = string[:index2] + "]" + string[index2:]
return string
def gobang(string):
if "." not in string: return string
decimalindex = string.index(".")
endindex = decimalindex + 1
try:
while endindex <= len(string) and string[endindex] in digits:
endindex = endindex + 1
except IndexError:
endindex = len(string) + 1
endindex = endindex - 1
if endindex - decimalindex > 6:
string = brackets(string, decimalindex, endindex+2)
decimalportion = string.split("[")[1].split("]")[0]
rounded = str(round(float(decimalportion), 6))[1:]
if "e" in rounded: rounded = ".0"
string = string.replace("[" + decimalportion + "]", rounded)
string = string.replace(".", "DECIMALPOINT", 1)
return gobang(string)
def grod(string):
string = string.replace("...", "DOTDOTDOT")
string = gobang(string)
string = string.replace("DECIMALPOINT", ".")
string = string.replace("DOTDOTDOT", "...")
return string
def numsof(string):
string = list(string)
valids = digits + ["."]
string = [i for i in string if i in valids]
return "".join(string)
def substitute(query):
for i in library.keys():
catch = "[" + i + "]"
if "#" + catch in query:
query = query.replace("#" + catch, "(" + numsof(library[i]) + ")")
elif "&" + catch in query:
query = query.replace("&" + catch, "(" + library[i] + ")")
else:
query = query.replace(catch, "(" + eqclean(library[i]) + ")")
return query
def hasthings(l):
for i in l:
if i: return True
return False
def go(run):
run = run + 1
if (run - 1): print()
query = input("Input: ")
if query == "exit": return None
needsinterpreting = False
if "[" in query:
query = substitute(query)
needsinterpreting = True
queryurl = urllib.parse.quote(query)
url = "http://api.wolframalpha.com/v2/query?appid=" + appid + "&input=" + queryurl
xml = urlopen(url)
xmldict = {}
result = ET.parse(xml)
for pod in result.findall("pod"):
title = pod.get("title")
if title not in blacklist:
subpods = pod.findall("subpod")
for subpod in subpods:
text = subpod.find("plaintext").text
if title not in xmldict.keys():
xmldict[title] = [text]
else:
xmldict[title].append(text)
xmldict = {i: xmldict[i] for i in xmldict if hasthings(xmldict[i])}
titles = list(xmldict.keys())
printqueue = []
index = 0
for title in titles:
if title == "Input interpretation":
print("Interpretation: " + xmldict[title][0])
needsinterpreting = False
else:
for i in range(len(xmldict[title])):
letter = alphabet[index]
index = index + 1
label = str(run) + letter
item = xmldict[title][i]
if "." in item: item = grod(item)
library[label] = clean(item)
printqueue.append("[" + label + "] " + str(item))
if not printqueue:
print("\x1b[38;5;9m" + "Wolfram|Alpha did not understand your input" + "\x1b[0m")
if run == 1: print("")
run = run - 1
else:
if needsinterpreting:
print("Interpretation: " + query)
for i in printqueue:
print("\x1b[38;5;214m" + i + "\x1b[0m")
print(titles)
go(run)
go(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment