summaryrefslogtreecommitdiff
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
parent3786e330479d00517ecb894804180f59b0544e1a (diff)
added syntax tree generation, feature unification
-rw-r--r--cadence.py2
-rw-r--r--diagram.py23
-rw-r--r--grammar.py16
-rw-r--r--lexicon.py77
-rw-r--r--main.py30
-rw-r--r--passage.py95
6 files changed, 178 insertions, 65 deletions
diff --git a/cadence.py b/cadence.py
index c277112..76c8ac0 100644
--- a/cadence.py
+++ b/cadence.py
@@ -57,9 +57,7 @@ class Cadence(object):
'function': self.function,
'path': self.path,
}
- print("\nCadence")
pprint(features)
- print()
'''
# example
diff --git a/diagram.py b/diagram.py
deleted file mode 100644
index 6401469..0000000
--- a/diagram.py
+++ /dev/null
@@ -1,23 +0,0 @@
-from binarytree import *
-
-class Diagram(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:
- # raise NodeNotFoundError('no node to delete at index {}')
- print('syntax diagram impossible for height'+height+'and size'+size)
-
- def print(self):
- self.tree.pprint(index=True, delimiter='\'')
-
-'''
-# example
-dee = Diagram()
-dee.print()
-''' \ No newline at end of file
diff --git a/grammar.py b/grammar.py
index 2f8e385..44e110b 100644
--- a/grammar.py
+++ b/grammar.py
@@ -2,16 +2,12 @@ from cadence import *
from numpy import array
from numpy.random import choice, seed
-
-
class Grammar(object):
def __init__(self, phonology=None, morphology=None, syntax=None):
- self.phonology
- self.morphology
- self.syntax
-
-
+ self.phonology = phonology
+ self.morphology = morphology
+ self.syntax = syntax
class Phonology(object):
@@ -124,6 +120,12 @@ class Morphology(object):
def addProlongationRule(self, projectedFeatures):
self.prolongationRules.append(projectedFeatures)
+ def selectAnticipation(self):
+ return choice(self.anticipationRules)
+
+ def selectProlongation(self):
+ return choice(self.prolongationRules)
+
def print(self):
features = {
'anticipations': self.anticipationRules,
diff --git a/lexicon.py b/lexicon.py
index 38bb40b..c7eef6a 100644
--- a/lexicon.py
+++ b/lexicon.py
@@ -3,40 +3,71 @@ from numpy.random import choice, seed
class Lexicon(object):
- def __init__(self, genesis_size=0):
+ def __init__(self):
self.categories = []
- def generate(self, grammar, number_categories=0):
- for x in range(number):
- cadence =
- anticipation =
- prolongation =
- lexeme = Lexeme(cadence, anticipation, prolongation)
- self
+ def populate(self, grammar, number_categories=0):
+ for x in range(number_categories):
+ lexeme = self.makeLexeme(grammar)
+ lexeme.label = self.nextLabel()
+ self.categories.append(lexeme)
+
+ def addLexeme(self, grammar, lexeme=None):
+ if lexeme == None:
+ lexeme = self.makeLexeme(grammar)
+ lexeme.label = self.nextLabel()
+ self.categories.append(lexeme)
+
+ def makeLexeme(self, grammar):
+ cadence = grammar.phonology.makeCadence()
+ anticipation = grammar.morphology.selectAnticipation()
+ prolongation = grammar.morphology.selectProlongation()
+ lexeme = Lexeme(cadence, anticipation, prolongation)
+ return lexeme
+
+ def selectLexeme(self, index=None):
+ if index == None:
+ lexeme = choice(self.categories)
+ else:
+ lexeme = self.categories[index]
+ return lexeme
+
+ def nextLabel(self):
+ nextLabel = 0
+ if len(self.categories) > 0:
+ lexeme = self.categories[-1].label + 1
+ return nextLabel
+
+ def print(self):
+ for i, lex in enumerate(self.categories):
+ print('Lexeme', str(i))
+ print('--------')
+ lex.print()
+ print()
- def addLexeme():
-
- def makeCategory():
-
-
- def makeLexeme(cadence, anticipation=None, prolongation=None):
-
-
- def makeProjection(self):
- pass
class Lexeme(object):
"""the specification of an dependant and its agreement"""
- def __init__(self, cadence=None, anticipation=None, prolongation=None):
- if cadence=None:
+ def __init__(self, cadence, anticipation=None, prolongation=None):
self.cadence = cadence
- # lexemes
- self.antecedents = []
- self.consequents = []
+
# projections
self.anticipation = anticipation
self.prolongation = prolongation
+ # dependents
+ self.antecedents = []
+ self.consequents = []
+
+ self.label = 'unlabeled'
+
+ def print(self):
+ self.cadence.print()
+ print("anticipation:", self.anticipation)
+ print("prolongation:", self.prolongation)
+ print("antecedents:", self.antecedents)
+ print("consequents:", self.consequents)
+
def addAntecedent(self, category):
self.antecedents.append(category)
return self
diff --git a/main.py b/main.py
index 976e214..7cf66a2 100644
--- a/main.py
+++ b/main.py
@@ -1,5 +1,6 @@
-from diagram import *
from grammar import *
+from lexicon import *
+from passage import *
from spec import *
'''
@@ -10,24 +11,33 @@ projection.py dependencies and agreement
antecedent or consequent cadence bundled with prolongation rules for each feature - (dominates) or (dominates, agrees) or (dominates, agrees, submits)
(1)==Head; (0)==Complement; (1,0)==XP; (1,1)==XBar; (0,1)==X; (0,0)==None;
-diagram.py define binary tree structures
phonology.py distribution of cadential features for given grammar
lexicon.py repository of lexemes: (cadence, dependencies, prolongation)
-passage.py lexemes unified over tree diagram
+passage.py lexemes unified over binary tree
spellout() sequence of unified cadences in passage (for each voice)
score.py list of n passages rendered to music21 score object
- export() to xml, midi, ly
+ export() to xml, midi, ly'
'''
# lexicon.addCategory()
-phonology.print()
-morphology.print()
+g.phonology.print()
+g.morphology.print()
+
+lexicon = Lexicon()
+lexicon.populate(g, 4)
+lexicon.print()
+
+passage = Passage(size=12)
+passage.printSyntax()
+
+realization = passage.spellout(lexicon)
+
+pprint(realization)
+
+for lex in realization:
+ lex.print()
-lexeme = phonology.makeCadence()
-lexeme.print()
-syntax = Diagram()
-syntax.print()
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()
+
+'''