import random ''' INSTRUCTIONS -Define your phonemes for each syllable inside the square brackets below. -Each phoneme has a weight assigned to it inside the round bracket, e.g ('s', 5). Change this number to change the probability of it being selected. -For instance, ('s', 5) means that 's' is 5 times more likely to appear than 't', which is ('t', 1). -Fractions can be used to lower the probability -- ('p', 1/3). ''' #1st Syllable onset = [('s', 5), ('p', 1/3), ('t', 1), ('m', 1), ('', 1)] vowel = [('a', 1), ('e', 1), ('o', 1)] final = [('s', 1), ('z', 1), ('sh', 1), ('', 1)] #2nd Syllable onset2 = [('p', 1), ('t', 1), ('', 1)] vowel2 = [('a', 1), ('e', 1), ('o', 1), ('u', 1), ('i', 1)] final2 = [('ng', 1), ('n', 1), ('m', 1), ('t', 1), ('', 1)] #This is a null syllable which will be called upon later when building the word. no_syllable = [''] #This part of the code handles the weighting of the phonemes and feeds into the code below. onset_weight = [val for val, cnt in onset for i in range(cnt)] vowel_weight = [val for val, cnt in vowel for i in range(cnt)] final_weight = [val for val, cnt in final for i in range(cnt)] onset_weight2 = [val for val, cnt in onset2 for i in range(cnt)] vowel_weight2 = [val for val, cnt in vowel2 for i in range(cnt)] final_weight2 = [val for val, cnt in final2 for i in range(cnt)] #This part of the code defines what the syllables are made of, and then defines what the word is made of. #Syllables 2 and 3 have the added feature of a "no_syllable", without which all words would be 3 syllables long. #Probablity of no_syllable can be increased by timesing it (*) by a number. for i in range(100): # <--- This loops everything indented below it. Change the number in range() to change the number of words generated. syllable1 = random.choice(onset_weight) + random.choice(vowel_weight) + random.choice(final_weight) syllable2 = random.choice(((no_syllable) * 1) + [random.choice(onset_weight2) + random.choice(vowel_weight2) + random.choice(final_weight2)]) syllable3 = random.choice(((no_syllable) * 3) + [random.choice(onset_weight2) + random.choice(vowel_weight2) + random.choice(final_weight2)]) word = syllable1 + syllable2 + syllable3 print word
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question