Skip to content

Instantly share code, notes, and snippets.

@sleebapaul
Created June 1, 2018 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sleebapaul/fa0a29a7acd6d6f85f2e4ee9d51d1156 to your computer and use it in GitHub Desktop.
Save sleebapaul/fa0a29a7acd6d6f85f2e4ee9d51d1156 to your computer and use it in GitHub Desktop.
The simplest character level language model using random picking of characters from a dictionary
import random
import string
from string import punctuation
# Get all the alphabets
alphabetsList = list(string.ascii_letters)
# Get all the punctuations
punctuationList = list(punctuation)
# Create a dictionary with all these characters and space
dictionary = alphabetsList + punctuationList + [" "]
def randomCharPicker(aList):
"""
Returns a randomly selected a character from aList
Probability of selection is 1 / length of dictionary
"""
return random.choice(aList)
if __name__ == "__main__":
tempSentence = ""
# Creating a sentence of 100 characters
for _ in range(100):
# Pick a random charater from dictionary
randomChar = randomCharPicker(dictionary)
# Append it to the output sentence
tempSentence += randomChar
# Display the sentence
print("Current sentence: {}".format(tempSentence))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment