From 01071c8123e6b30eeff88272bf4fc7325d62dd5a Mon Sep 17 00:00:00 2001 From: Houyhnhnm Date: Sun, 3 Jun 2018 18:47:29 +0200 Subject: added syntax tree generation, feature unification --- passage.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 passage.py (limited to 'passage.py') diff --git a/passage.py b/passage.py new file mode 100644 index 0000000..e88926e --- /dev/null +++ b/passage.py @@ -0,0 +1,95 @@ +from binarytree import * +from copy import copy, deepcopy + +class Passage(object): + """represents a syntactic structure (wrapper for binary tree) + """ + def __init__(self, height=3, size=None): + self.tree = tree(height=height) + if isinstance(size, int): + if size>=height and size<((2**(height+1))-1): + while self.tree.size != size: + self.tree = tree(height=height) + else: + print('Syntax tree of height '+str(height)+' must have between '+str(height+1)+' and '+str((2**(height+1))-1)+' nodes, but you asked for '+str(size)+' nodes.') + # relabel nodes in level order + for i, node in enumerate(self.tree.levelorder): + node.value = i + node.unify = {} + + def printSyntax(self): + print(self.tree) + + def spellout(self, lexicon): + + # prolonged features trickle down to leaves + for node in self.tree.levelorder: + node.lexeme = lexicon.selectLexeme() + + if node.left: + node.left.unify = copy(node.unify) + for feat in node.lexeme.anticipation: + # if feat == 'footprint': <- take off first condition for 'agree' prolongation + if feat not in node.left.unify.keys() and feat == 'footprint': + node.left.unify['footprint'] = node.lexeme.cadence.footprint + if feat not in node.left.unify.keys() and feat in node.lexeme.cadence.function: + node.left.unify[feat] = node.lexeme.cadence.function[feat] + if feat not in node.left.unify.keys() and feat in node.lexeme.cadence.path: + node.left.unify[feat] = node.lexeme.cadence.path[feat] + + if node.right: + node.right.unify = copy(node.unify) + for feat in node.lexeme.prolongation: + if feat not in node.right.unify.keys() and feat == 'footprint': + node.right.unify['footprint'] = node.lexeme.cadence.footprint + if feat not in node.right.unify.keys() and feat in node.lexeme.cadence.function: + node.right.unify[feat] = node.lexeme.cadence.function[feat] + if feat not in node.right.unify.keys() and feat in node.lexeme.cadence.path: + node.right.unify[feat] = node.lexeme.cadence.path[feat] + + spellout = [] + # now read out nodes into list, with prolonged features replacing lexical features + for node in self.tree.levelorder: + next_lexeme = deepcopy(node.lexeme) + for feat in node.unify.keys(): + if feat == 'footprint': + next_lexeme.cadence.footprint = node.unify[feat] + if feat in ['address', 'transposition']: + next_lexeme.cadence.function[feat] = node.unify[feat] + if feat in ['range','figure','tilt']: + next_lexeme.cadence.path[feat] = node.unify[feat] + spellout.append(next_lexeme) + return spellout + + +''' + +>>> print(root) + + __1 + / \ + 2 3 + / \ +4 5 + +>>> root.inorder - linear order of lexemes +[Node(4), Node(2), Node(5), Node(1), Node(3)] + +>>> root.levelorder +[Node(1), Node(2), Node(3), Node(4), Node(5)] + +>>> root.preorder +[Node(1), Node(2), Node(4), Node(5), Node(3)] + +>>> root.postorder +[Node(4), Node(5), Node(2), Node(3), Node(1)] + + + +{scaffold, configuration, shell, backbone, spine, projection} + +# example +dee = Passage() +dee.print() + +''' -- cgit v1.2.3