Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import random
  2. import json
  3. from pprint import pprint
  4. import re
  5.  
  6.  
  7. with open('grimm_tales.json') as f:
  8. all_stories = json.load(f)
  9.  
  10. storiesCount = len(all_stories['stories'])
  11. print(storiesCount)
  12.  
  13.  
  14. # first and last sentence of a story
  15. print(all_stories['stories'][8]['story'][0])
  16. print(all_stories['stories'][8]['story'][-1])
  17.  
  18. firstSent = []
  19. lastSent = []
  20. middleSent = []
  21.  
  22. for story in all_stories['stories']:
  23. firstSent.append(story['story'][0])
  24. lastSent.append(story['story'][-1])
  25. # find middlepoint in the story
  26. middleIndex = (len(story['story']) - 1)/2
  27. middleSent.append(story['story'][int(middleIndex)])
  28.  
  29. aList = [1,2,3,4,5]
  30. #minus 1 because the first element is index 0
  31. middleIndex = (len(aList) - 1)/2
  32. print (middleIndex)
  33. print (aList[int(middleIndex)])
  34.  
  35. # random number from 0 to array length
  36. RandomNum = random.randint(1,storiesCount)
  37.  
  38. span = 6
  39. firstPart = firstSent[RandomNum].split(' ')
  40. firstPartArray = [" ".join(firstPart[i:i+span]) for i in range(0, len(firstPart), span)]
  41.  
  42. secondPart = lastSent[RandomNum].split(' ')
  43. secondPartArray = [" ".join(secondPart[i:i+span]) for i in range(0, len(secondPart), span)]
  44.  
  45.  
  46.  
  47.  
  48. for line in firstPartArray:
  49. print(line)
  50.  
  51. print(' - ')
  52.  
  53. for line in secondPartArray:
  54. print(line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement