ThatSnail

AwfulJokesBot

Feb 27th, 2013
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. import string
  2. import wikipedia
  3. import re
  4. import math
  5. import random
  6.  
  7. VOWELS = "AEIOUYaeiouy"
  8. CONSONANTS = "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz"
  9. MIN_PHENOMES_SHARED = 3
  10.  
  11. def getPhenomes(word):
  12. word = word.upper();
  13. result = "Word not found."
  14. refFile = open("phoneme_ref.txt");
  15. for line in refFile.read().splitlines():
  16. if line.find(word, 0, len(word)) > -1:
  17. result = line
  18. break
  19. refFile.close();
  20. # clean result
  21. result = result[len(word)+1:]
  22. return result
  23.  
  24. def getWordAssocs(word, n):
  25. nounfile = open("nouns")
  26. nounlist = [x for x in nounfile.read().splitlines()]
  27. nounfile.close()
  28.  
  29. wikitext = wikipedia.getArticle(word)
  30. wordlist = []
  31. if wikitext == False:
  32. return False
  33. for words in wikitext.split():
  34. words = words.lower()
  35. words = words.translate(string.maketrans("", ""), string.punctuation)
  36. if nounlist.count(words) > 0:
  37. if [x[1] for x in wordlist].count(words) == 0:
  38. wordlist.append((1, words))
  39. else:
  40. i = [x[1] for x in wordlist].index(words)
  41. t = wordlist[i] # tuple
  42. wordlist[i] = (t[0]+1, t[1])
  43. wordlist.sort()
  44. wordlist.reverse()
  45. return [x[1] for x in wordlist[:n]]
  46.  
  47. def getRandomNoun():
  48. nounfile = open("nouns")
  49. nounlist = [x for x in nounfile.read().splitlines()]
  50. nounfile.close()
  51.  
  52. return nounlist[int(math.floor(random.random() * len(nounlist)))]
  53.  
  54. def badjoke():
  55. ASSOC_COUNT = 5
  56.  
  57. word1 = getRandomNoun()
  58. word2 = getRandomNoun()
  59. print("What do you get when you cross a " + word1 + " with a " + word2 + "?")
  60.  
  61. wordassocslist1 = getWordAssocs(word1, ASSOC_COUNT)
  62. wordassocslist2 = getWordAssocs(word2, ASSOC_COUNT)
  63.  
  64. if wordassocslist1 == False or wordassocslist2 == False or len(wordassocslist1) == 0 or len(wordassocslist2) == 0:
  65. print("JOKE FAILED, ABORT, ABORT")
  66. return
  67.  
  68. print([getPhenomes(x) for x in wordassocslist1])
  69. print([getPhenomes(x) for x in wordassocslist2])
  70.  
  71. # wordassocs1 = wordassocslist1[int(math.floor(random.random() * len(wordassocslist1)))]
  72. # wordassocs2 = wordassocslist2[int(math.floor(random.random() * len(wordassocslist2)))]
  73.  
  74. # print("A " + wordassocs1 + wordassocs2)
  75.  
  76. def getNounPuns(list1, list2):
  77. punlist = []
  78. for word1 in list1: # word1 = "bowl"
  79. for word2 in list2: # word2 = "fishbowl"
  80. phenword1 = getPhenomes(word1) # phenword1 = ("B OW L")
  81. phenword2 = getPhenomes(word2) # phenword2 = ("F IH SH B OW L")
  82. lword1 = phenword1.split() # lword1 = ("B", "OW", "L")
  83. lword2 = phenword2.split() # lword2 = ("F", "IH", "SH", "B", "OW", "L")
  84.  
  85. # check prefixes word1, suffixes word2
  86. for phonemeCount in range(MIN_PHENOMES_SHARED, len(lword1) + 1): # phonemeCount = 2, 3
  87. newword = string.join(lword1[:phonemeCount]) # newword = "B OW"
  88. ind = string.find(phenword2, newword, 1)
  89. if ind != -1 and ind + len(newword) == len(phenword2):
  90. # pun found!
  91. punlist.append(word2 + pruneNoun(word1, newword))
  92. # check prefixes word2, suffixes word1
  93. for phonemeCount in range(MIN_PHENOMES_SHARED, len(lword2) + 1):
  94. newword = string.join(lword2[:phonemeCount])
  95. ind = string.find(phenword1, newword, 1)
  96. if ind != -1 and ind + len(newword) == len(phenword1):
  97. # pun found!
  98. punlist.append(word1 + pruneNoun(word2, newword))
  99. return punlist
  100.  
  101. def isConsonant(letter):
  102. if CONSONANTS.count(letter) > 0:
  103. return True
  104. else:
  105. return False
  106.  
  107. def isVowel(letter):
  108. if VOWELS.count(letter) > 0:
  109. return True
  110. else:
  111. return False
  112.  
  113. def getConsonantCount(word):
  114. consonantCount = 0
  115. for letter in list(word):
  116. if isConsonant(letter):
  117. consonantCount += 1
  118. return consonantCount
  119.  
  120. def pruneNoun(word, phonemes):
  121. phonemes.replace(" ", "")
  122.  
  123. consonants = getConsonantCount(phonemes)
  124.  
  125. consonantsCounted = 0
  126. cutindex = -1
  127. for letter in list(word):
  128. cutindex += 1
  129. if isConsonant(letter):
  130. consonantsCounted += 1
  131. if consonantsCounted >= consonants:
  132. break
  133. return word[cutindex:]
  134.  
  135. def conjunctify(word):
  136. if isVowel(word[0]):
  137. return "n " + word
  138. else:
  139. return " " + word
  140.  
  141. def main():
  142. result = ""
  143. while True:
  144. noun1 = getRandomNoun()
  145. noun2 = getRandomNoun()
  146. list1 = [noun1]
  147. list2 = [noun2]
  148. punlist = getNounPuns(list1, list2)
  149. if punlist != []:
  150. jokenoun1 = getWordAssocs(noun1, 1)
  151. jokenoun2 = getWordAssocs(noun2, 1)
  152. if jokenoun1 != [] and jokenoun2 != []:
  153. jokenoun1 = jokenoun1[0]
  154. jokenoun2 = jokenoun2[0]
  155. result += "What do you get when you cross a" + conjunctify(jokenoun1) + " with a" + conjunctify(jokenoun2) + "? "
  156. result += "A" + conjunctify(punlist[0]) + "!"
  157. break
  158. print(result)
  159. main()
Advertisement
Add Comment
Please, Sign In to add comment