Commit 7956f219 authored by gijs's avatar gijs

Starting to add comments.

parent e483ac74
......@@ -7,32 +7,35 @@ import re
#t = turtle.Pen()
empty = '???'
token_empty = '???'
name_pattern = re.compile(r'(.+)\s\((.+)\)')
branch = '├──'
branch_close = '└──'
indent = '│ '
token_branch = '├──' # Token to indicate a branch at the current level
token_branch_close = '└──' # Token to indicate the end of a branch
token_indent = '│ ' # Token to indicate this line holds a branch at a deeper level
tree = None
path = 'Cocos+Q13099531+Kazakh.txt'
path = 'Cocos+Q13099531+Russian.txt'
def parse_line (line):
level = 0
offset = 0
end = False
while line.startswith(indent, offset):
# Detect at which level in the tree this
# line is placed
while line.startswith(token_indent, offset):
level += 1
offset += len(indent)
offset += len(token_indent)
if line.startswith(branch_close, offset):
#
if line.startswith(token_branch_close, offset):
level += 1
offset += len(branch_close)
offset += len(token_branch_close)
end = True
elif line.startswith(branch, offset):
elif line.startswith(token_branch, offset):
level += 1
offset += len(branch)
offset += len(token_branch)
m = name_pattern.match(line[offset:])
......@@ -40,7 +43,7 @@ def parse_line (line):
name = m.group(1)
code = m.group(2)
if name.startswith(empty):
if name.startswith(token_empty):
name = None
return (name, code, level, end)
......@@ -48,20 +51,22 @@ def parse_line (line):
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, [])
leaf = (name, code, [])
if line_level > level:
if is_end:
tree[-1][2].append(leave)
tree[-1][2].append(leaf)
else:
tree[-1][2].extend(parse_lines(lines, line_level, [leave]))
subtree = parse_lines(lines, line_level, [leaf])
tree[-1][2].extend(subtree)
else:
tree.append(leave)
tree.append(leaf)
if is_end:
return tree
......@@ -114,6 +119,7 @@ def draw_tree (length, tree, gap=8):
length = 400
turtle.speed(0)
turtle.penup()
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