summaryrefslogtreecommitdiff
path: root/passage.py
diff options
context:
space:
mode:
authorHouyhnhnm <hykusb@bitmai.la>2018-06-03 18:47:29 +0200
committerHouyhnhnm <hykusb@bitmai.la>2018-06-03 18:47:29 +0200
commit01071c8123e6b30eeff88272bf4fc7325d62dd5a (patch)
tree9598b4cc53546df62804e672e3c14215edf8ee94 /passage.py
parent3786e330479d00517ecb894804180f59b0544e1a (diff)
added syntax tree generation, feature unification
Diffstat (limited to 'passage.py')
-rw-r--r--passage.py95
1 files changed, 95 insertions, 0 deletions
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()
+
+'''