summaryrefslogtreecommitdiff
path: root/passage.py
blob: e88926e63c76362f56110ccbaf12ef440f991d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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()

'''