Created
September 6, 2015 11:10
-
-
Save kingoflolz/9889d2934cef97ed7c5c to your computer and use it in GitHub Desktop.
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
from pprint import pprint | |
import json | |
def application(functor, arg, slash): | |
if functor is None or arg is None: | |
return None | |
if '/' in arg or '\\' in arg: | |
arg = '(' + arg + ')' | |
arg = slash + arg | |
if functor.endswith(arg): | |
res = functor[:-len(arg)] | |
if '/' in res or '\\' in res: | |
return res[1:-1] | |
return res | |
def fwd_app(left, right): | |
return application(left, right, '/') | |
def bwd_app(left, right): | |
return application(right, left, '\\') | |
levels = [[]] | |
things = input("Enter categories: ").split(" ") | |
for t in things: | |
levels[0].append([t]) | |
j = 0 | |
#pprint(levels) | |
while j < len(things): | |
i = 0 | |
levels.append([]) | |
#for t in range(j): | |
# while i < len(levels[j-1-t])-1: | |
# if t == 0: | |
# levels[j].append([]) | |
# for x in levels[j-1-t][i]: | |
# #print(levels[j][i]) | |
# for y in levels[j-1+t][i+1-t]: | |
# left, right = x, y | |
# if fwd_app(left, right) is not None: | |
# levels[j][-1].append(fwd_app(left, right)) | |
# if bwd_app(left, right) is not None: | |
# levels[j][-1].append(bwd_app(left, right)) | |
# i += 1 | |
#j += 1 | |
for current_cell in range(len(levels[j-1])-1): | |
levels[j].append([]) | |
for vertical in range(j): | |
left = levels[vertical][current_cell] | |
#print(levels[vertical],vertical) | |
right = levels[j-vertical-1][current_cell+vertical+1] | |
for l in left: | |
for r in right: | |
if l != "conj": | |
if bwd_app(l, r): | |
levels[j][-1].append(bwd_app(l, r)) | |
if fwd_app(l, r): | |
levels[j][-1].append(fwd_app(l, r)) | |
#levels[j][-1].append("left: " + str(vertical) +" ,"+ str(current_cell)+ " right: " + str(j-vertical-1) +" ,"+ str(current_cell+vertical+1) + "current: " + str(j) +" ,"+ str(len(levels[j])-1)) | |
else: | |
if "\\" in r or "/" in r: | |
r = "(" + r + ")" | |
levels[j][-1].append(r+"\\"+r) | |
j += 1 | |
chart = levels[::-1][1:] | |
#print(chart) | |
#pprint(levels) | |
#print(json.dumps(levels[::-1][1:] ,indent=4, separators=(',', ': '))) | |
maxLen = 0 | |
for r in chart: | |
for c in r: | |
for t in c: | |
#print(t) | |
if t is not None: | |
if len(t) > maxLen: | |
maxLen = len(t) | |
cellw = maxLen+2 | |
out = "" | |
i = 1 | |
for row in chart: | |
maxH = 1 | |
for c in row: | |
if maxH < len(c): | |
maxH = len(c) | |
out += ("+" + "-"*(maxLen+2))*i+"+\n" | |
for t in range(maxH): | |
for c in row: | |
try: | |
out += "|" + " " * (cellw - len(c[t]) - 1) + c[t] + " " | |
except: | |
out += "|" + " " * cellw | |
out += "|\n" | |
i += 1 | |
out += ("+" + "-"*(maxLen+2))*(i-1)+"+" | |
print(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment