Last Updated: February 25, 2016
·
451
· jesuscast

Python program that does my guided reading for AP Psychology class

this program is pretty simple. It tries to detect questions, then it separates them, looks them up in wikipedia and then collects the answers. Alright I am a high schooler and I don't have that much time.

Requirements (libraries)

Requests,
Wikipedia,
JSON

Usage

Call the function:

do("Your questions")

For example:

do("What is the spacing effect? How does retrospective thinking works? Who is the father of psychoanalysis?")

Code:

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 "
    return hw
def do(info):
    r = homework(info)
    f = open('results1.txt','w')
    f.write(r)
    f.close()