diff options
| author | Houyhnhnm <hykusb@bitmai.la> | 2018-06-03 14:06:16 +0200 |
|---|---|---|
| committer | Houyhnhnm <hykusb@bitmai.la> | 2018-06-03 14:06:16 +0200 |
| commit | 3786e330479d00517ecb894804180f59b0544e1a (patch) | |
| tree | ef503829490755f4da0ecd3ecb7e47441e37590e | |
| parent | 2fe6d52452b2cf48ceeffcbc0e8c2b30932f889d (diff) | |
bundle lexical ingredients in grammar object, separate spec file therefrom
| -rw-r--r-- | cadence.py | 8 | ||||
| -rw-r--r-- | grammar.py | 190 | ||||
| -rw-r--r-- | lexicon.py | 32 | ||||
| -rw-r--r-- | main.py | 4 | ||||
| -rw-r--r-- | phonology.py | 124 | ||||
| -rw-r--r-- | spec.py | 58 |
6 files changed, 222 insertions, 194 deletions
@@ -20,11 +20,10 @@ Cadence class Cadence(object): """a musical morpheme bundling rhythmic, gestural and harmonic features""" - def __init__(self, footprint=None, function=None, path=None, label=''): + def __init__(self, footprint=None, function=None, path=None): self.footprint = footprint self.function = function self.path = path - self.label = label # string represention of cadence - note list ? def __str__(self): @@ -44,9 +43,6 @@ class Cadence(object): } return features - def assignLabel(label): - self.label = label - def update(featureDict): if 'footprint' in featureDict: self.footprint = featureDict['footprint'] @@ -61,7 +57,7 @@ class Cadence(object): 'function': self.function, 'path': self.path, } - print("\nCadence", str(self.label)) + print("\nCadence") pprint(features) print() @@ -1,56 +1,136 @@ +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 + + + + +class Phonology(object): + """weighted repository of rhythmic, harmonic and gestural features""" + + def __init__(self, footprints=[], functions=[], paths=[]): + self.footprints = footprints + self.functions = functions + self.paths = paths + + def print(self): + features = { + 'footprints': self.footprints, + 'functions': self.functions, + 'paths': self.paths, + } + print("\nPhonology") + pprint(features) + print() + + def addFootprintRule(self, footprintRule, weight=1): + self.footprints.append((footprintRule, weight)) + + def addFunctionRule(self, functionRule, weight=1): + self.functions.append((functionRule, weight)) + + def addPathRule(self, pathRule, weight=1): + self.paths.append((pathRule, weight)) + + def makeCadence(self): # (duration=None, harmonicFilter=None) + """ assemble bundle of features from phonology rules """ + cadence = Cadence() + cadence.footprint = self.makeFootprint() + cadence.function = self.makeFunction() + cadence.path = self.makePath() + return cadence + + def makeFootprint(self): + # select footprint rule + weights = array([rule[1] for rule in self.functions]) + normalized_weights = weights.astype(float) / weights.sum() + footprintChoices = array(self.footprints) + index = choice(len(footprintChoices), p=normalized_weights) + footprintRule = footprintChoices[index][0] + + # select feature value from range where necessary + footprint = [] + for footRule in footprintRule: + foot = {} + # each foot option is composite + footChoices = array(footRule['foot']) + index = choice(len(footChoices)) + foot['foot'] = list(footChoices[index]) + # other feature options aren't + foot['span'] = choice(footRule['span']) + foot['gait'] = choice(footRule['gait']) + # timing options are per note: + foot['step'] = [] + for steps in footRule['step']: + foot['step'].append( choice(steps) ) + footprint.append(foot) + + return footprint + + + def makeFunction(self): + # select function rule + weights = array([rule[1] for rule in self.functions]) + normalized_weights = weights.astype(float) / weights.sum() + functionChoices = array(self.functions) + index = choice(len(functionChoices), p=normalized_weights) + functionRule = functionChoices[index][0] + + # select value from range for each feature + function = {} + function['address'] = choice(functionRule['address']) + function['transposition'] = choice(functionRule['transposition']) + + return function + + + def makePath(self): + + # select path rule + weights = array([rule[1] for rule in self.paths]) + normalized_weights = weights.astype(float) / weights.sum() + pathChoices = array(self.paths) + index = choice(len(pathChoices), p=normalized_weights) + pathRule = pathChoices[index][0] + + + # select value from range for each feature + path = {} + path['range'] = choice(pathRule['range']) + path['figure'] = choice(pathRule['figure']) + path['tilt'] = choice(pathRule['tilt']) + + return path + + +class Morphology(object): + + def __init__(self): + self.anticipationRules = [] + self.prolongationRules = [] + + def addAnticipationRule(self, projectedFeatures): + self.anticipationRules.append(projectedFeatures) + + def addProlongationRule(self, projectedFeatures): + self.prolongationRules.append(projectedFeatures) + + def print(self): + features = { + 'anticipations': self.anticipationRules, + 'prolongations': self.prolongationRules, + } + print("\nMorphology") + pprint(features) + print() + -footprintRules = [ - [{ - 'span': [2], - 'gait': ['double','triple'], - 'foot': [[-1/2, 0, 1/2]], - 'step': [[0, 1/2], [0, 1/2], [0, 1/2]], - }], -] - -functionRules = [ - { - 'address': ['A1','B1'], - 'transposition': [-1,0,1], - }, -] - -pathRules = [ - { - 'range': [2,3,4], - 'figure': ['rampUp', 'rampDown'], - 'tilt': ['float', 'dive'], - }, -] - -anticipationRules = [ - ['footprint'], - ['tilt'], - [], -] - -prolongationRules = [ - ['address','transposition'], - ['address'], - ['figure','range'], - ['range'], - ['range','tilt'], -] - -phonology = Phonology() -morphology = Morphology() - -for rule in footprintRules: - phonology.addFootprintRule(rule) - -for rule in functionRules: - phonology.addFunctionRule(rule) - -for rule in pathRules: - phonology.addPathRule(rule) - -for rule in anticipationRules: - morphology.addAnticipationRule(rule) - -for rule in prolongationRules: - morphology.addProlongationRule(rule) @@ -1,23 +1,41 @@ -from phonology import * +from grammar import * from numpy.random import choice, seed class Lexicon(object): - def makeLexeme(self): + + def __init__(self, genesis_size=0): + self.categories = [] + + def generate(self, grammar, number_categories=0): + for x in range(number): + cadence = + anticipation = + prolongation = + lexeme = Lexeme(cadence, anticipation, prolongation) + self + + 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, antecedent=None, consequent=None): + def __init__(self, cadence=None, anticipation=None, prolongation=None): if cadence=None: self.cadence = cadence # lexemes self.antecedents = [] self.consequents = [] # projections - self.anticipations = [] - self.prolongations = [] - - # self.label -- manage serial numbers somehow + self.anticipation = anticipation + self.prolongation = prolongation def addAntecedent(self, category): self.antecedents.append(category) @@ -1,6 +1,6 @@ -from phonology import * from diagram import * from grammar import * +from spec import * ''' modules: @@ -25,7 +25,7 @@ score.py list of n passages rendered to music21 score object phonology.print() morphology.print() -lexeme = phonology.makeCadence(label=1) +lexeme = phonology.makeCadence() lexeme.print() syntax = Diagram() diff --git a/phonology.py b/phonology.py deleted file mode 100644 index 55d1cc6..0000000 --- a/phonology.py +++ /dev/null @@ -1,124 +0,0 @@ -from cadence import * -from numpy import array -from numpy.random import choice, seed - -class Phonology(object): - """weighted repository of rhythmic, harmonic and gestural features""" - - def __init__(self, footprints=[], functions=[], paths=[]): - self.footprints = footprints - self.functions = functions - self.paths = paths - - def print(self): - features = { - 'footprints': self.footprints, - 'functions': self.functions, - 'paths': self.paths, - } - print("\nPhonology") - pprint(features) - print() - - def addFootprintRule(self, footprintRule, weight=1): - self.footprints.append((footprintRule, weight)) - - def addFunctionRule(self, functionRule, weight=1): - self.functions.append((functionRule, weight)) - - def addPathRule(self, pathRule, weight=1): - self.paths.append((pathRule, weight)) - - def makeCadence(self, label=''): # (duration=None, harmonicFilter=None) - """ assemble bundle of features from phonology rules """ - cadence = Cadence(label=label) - cadence.footprint = self.makeFootprint() - cadence.function = self.makeFunction() - cadence.path = self.makePath() - return cadence - - def makeFootprint(self): - # select footprint rule - weights = array([rule[1] for rule in self.functions]) - normalized_weights = weights.astype(float) / weights.sum() - footprintChoices = array(self.footprints) - index = choice(len(footprintChoices), p=normalized_weights) - footprintRule = footprintChoices[index][0] - - # select feature value from range where necessary - footprint = [] - for footRule in footprintRule: - foot = {} - # each foot option is composite - footChoices = array(footRule['foot']) - index = choice(len(footChoices)) - foot['foot'] = list(footChoices[index]) - # other feature options aren't - foot['span'] = choice(footRule['span']) - foot['gait'] = choice(footRule['gait']) - # timing options are per note: - foot['step'] = [] - for steps in footRule['step']: - foot['step'].append( choice(steps) ) - footprint.append(foot) - - return footprint - - - def makeFunction(self): - # select function rule - weights = array([rule[1] for rule in self.functions]) - normalized_weights = weights.astype(float) / weights.sum() - functionChoices = array(self.functions) - index = choice(len(functionChoices), p=normalized_weights) - functionRule = functionChoices[index][0] - - # select value from range for each feature - function = {} - function['address'] = choice(functionRule['address']) - function['transposition'] = choice(functionRule['transposition']) - - return function - - - def makePath(self): - - # select path rule - weights = array([rule[1] for rule in self.paths]) - normalized_weights = weights.astype(float) / weights.sum() - pathChoices = array(self.paths) - index = choice(len(pathChoices), p=normalized_weights) - pathRule = pathChoices[index][0] - - - # select value from range for each feature - path = {} - path['range'] = choice(pathRule['range']) - path['figure'] = choice(pathRule['figure']) - path['tilt'] = choice(pathRule['tilt']) - - return path - - -class Morphology(object): - - def __init__(self): - self.anticipationRules = [] - self.prolongationRules = [] - - def addAnticipationRule(self, projectedFeatures): - self.anticipationRules.append(projectedFeatures) - - def addProlongationRule(self, projectedFeatures): - self.prolongationRules.append(projectedFeatures) - - def print(self): - features = { - 'anticipations': self.anticipationRules, - 'prolongations': self.prolongationRules, - } - print("\nMorphology") - pprint(features) - print() - - @@ -0,0 +1,58 @@ +from grammar import * + +footprintRules = [ + [{ + 'span': [2], + 'gait': ['double','triple'], + 'foot': [[-1/2, 0, 1/2]], + 'step': [[0, 1/2], [0, 1/2], [0, 1/2]], + }], +] + +functionRules = [ + { + 'address': ['A1','B1'], + 'transposition': [-1,0,1], + }, +] + +pathRules = [ + { + 'range': [2,3,4], + 'figure': ['rampUp', 'rampDown'], + 'tilt': ['float', 'dive'], + }, +] + +anticipationRules = [ + ['footprint'], + ['tilt'], + [], +] + +prolongationRules = [ + ['address','transposition'], + ['address'], + ['figure','range'], + ['range'], + ['range','tilt'], +] + +# assemble grammar object +phonology = Phonology() +for rule in footprintRules: + phonology.addFootprintRule(rule) +for rule in functionRules: + phonology.addFunctionRule(rule) +for rule in pathRules: + phonology.addPathRule(rule) + +morphology = Morphology() +for rule in anticipationRules: + morphology.addAnticipationRule(rule) +for rule in prolongationRules: + morphology.addProlongationRule(rule) + +g = Grammar() +g.phonology = phonology +g.morphology = morphology |