Skip to content

Instantly share code, notes, and snippets.

@askmeegs
Created August 25, 2015 18:42
Show Gist options
  • Save askmeegs/522d793af300a6886b37 to your computer and use it in GitHub Desktop.
Save askmeegs/522d793af300a6886b37 to your computer and use it in GitHub Desktop.
storymaker demo
#storymaker.py
#by megan o'keefe
#created: July 31st, 2015
#last modified: 7/31/2015
#has WordsApi definitions, also working on: wikipedia articles.
import json
import unirest
import wikipedia
def main():
filename = str(raw_input("Welcome to Storymaker 5000. Please enter your output file name: ")+".txt")
print("\n\n(DEF [word] = dictionary lookup. QUIT = write out.)\n\n")
story = "\n" #to break up each snip
current = "" #user's current response
while current != "QUIT":
print("\n")
current = raw_input("")
if current[:3] == "DEF":
print getDefinitions(current)
if current[:4] == "WIKI":
print getWiki(current)
else: #they're actually writing
if current != "QUIT":
story = story + " " + current
#once user says QUIT, write the batch to text file.
with open(filename, 'a') as outfile:
outfile.write(story)
def getDefinitions(word):
word = current[4:]
response = unirest.get("https://wordsapiv1.p.mashape.com/words/" + word + "/definitions",
headers={
"X-Mashape-Key": "SSQyVRXCfqmshYWpCnDBGPBrFtNmp15Sk05jsnthREsSW5a8t9",
"Accept": "application/json"
}
)
#extract definitions
defs = ""
for item, index in response.body['definitions']:
defs = defs + item['definition'] + "; "
return "(" + defs[:2] + ")\n" #nice and packaged
def getWiki(searchterm):
searchterm = searchterm[5:]
return wikipedia.summary(searchterm)
#UP NEXT - get synonyms.
if __name__=="__main__":
main()
@vNh123
Copy link

vNh123 commented Nov 5, 2017

#by megan o'keefe
#created: July 31st, 2015
#last modified: 7/31/2015
#has WordsApi definitions, also working on: wikipedia articles.

import json
import unirest
import wikipedia

def main():
filename = str(raw_input("Welcome to Storymaker 5000. Please enter your output file name: ")+".txt")
print("\n\n(DEF [word] = dictionary lookup. QUIT = write out.)\n\n")

story = "\n" #to break up each snip
current = "" #user's current response

while current != "QUIT":
    print("\n")
    current = raw_input("")
    if current[:3] == "DEF":
        print getDefinitions(current)
    if current[:4] == "WIKI":
        print getWiki(current)
    else: #they're actually writing
        if current != "QUIT":
            story = story + " " + current
   
#once user says QUIT, write the batch to text file.
with open(filename, 'a') as outfile:
    outfile.write(story)

def getDefinitions(word):
word = current[4:]
response = unirest.get("https://wordsapiv1.p.mashape.com/words/" + word + "/definitions",
headers={
"X-Mashape-Key": "SSQyVRXCfqmshYWpCnDBGPBrFtNmp15Sk05jsnthREsSW5a8t9",
"Accept": "application/json"
}
)
#extract definitions
defs = ""
for item, index in response.body['definitions']:
defs = defs + item['definition'] + "; "
return "(" + defs[:2] + ")\n" #nice and packaged

def getWiki(searchterm):
searchterm = searchterm[5:]
return wikipedia.summary(searchterm)

#UP NEXT - get synonyms.

if name=="main":
main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment