Skip to content

Instantly share code, notes, and snippets.

@itayniv
Last active March 14, 2019 18:05
Show Gist options
  • Save itayniv/e7bd5e6626c4d31505a1949e062e3929 to your computer and use it in GitHub Desktop.
Save itayniv/e7bd5e6626c4d31505a1949e062e3929 to your computer and use it in GitHub Desktop.
import random
import json
from pprint import pprint
import re
with open('grimm_tales.json') as f:
all_stories = json.load(f)
storiesCount = len(all_stories['stories'])
print(storiesCount)
# first and last sentence of a story
print(all_stories['stories'][8]['story'][0])
print(all_stories['stories'][8]['story'][-1])
firstSent = []
lastSent = []
middleSent = []
for story in all_stories['stories']:
firstSent.append(story['story'][0])
lastSent.append(story['story'][-1])
# find middlepoint in the story
middleIndex = (len(story['story']) - 1)/2
middleSent.append(story['story'][int(middleIndex)])
aList = [1,2,3,4,5]
#minus 1 because the first element is index 0
middleIndex = (len(aList) - 1)/2
print (middleIndex)
print (aList[int(middleIndex)])
# random number from 0 to array length
RandomNum = random.randint(1,storiesCount)
span = 6
firstPart = firstSent[RandomNum].split(' ')
firstPartArray = [" ".join(firstPart[i:i+span]) for i in range(0, len(firstPart), span)]
secondPart = lastSent[RandomNum].split(' ')
secondPartArray = [" ".join(secondPart[i:i+span]) for i in range(0, len(secondPart), span)]
for line in firstPartArray:
print(line)
print(' - ')
for line in secondPartArray:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment