Commit e483ac74 authored by gijs's avatar gijs

Started on visualisation

parent f1213153
import turtle
import random
import re
# color('grzeen', 'yellow')
# begin_fill()
#t = turtle.Pen()
empty = '???'
name_pattern = re.compile(r'(.+)\s\((.+)\)')
branch = '├──'
branch_close = '└──'
indent = '│ '
tree = None
path = 'Cocos+Q13099531+Kazakh.txt'
def parse_line (line):
level = 0
offset = 0
end = False
while line.startswith(indent, offset):
level += 1
offset += len(indent)
if line.startswith(branch_close, offset):
level += 1
offset += len(branch_close)
end = True
elif line.startswith(branch, offset):
level += 1
offset += len(branch)
m = name_pattern.match(line[offset:])
if m:
name = m.group(1)
code = m.group(2)
if name.startswith(empty):
name = None
return (name, code, level, end)
else:
print('Unrecognized line `{}`'.format(line[offset:]))
def parse_lines (lines, level = 0, tree=[]):
while lines:
line = lines.pop(0)
name, code, line_level, is_end = parse_line(line)
leave = (name, code, [])
if line_level > level:
if is_end:
tree[-1][2].append(leave)
else:
tree[-1][2].extend(parse_lines(lines, line_level, [leave]))
else:
tree.append(leave)
if is_end:
return tree
return tree
with open(path, 'r') as source:
lines = list(source.readlines())
tree = parse_lines(lines)
print(tree)
# for line in source.readlines():
# line_name, line_level, line_is_end = parse_line(line)
# if
# else:
# print('Unrecognized line `{}`'.format(line[offset:]))
def draw_tree (length, tree, gap=8):
leaf_count = len(tree)
leaf_length = (length - (gap * (leaf_count - 1))) / leaf_count
for leaf in tree:
name, code, subtree = leaf
if subtree:
turtle.left(90)
turtle.forward(20)
turtle.right(90)
draw_tree(leaf_length, subtree, gap / 2)
turtle.right(90)
turtle.forward(20)
turtle.left(90)
if name:
turtle.pendown()
turtle.forward(leaf_length)
turtle.penup()
turtle.forward(gap)
turtle.left(180)
turtle.forward(length + gap)
turtle.right(180)
length = 400
turtle.speed(0)
draw_tree(length, tree)
turtle.getscreen().getcanvas().postscript(file='lines.ps')
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment