Nathan Roth's Blog

August 5, 2009

Of Origins: the Fibonacci Syllabic Generator V-2.0

Filed under: Digital Writing In Python — admin @ 12:12 am

For my Digital Writing in Python final I decided to expand upon my Fibonacci Syllabic Generator program.  After running the program a few times I realized that I neglected some very simple things.  The output would often spit out garbage characters like ‘(‘ or ‘:’ or ‘;’ etc….  This made things look messy so I constructed a regular expression to catch some of the common characters that were messy:
###########################################################################
#   Remove random chars (: ; ‘ ” , )) #
###########################################################################
def text_cleaner(line):
x = line
p = re.findall(r”[,':)(;.\"]“,x)
for i in p:
x = x.replace(i,”)
return x

Once That was fixed I moved on to the task of making the program take dynamic input.  In its first iteration I used a line to write the sequence from 1-13 with a huge ugly string of concatenated strings.  In the new version I can define how far I want the sequence to continue its generation.  This was achieved as follows:

#################################################################################
# Line of code that Takes a Fibonacci Line  and Outputs the entire Syllable line#
#################################################################################
def line_generator(x):
p = fibonacci_generator(x)
q = syl_num(p)
return q

###########################################################################
# Fibonacci  Number Generator Function #
##########################################################################
def fibonacci_generator(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return fibonacci_generator(x-1) + fibonacci_generator(x-2)

#################################################################################
# Function that takes sequence line number and generates correct syllable lines #
#################################################################################

def syl_num(x):
if x == 1:
return random.choice(one_syllable)
else:
mod = x%2
t = x/2
one_syl_string = ‘ ‘
two_syl_string = syll_print(t)
spacer = ‘ ‘
if mod == 1:
one_syl_string = random.choice(one_syllable)
return str(one_syl_string) + str(spacer) + str(two_syl_string)

#################################################################################
# Function That constructs a string according to a number of syllables on input #
#################################################################################

def syll_print(x):
os_string = random.choice(two_syllable)
s_string = random.choice(two_syllable)
spacer = ‘ ‘
while (x > 1):
os_string = random.choice(two_syllable)
s_string = str(os_string) + str(spacer) + str(s_string)
x +=-1
return s_string

#################################################################################
#Final Sequence Generator Counting UP#
#################################################################################
def fb_sq_up(x):
lines = list()
while x > 0:
lines.append(str(line_generator(x)))
x +=-1
t = x
lines.reverse()
for i in lines:
i =i.strip()
i = i.center(100,’+')
print i

#################################################################################
#Final Sequence Generator Counting Down#
#################################################################################
def fb_sq_down(x):
lines = list()
while x > 0:
lines.append(str(line_generator(x)))
x +=-1
t = x
for i in lines:
i =i.strip()
i = i.center(100,’+')
print i

As you can see I centered the lines with ‘+’ s to make the text more visually interesting.  Runing the algorithm from up to 6 and down from 6 yields output that looks like this:

Genesis Creation Narrative:

example_1

example_2

Here is the Output from The Apache Creation Narrative:

example_3

Here is output from the Samoan Creation Narrative:

example_4

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress