novel_starring_you~20171104-131946.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env/ python
  2. # This script rewrites the novel by replacing the name of the principal
  3. # character in the novel by another name.
  4. # It writes the new version of novel to a file called starring_me.txt and to a Logbook in Context
  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. import colors
  16. from colors import red, green, yellow, blue, magenta, cyan, bold, underline
  17. import time
  18. import os, sys
  19. ## FUNCTIONS
  20. # print on screen character per character
  21. def typewrite(sentence):
  22. words = sentence.split(" ")
  23. for word in words:
  24. for char in word:
  25. sys.stdout.write('%s' % char)
  26. sys.stdout.flush()
  27. time.sleep(0.1)
  28. sys.stdout.write(" ")
  29. sys.stdout.flush()
  30. # write to file
  31. def archive(sentence):
  32. with open("novel_starring_you.txt", "a") as destination:
  33. destination.write(sentence)
  34. # loop script
  35. while True:
  36. # introduction, getting the variables
  37. print("\n\t\tDear visitor, we will rewrite the opening scene of ", green("Kurt Vonnegut's 2BRO2B"), " using your name and favourite city.\n")
  38. #time.sleep(2)
  39. first_name = input("\t\tPlease type your first name: ")
  40. #time.sleep(2)
  41. last_name = input("\n\t\tPlease type your last name: ")
  42. #time.sleep(2)
  43. country = input("\n\t\tChoose a country: ")
  44. #time.sleep(2)
  45. city = input("\n\t\tChoose a city in that country: ")
  46. #time.sleep(2)
  47. print("\n\t\tDo you want to be", green('female'), "or", green('male?'))
  48. gender = input("\t\tPlease type f or m: ")
  49. #time.sleep(5)
  50. print("\n")
  51. # specify input text
  52. source = open("vonnegut.txt", "r")
  53. sentences =[]
  54. # write & replace
  55. archive("\n\nNovel Starring You\n")
  56. archive("-------------\n\n")
  57. with source as text:
  58. for line in text:
  59. line = line.replace("the United States", country)
  60. line = line.replace("Chicago", city)
  61. line = line.replace("Edward K.", first_name)
  62. line = line.replace("Wehling", last_name)
  63. if gender == 'f':
  64. line = line.replace(" man ", " woman ")
  65. line = line.replace(" man,", " woman,")
  66. line = line.replace(" his ", " her ")
  67. line = line.replace(" him ", " her ")
  68. line = line.replace(" His ", " Her ")
  69. line = line.replace(" wife ", " husband ")
  70. line = line.replace(" he ", " she ")
  71. line = line.replace(" He ", " She ")
  72. typewrite(line)
  73. archive(line)
  74. # break before relaunching the script
  75. time.sleep(10)