Last active
February 18, 2023 20:23
-
-
Save 0x07dc/a35e18fbf883631b69baf5f6f838087a to your computer and use it in GitHub Desktop.
Python Script to Generate Pseudo-language Paragraphs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################ | |
# Author: Jamil Voss (2023) | |
# MIT License (free to use open-source license) | |
# Language: Python | |
# About: | |
# Python script to generate random pseudo-language paragraph. | |
# Uses arrays of vowel and consonant phonemes and | |
# a normally distributed random index generator | |
# to concatenate phonemes. | |
# | |
# Try it out here: https://www.mycompiler.io/view/K4dwPObRtH4 | |
import random | |
from scipy.stats import truncnorm | |
import unicodedata | |
phonemesCons = ["b", | |
"d", | |
"f", | |
"g", | |
"h", | |
"j", | |
"k", | |
"l", | |
"m", | |
"n", | |
"p", | |
"r", | |
"s", | |
"t", | |
"v", | |
"w", | |
"y", | |
"z"] | |
phonemesCons.extend(["ch", | |
"sh", | |
"ng", | |
"th", | |
"th", | |
"zh", | |
"wh"]) | |
phonemesVow = ["ar", | |
"ār", | |
"ir", | |
"or", | |
"ur"] | |
phonemesVow.extend(["ā", | |
"ē", | |
"ī", | |
"ō", | |
"ū"]) | |
phonemesVow.extend(["a", | |
"e", | |
"i", | |
"o", | |
"u"]) | |
phonemesVow.extend(["oo", | |
"ōō"]) | |
phonemesVow.extend(["ow", | |
"oy"]) | |
phonemesCons.extend(["bl", | |
"cl", | |
"fl", | |
"gl", | |
"pl", | |
"br", | |
"cr", | |
"dr", | |
"fr", | |
"gr", | |
"pr", | |
"tr", | |
"sk", | |
"sl", | |
"sp", | |
"st", | |
"sw", | |
"spr", | |
"str"]) | |
#print(phonemesCons) | |
#print(phonemesVow) | |
# https://stackoverflow.com/a/518232 | |
def removeAccents(s): | |
return ''.join(c for c in unicodedata.normalize('NFD', s) | |
if unicodedata.category(c) != 'Mn') | |
# https://stackoverflow.com/a/74448424 | |
def rand0To1NormalDist(mean=0, sd=1, low=0, upp=10): | |
result = -1 | |
while result < 0 or\ | |
result > 1: | |
result = truncnorm( | |
(low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd).rvs() | |
return result | |
def randomInt(lowVal, highVal): | |
return int(rand0To1NormalDist() * (highVal-lowVal) + lowVal) | |
def randomFloat(): | |
return rand0To1NormalDist() | |
def randWord(): | |
numPhonemes = randomInt(2,4) | |
newWord = "" | |
for i in range(numPhonemes): | |
randIdxCons = randomInt(0, len(phonemesCons) - 1) | |
#print(randIdxCons) | |
randIdxVow = randomInt(0, len(phonemesVow) - 1) | |
#print(randIdxVow) | |
newWord += phonemesCons[randIdxCons] | |
newWord += phonemesVow[randIdxVow] | |
if(randomFloat() < 0.2): | |
randIdxVow = randomInt(0, len(phonemesVow) - 1) | |
newWord += phonemesVow[randIdxVow] | |
if(randomFloat() < 0.2): | |
randIdxCons = randomInt(0, len(phonemesCons) - 1) | |
newWord += phonemesCons[randIdxCons] | |
return removeAccents(newWord) | |
#print(randWord()) | |
def randSentence(): | |
newSentence = "" | |
numWords = randomInt(4, 11) | |
for i in range(numWords): | |
newSentence += randWord() | |
if(i != numWords - 1): | |
newSentence += " " | |
else: | |
newSentence += "." | |
return newSentence.capitalize() | |
#print(randSentence()) | |
def randParagraph(): | |
newParagraph = "" | |
numSentences = randomInt(7, 22) | |
for i in range(numSentences): | |
newParagraph += randSentence() | |
if(i != numSentences - 1): | |
newParagraph += " " | |
return newParagraph | |
print(randParagraph()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment