Skip to content

Instantly share code, notes, and snippets.

@uttamg911
Created March 29, 2014 16:08
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 uttamg911/9857258 to your computer and use it in GitHub Desktop.
Save uttamg911/9857258 to your computer and use it in GitHub Desktop.
# Imports Natual Language Tool Kit
import nltk
# Imports CMU Pronunciation Dictionary from NLTK Corpus
from nltk.corpus import cmudict
# Imports Random
import random
full_dict = cmudict.entries()[:]
l = 0
w = 0
wrd = ""
s = ""
while l < 3: # 3 lines
while w < 3: # 3 letters for each word
r = full_dict[random.randint(0, len(full_dict)-1)] # randomly picks a word and its pronunciation as a list
q = r[1] # gets the word's pronunciation
wrd = wrd + " " + r[0]
for p in q:
if ( len(p) !=1 and q.index(p) != 0 and q.index(p) != len(q)-1 ): # checks for boundary conditions
s = s + " " + q[q.index(p)-1] + "-" + p + "-" + q[q.index(p)+1] # constructs the line
w += 1
print wrd + ":" + s
w = 0
s = ""
wrd = ""
l += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment