python2_oulipo~20171108-210406.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env/ python
  2. # This script makes you choose 1 out of 2 Oulipo constraints:
  3. # Constraint 1: http://oulipo.net/fr/contraintes/litterature-definitionnelle
  4. # Constraint 2: rewrites the beginning of a novel by replacing the principal names/places/gender
  5. # The idea for this script comes from the book 'Think Python'.
  6. # Copyright (C) 2016 Constant, Algolit, An Mertens
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details: <http://www.gnu.org/licenses/>.
  15. from __future__ import division
  16. import nltk
  17. from nltk.corpus import wordnet as wn
  18. from pattern.en import tag
  19. import nltk.data
  20. from random import shuffle, choice, randrange
  21. import colors
  22. from colors import red, green, yellow, blue, magenta, cyan, bold, underline
  23. import time
  24. import os, sys
  25. ## FUNCTIONS
  26. # print on screen character per character
  27. def typewrite(sentence):
  28. words = sentence.split(" ")
  29. for word in words:
  30. for char in word:
  31. if char != "<" and char != ">":
  32. sys.stdout.write('%s' % char)
  33. sys.stdout.flush()
  34. time.sleep(0.1)
  35. sys.stdout.write(" ")
  36. sys.stdout.flush()
  37. # loop script
  38. while True:
  39. print "\n\t\tDear visitor, you can choose between", red(" two Oulipo applications.\n")
  40. #source = open("frankenstein_for_machines.txt", 'r')
  41. print "\t\tOption a is ", green("Litterature Definitionnelle"), " with sentences from Mary Shelley's Frankenstein.\n"
  42. #source = open("frankenstein_for_machines.txt", 'r')
  43. print "\t\tOption b is ", green("A Novel Starring You.\n")
  44. #source = open("frankenstein_for_machines.txt", 'r')
  45. print "\t\tType ", green('a'), " if you want to play with Litterature Definitionnelle.\n"
  46. #source = open("frankenstein_for_machines.txt", 'r')
  47. print "\t\tType ", green('b'), " if you want to be a star in the opening scene of Kurt Vonneguts' 2BRO2B.\n"
  48. #source = open("frankenstein_for_machines.txt", 'r')
  49. choice = raw_input("\t\tYour choice is: ")
  50. #source = open("frankenstein_for_machines.txt", 'r')
  51. print "\n"
  52. os.system('cls' if os.name == 'nt' else 'clear')
  53. print "\n"
  54. ### MEET ---------------------------------------------------------------------------------------------
  55. ### --------------------------------------------------------------------------------------------------
  56. ### MEET/INTRO ---------------------------------------------------------------------------------------------
  57. # retrain model
  58. if choice == 'a':
  59. ### Litterature definitionnelle
  60. # textfiles
  61. source = open("frankenstein_for_machines.txt", 'r')
  62. #source = open("1984_fragment.txt", 'r')
  63. destination = open("litterature_definitionelle.txt", "wt")
  64. ## SCRIPT
  65. # select 4 sentences from source
  66. ## split source text into list of sentences
  67. finding_sentences = nltk.data.load('tokenizers/punkt/english.pickle')
  68. sentences_list = []
  69. with source as text:
  70. for line in text:
  71. # this returns a list with 1 element containing the entire text, sentences separated by \n
  72. sentences = '\n'.join(finding_sentences.tokenize(line.strip()))
  73. # transform string into list of sentences
  74. sentences_list = sentences.split("\n")
  75. selected_sentences = [sentences_list[randrange(len(sentences_list))]
  76. for s in range(4)]
  77. # tokenize source and get Part-of-Speech tags for each word
  78. definitions = []
  79. for sentence in selected_sentences:
  80. # create tuple of tuples with pairs of word + POS-tag
  81. collection = tag(sentence, tokenize=True, encoding='utf-8')
  82. # transform tuple into list to be able to manipulate it
  83. collection = list(collection)
  84. # for each pair:
  85. for element in collection:
  86. # look for nouns & replace them with their definition
  87. if element[1] == "NN":
  88. if wn.synsets(element[0]):
  89. synset = wn.synsets(element[0])
  90. definitions.append("<")
  91. definitions.append(synset[0].definition())
  92. definitions.append(">")
  93. else:
  94. break
  95. else:
  96. # non-nouns are left as words
  97. definitions.append(element[0])
  98. # write the transformed sentence
  99. #print " ".join(definitions)
  100. for d in definitions:
  101. typewrite(d)
  102. raw_input("\nPress Enter to continue...")
  103. # time.sleep(10)
  104. # -------------------------------------------
  105. elif choice == 'b':
  106. ### A Novel Starring You
  107. # introduction, getting the variables
  108. print "\n\t\tDear visitor, we will rewrite the opening scene of ", green("Kurt Vonnegut's 2BRO2B"), " using your name and favourite city.\n"
  109. ##source = open("frankenstein_for_machines.txt", 'r')
  110. first_name = raw_input("\t\tPlease type your first name: ")
  111. ##source = open("frankenstein_for_machines.txt", 'r')
  112. last_name = raw_input("\n\t\tPlease type your last name: ")
  113. ##source = open("frankenstein_for_machines.txt", 'r')
  114. country = raw_input("\n\t\tChoose a country: ")
  115. ##source = open("frankenstein_for_machines.txt", 'r')
  116. city = raw_input("\n\t\tChoose a city in that country: ")
  117. ##source = open("frankenstein_for_machines.txt", 'r')
  118. print "\n\t\tDo you want to be", green('female'), "or", green('male?')
  119. gender = raw_input("\t\tPlease type f or m: ")
  120. #time.sleep(5)
  121. print "\n"
  122. # specify input text
  123. source = open("vonnegut.txt", "r")
  124. sentences =[]
  125. # write & replace
  126. archive("\n\nNovel Starring You\n")
  127. archive("-------------\n\n")
  128. with source as text:
  129. for line in text:
  130. line = line.replace("the United States", country)
  131. line = line.replace("Chicago", city)
  132. line = line.replace("Edward K.", first_name)
  133. line = line.replace("Wehling", last_name)
  134. if gender == 'f':
  135. line = line.replace(" man ", " woman ")
  136. line = line.replace(" man,", " woman,")
  137. line = line.replace(" his ", " her ")
  138. line = line.replace(" him ", " her ")
  139. line = line.replace(" His ", " Her ")
  140. line = line.replace(" wife ", " husband ")
  141. line = line.replace(" he ", " she ")
  142. line = line.replace(" He ", " She ")
  143. typewrite(line)
  144. archive(line)
  145. # break before relaunching the script
  146. raw_input("\nPress Enter to continue...")
  147. time.sleep(10)
  148. ### ELSE --------------------------------------------------------------------------------------
  149. ### -------------------------------------------------------------------------------------------
  150. # try again
  151. else:
  152. print "\t\tYou must have typed something else."
  153. #time.sleep(30)
  154. raw_input("\nPress Enter to continue...")
  155. os.system('cls' if os.name == 'nt' else 'clear')