Created
December 14, 2013 23:47
-
-
Save jesuscast/7966634 to your computer and use it in GitHub Desktop.
AP Psy homework doer. Use along a OCR like "http://www.onlineocr.net/"
Ex. do("your questions")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import wikipedia | |
def retrieveQuestions(dataStr): | |
dataStr = dataStr.lower() | |
questions = [] | |
another_question = False | |
w_s = ["what","why","when","how","have","can","could"] | |
w_c = "" | |
pos_i = 0 | |
pos_f = 0 | |
for w in w_s: | |
#print("\n ",w," \n") | |
another_question = w in dataStr | |
w_c = w | |
while another_question: | |
#loli=input("data") | |
#print(dataStr) | |
matchers = [] | |
match="" | |
if w_c == "what": | |
matchers = ["what is it ","what is the ","what would ","what are the","what are ","what does the","what does ","what"] | |
elif w_c == "why": | |
matchers = ["why is it ","why is the ","why would ","why are the","why are ","why does the","why does ","why"] | |
elif w_c == "when": | |
matchers = ["when is it ","when is the ","when would ","when are the","when are ","when does the","when does ","when"] | |
elif w_c == "how": | |
matchers = ["how is it ","how is the ","how would ","how are the","how are ","how does the","how does ","how"] | |
elif w_c =="have": | |
matchers = ["have"] | |
elif w_c == "can": | |
matchers = ["can"] | |
elif w_c == "could": | |
matchers = ["could"] | |
for match in matchers: | |
#print("match:",match) | |
if match in dataStr: | |
pos_i = dataStr.find(match) | |
break | |
#print(pos_i, "\n") | |
pos_f = dataStr.find("?") | |
#print("pos_f",pos_f) | |
reps = 0 | |
while pos_f < pos_i: | |
pos_f = dataStr.find("?", pos_f+1) | |
#print("pos_f-inside-while:",pos_f) | |
reps += 1 | |
if reps > 100: | |
break | |
if reps > 100: | |
break | |
else: | |
reps = 0 | |
#loli = input("matches") | |
#print("match:",match,"-data",dataStr) | |
questions.append(dataStr[pos_i+len(match):pos_f]) | |
whole_question = dataStr[pos_i:(pos_f+1)] | |
dataStr = dataStr.replace(whole_question, "") | |
another_question = False | |
another_question = w_c in dataStr | |
return questions | |
def homework(info): | |
textR= {} | |
hw = "" | |
textR['clean'] = info.lower() | |
textR['questions'] = retrieveQuestions(textR['clean']) | |
for question in textR['questions']: | |
try: | |
search = wikipedia.summary(question, sentences = 10) | |
except: | |
print("error") | |
hw+="*****"+question+"***** \n " + search + " \n \n " | |
# for question in textR['questions']: | |
# googleR = [] | |
# googleR['url'] = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0" | |
# googleR['payload'] = {"q":question} | |
# googleR['r'] = requests.get(googleR['url'],params=googleR['payload']) | |
# googleR['results'] = (json.loads(googleR['r'].text))["responseData"]["results"] | |
# link = "" | |
# for result in googleR['results']: | |
# if "wiki" in result['url']: | |
# link = result['url'] | |
# break | |
# if link != "": | |
return hw | |
def do(info): | |
r = homework(info) | |
f = open('results1.txt','w') | |
f.write(r) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment