def operate(self): self.show_item_info() while True: op = raw_input("{}\'s Answer Item$ ".format(self.username)) if op == "voteup": self.vote_up() elif op == "votecancle": self.vote_cancle() elif op == "votedown": self.vote_down() elif op == "answer": answer_link = self.get_answer_link() from Answer import Answer answer = Answer(zhihu + answer_link) if answer.operate(): return True elif op == "question": from Question import Question question = Question(zhihu + self.get_question_link()) if question.operate(): return True elif op == "pwd": self.show_item_info() elif op == "quit": return True elif op == "help": self.help() elif op == "clear": clear() elif op == "break": break else: error()
def toQestion(self): type = "checkbox" if self.isradio: type = "radio" question = Question(qid=self.qid,text=self.rawstring,type=type,options=self.potentialanswers.keys()) question.descriptions = self.descriptions return question
def __init__(self, fileName): # First, check if an mdq file has been made self.mdqFileName = "." + fileName + ".mdq" if self.mdqFileName in os.listdir(): readFile = open(self.mdqFileName, 'rb') self.questions = pickle.load(readFile) printd("Successfully imported {} questions from {}".format( len(self.questions), self.mdqFileName)) else: self.questions = {} answer = "" questions = {} if fileName in os.listdir(): readFile = open(fileName) for line in readFile.readlines(): if line[0:2] == "# ": if answer != "" and given not in questions.keys(): questions[given] = Question(given=given, answer=answer.strip()) given = line[2:].strip() answer = "" elif not line.isspace(): answer += line.strip() + "\n" if answer != "" and given not in questions.keys(): questions[given] = Question(given=given, answer=answer) for k, v in questions.items(): if k in self.questions.keys(): questions[k].history = self.questions[k].history self.questions = questions printd("Successfully updated questions to {} from {}".format( len(self.questions), fileName)) else: printd("Error: File {} not found".format(fileName)) return
def operate(self): self.show_item_info() while True: op = raw_input("{}\'s Answer Item$ ".format(self.username)) if op == "voteup": self.vote_up() elif op =="votecancle": self.vote_cancle() elif op == "votedown": self.vote_down() elif op == "answer": answer_link = self.get_answer_link() from Answer import Answer answer = Answer(zhihu + answer_link) if answer.operate(): return True elif op == "question": from Question import Question question = Question(zhihu + self.get_question_link()) if question.operate(): return True elif op == "pwd": self.show_item_info() elif op == "quit": return True elif op == "help": self.help() elif op == "clear": clear() elif op == "break": break else: error()
def get_question(tag): mongoengine.connect('Words', host='mongodb://localhost/Words') count = len(Question.objects(tags__name__istartswith=tag.encode())) number = randint(1, count) question = Question.objects(tags__name__istartswith=tag.encode())[number:number + 1][0] option = Question.objects(id__ne=question.id)[:3] wrong = [] tag = [] for index in option: wrong.append(dict(word=index['answer'], answer=False)) wrong.append(dict(word=question.answer, answer=True)) for index in question.tags: tag.append(dict(id=index['id'], name=index['name'])) js = { 'word': question.word, 'tags': json.loads(JSONEncoder().encode(tag)), 'option': json.loads(JSONEncoder().encode(wrong)) } result = json.dumps(js) resp = Response(result, status=200, mimetype='application/json') return resp
def redirectSubmit(): """ Handles HTTP requests (GET and POST) with redirection after the request submission. :return: The rendered new page that will be displayed, with relevant arguments provided """ postRequest = request.json or request.form or request.args print postRequest rawText = str(postRequest.items()[0][1]) collist = key_words_filter(rawText) if len(collist) != 0: dna.db.fileter_cato(collist,0) if dna.currentquestion.qid == -1: print "error got" SESSION_INFO.result = dna.currentList q = Question() q.qid = "-1" SESSION_INFO.question = q SESSION_INFO.answerlist = dna.answerList return render_template('question.html', session_info=json.dumps(SESSION_INFO.toJson()))
def makeQuestions(self): quest = [] try: file = open('text.txt', 'r') except FileNotFoundException : print("file not found!") exit() q = file.readline() c = [] c.append(file.readline()) c.append(file.readline()) c.append(file.readline()) c.append(file.readline()) a = file.readline() quest.append(Question(q,c,a)) q = file.readline() c = [] c.append(file.readline()) c.append(file.readline()) c.append(file.readline()) c.append(file.readline()) a = file.readline() quest.append(Question(q,c,a)) q = file.readline() c = [] c.append(file.readline()) c.append(file.readline()) c.append(file.readline()) c.append(file.readline()) a = file.readline() quest.append(Question(q,c,a)) return quest
def get_question(tag): mongoengine.connect('Words', host='mongodb://localhost/Words') questionList = Question.objects(tags__name=tag.encode())[:10] number = randint(0, len(questionList)-1) question = questionList[number] option = Question.objects(id__ne=question.id)[:3] wrong = [] tag = [] for index in option: wrong.append(index['word'].encode('utf8')) for index in question.tags: tag.append(index['name'].encode('utf8')) js = { 'word': question.word, 'answer': question.answer, 'tags': json.dumps(tag), 'option': json.dumps(wrong) } result = json.dumps(js) resp = Response(result, status=200, mimetype='application/json') return resp
def blackboard_rank(): """ Given a board ID find the paths through the graph that match the query Then rank those paths. Return a list of paths with subgraphs and scores """ try: board_id = request.form.get('id') global collection_location condition = "id='{}'".format(board_id) rows = fetch_table_entries(collection_location, 'blackboards', condition) query = json.loads(rows[0][3]) # Query and Score will contact Neo4j # We just need to specify the query question = Question(query, board_id) ranking_data = question.answer() # ranking_data = queryAndScore({'query':query, 'board_id':board_id}) return jsonify({'ranking': ranking_data}) except Exception as ex: print(ex) raise InvalidUsage("Unspecified exception {0}".format(ex), 410) except: raise InvalidUsage('Failed to set run query.', 410)
def ajaxSubmit(): """ Handles HTTP requests (GET and POST) that are sent through ajax (i.e. without control of page redirection). :return: A serialized json object that contains the session information to be used in javascript """ postRequest = request.json or request.form # Short circuit the data fetch print postRequest print postRequest.getlist('answer') alist = eval("".join(postRequest.getlist('answer'))) if alist == []: return json.dumps({"session_info": SESSION_INFO.toJson()}) dna.answer(alist) dna.newQ() print dna.currentquestion if dna.currentquestion == -1 or dna.currentquestion == "error": print "error got" SESSION_INFO.result = dna.currentList q = Question() q.qid = "-1" SESSION_INFO.question = q return json.dumps({"session_info": SESSION_INFO.toJson()}) SESSION_INFO.question = dna.currentquestion.toQestion() return json.dumps({"session_info": SESSION_INFO.toJson()})
def setUp(self): self.q1 = Question() fakeM = '{"category":"Entertainment: Music","type":"multiple","difficulty":"medium","question":"Cryoshell, known for "Creeping in My Soul" did the advertising music for what Lego Theme?","correct_answer":"Bionicle","incorrect_answers":["Hero Factory","Ben 10 Alien Force","Star Wars"]}' self.q1.fromJson(fakeM) self.q2 = Question() fakeTF = '{"category":"General Knowledge","type":"boolean","difficulty":"easy","question":"The Lego Group was founded in 1932.","correct_answer":"True","incorrect_answers":["False"]}' self.q2.fromJson(fakeTF)
def run_for_question(question, number): q = Question(number, question, '../docs/top_docs.%d.gz'%number) query = BuildQuery.buildQuery(question) documents = q.search(query) rankings = q.golden_passage_retriever(documents) passage_metrics = q.test(rankings) return map(lambda x: float(sum(x)) / len(x), zip(*passage_metrics))
def edit_a_question(self, question_id, info): try: q = Question(info["type"], info["subject"], info["text"], info["answer"], info["inputs"], info["outputs"], info["value"], info["tags"]) return q.edit(self.db, question_id, self.org) except KeyError as k: return k.message
async def timer(self, ctx, question): await asyncio.sleep(30) if question is not None and question.get_awaiting_answer(): await ctx.send("Time's up!\nCorrect answer was: {}. {}".format( question.get_letter(), question.get_correct_answer())) self.question = Question() return True else: return False
def index(): if not Auth.auth(): redirect(SERVER_URL + "/login") #pull in questions q = Question() r = q.read() if r.status_code == 200: data = r.json() return template('member/questions', server_url=SERVER_URL, data=data)
def test_answers(self): with open(AnswerTests.TESTED_QUESTIONS_FILE_PATH) as test_data_file: tested_questions = json.load(test_data_file) incorrect = 0 incorrects = [] neg_count = [] zero_count = [] for question in tested_questions: # print 'reading tested questions json' new_question = Question(question) solved_answer = new_question.solve() if solved_answer != None: answer = Decimal(solved_answer) expected_answer = Decimal(question["lSolutions"][0]) print 'in tests' print "actual answer:", float(answer) print "expected answer:", float(expected_answer) if answer == 0.0: print 'in answer 0' answer = self.get_answer_from_quants(new_question) print 'new answer', answer print float(answer) != float(expected_answer) print Decimal(answer) != Decimal(expected_answer) print round(answer,2) == round(expected_answer,2) if float(answer) == float(expected_answer) or round(answer,2) == round(expected_answer,2): 'just' else: parent_index = str(question["ParentIndex"]) if answer == 0.0: zero_count.append(parent_index) elif answer < 0: neg_count.append(parent_index) print str(question["ParentIndex"]) incorrects.append(str(question["ParentIndex"])) incorrect = incorrect + 1 else: parent_index = str(question["ParentIndex"]) print str(question["ParentIndex"]) incorrects.append(str(question["ParentIndex"])) incorrect = incorrect + 1 # print "ParentIndex:" + str(question["ParentIndex"]) # print "ERROR:" + " " + new_question.m_question # print "ERROR:" + " Actual Answer " + str(answer) # print "ERROR:" + " Expected Answer " + str(expected_answer) # self.assertEquals(str(answer), str(expected_answer), new_question.m_question) # else: # print str(question["ParentIndex"]) print 'Incorrect: ' + str(incorrect) print incorrects print 'zero count',len(zero_count) print 'zero count:', zero_count print 'neg count',len(neg_count) print 'neg_count:', neg_count
def test_set_eventid(self): q1 = Question(0, "Question 1", ["user 1", "user 2"]) self.assertEqual(q1.get_eventid(), 0) q1.set_eventid(42) self.assertEqual(q1.get_eventid(), 42) q1.set_eventid(123) q1.set_eventid(999) self.assertEqual(q1.get_eventid(), 999)
def test1(): a = Question() a.set_qtext('aaaaaaa') a.append_qchoices('aaa', 'bbb') a.append_qchoices('ccc', 'ddd') print a.get_qtext() for i in range(a.len_qchoices()): print a.get_i_qchoices(i)
def insertQuestion(): response = make_response(redirect(url_for('index'))) question = Question() question.setQuestion(dict(request.form.items())) db = get_db() db.execute('insert into questions (\ primarySubject, secondarySubject, family,\ difficulty, question, correctIndex,\ response1, response2, response3, response4) values\ (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', question.asRow) db.commit() flash("New question written successfully") return response
def justDoIR(): questions = extract_questions(questions_file) output = open("../IRoutput.txt", 'w') for qNumber in questions: docs = DOCS + "top_docs." + str(qNumber) + ".gz" question = Question(qNumber,questions[qNumber],docs) # Can do baseline here or do full process. # Get expanded question: query = questions[qNumber] query = BuildQuery.buildFullQuery(query, hyponyms=True, hypernyms=True) ir_results = question.search(query) for (_,answer) in ir_results: output.write(answer+"\n") output.close()
def crawl_comment_data(self, people): print("question count: %d" % people.question_count) count = 0 for question in people.questions: Question.handle(question) count += 1 if count > self.max_count: break print("answer count: %d" % people.answer_count) count = 0 for answer in people.answers: Question.handle_answer(answer) count += 1 if count > self.max_count: break
def generateWordQuestion(self, list_of_words): answer = choice(list_of_words) word = list(answer) shuffle(word) result = " ".join(word) question = " " + result + " ? " return Question(question, answer)
def load_from_csv(): if os.path.isfile(raw_questions_file_name): print 'loading from pickle' data = cPickle.load(open(raw_questions_file_name, "rb")) # data = correct_text_encodings(data) # store(raw_questions_file_name, data) return data with open('sample_data\\30478_questions.csv', 'rb') as f: print 'reading csv' reader = csv.reader(f) next(reader, None) # header data = list(reader) questions = [Question.from_csv_arr(x) for x in data] questions = StackOverflowAdapter.get_body_from_so(questions) questions = prepare_data_set(questions) store(raw_questions_file_name, questions) return questions
def quiz_decider(self, mesutozil, poll, Ans_Key): attend = False islemsirasi = False # Bu isim degissin for x in mesutozil.matched_student_list: for index, row in poll.iterrows(): if (poll.iloc[index, 4] == 'Are you attending this lecture?'): islemsirasi = True if (poll.iloc[index, 1] == x.zoom_account.username): attend = True break else: attend = False else: if (poll.iloc[index, 1] == x.zoom_account.username): quiz = Poll_Quiz() for k in poll.iloc[index, lambda poll: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]]: # df.iloc[lambda x: x.index % 2 == 0] if (k != None): temp_question = Question(k) quiz.add_question(temp_question) for l in poll.iloc[index, lambda poll: [5, 7, 9, 11, 13, 15, 17, 19, 21, 23]]: if (l != None): temp_answer = Answer(l) quiz.add_student_answer(temp_answer) if (quiz.question_list[1].question_text == Ans_Key.q_list[1].question_text): quiz.give_name_to_poll(Ans_Key.poll_name) else: quiz.give_name_to_poll('Couldnt find') x.poll_adder(quiz) if (islemsirasi): attendance_poll = Poll_Attendance(x, attend)
def find_best_split(rows): best_gain = 0 # keep track of the best information gain best_question = None # keep train of the feature / value that produced it current_uncertainty = DecisionTreeClassifier.gini(rows) n_features = len(rows[0]) - 1 # number of columns for col in range(n_features): # for each feature values = set([row[col] for row in rows]) # unique values in the column for val in values: # for each value question = Question(col, val) # try splitting the dataset true_rows, false_rows = DecisionTreeClassifier.partition( rows, question) # Skip this split if it doesn't divide the dataset. if len(true_rows) == 0 or len(false_rows) == 0: continue # Calculate the information gain from this split gain = DecisionTreeClassifier.info_gain( true_rows, false_rows, current_uncertainty) if gain >= best_gain: best_gain, best_question = gain, question return best_gain, best_question
def find_best_split(rows, header): """ Find the best question to ask by iterating over every feature / value and calculating the information gain.""" best_gain = 0 # keep track of the best information gain best_question = None # keep train of the feature / value that produced it current_uncertainty = gini(rows) n_features = len(rows[0]) - 1 # number of columns for col in range(n_features): # for each feature values = unique_vals(rows, col) # unique values in the column for val in values: # for each value question = Question(col, val, header) # try splitting the dataset true_rows, false_rows = partition(rows, question) # Skip this split if it doesn't divide the # dataset. if len(true_rows) == 0 or len(false_rows) == 0: continue # Calculate the information gain from this split gain = info_gain(true_rows, false_rows, current_uncertainty) # You actually can use '>' instead of '>=' here # but I wanted the tree to look a certain way for our # toy dataset. if gain >= best_gain: best_gain, best_question = gain, question return best_gain, best_question
def ChangeQuestions(QuestionList): #Alice print("1 to add new question") QuestionOption = input("Answer: ") if QuestionOption == "1": d = {"A": "", "B": "", "C": "", "D": ""} q = input("Enter the question: ") A = input("Enter answer A: ") d.update({'A': A}) B = input("Enter answer B: ") d["B"] = B C = input("Enter answer C: ") d["C"] = C D = input("Enter answer D: ") d["D"] = D CorrectAnswer = input("Enter the correct answer (A,B,C,D)") ex = input("Enter an explination for your answer: ") t = input("Enter list of topics seperated by SPACE").split() #x = Question() QuestionList.append(Question(q, d, CorrectAnswer, ex, t)) with open(r"Questions.csv", 'a') as csvfile: writer = csv.writer(csvfile) writer.writerow( [q, d["A"], d["B"], d["C"], d["D"], CorrectAnswer, ex, t]) else: print("Invalid input")
def ReadQuestionFile(self, filename): """ReadQuestionFile(filename): Input: File Name. Output: None Side effects: Reads the file that holds the Questions for the game.""" try: file = open(filename, 'r') for lines in file: line = lines.strip('\n') quest = Question() quest.text = str(line) self.questions.insert(1, quest) file.close() except IOError: print 'File IO Error on file name ' + filename
def add_question(): def idcheck(): i = 0 while i == 0: id = randint(10000, 99999) with open("QuestionBank.txt") as g: if str(id) in g.read(): pass else: i = 1 return id f = open("QuestionBank.txt", "a") myid = str(idcheck()) subName = input("Enter Question Subject:") f.write("Subject: " + subName + " ") amount = int(input("How many paragraphs in the question?")) for i in range(amount): scndsubName = input("Enter paragraph subject #" + str(i + 1) + ":") f.write("Sub-Subject(" + str(i + 1) + "):" + scndsubName + ", Difficulty: # ") fformat = input("Enter the Fomat(PDF or Word):") QuestionFile = input( "Enter Question File:") # entering the file PDF or Word solFile = input("Enter Solution File:") # entering the file PDF or Word solType = input("Enter the Solution Type(Full,Partial,Final):") exam = input("Enter the Exam semester it was taken from:") f.write("Semester: " + exam + " ") f.write("Solution type: " + solType + "\n\r") f.write("ID: " + myid + "\n\r") newQuestion = Question(myid, subName, fformat, QuestionFile, solFile, solType, exam) f.close() return newQuestion
def add_question(self, user_question, answer, lie_1, lie_2, user_category): new_question = [] if user_question == "": self.status = "Don't be shy, what is your question?" new_question.append(user_question) if answer == "": self.status = "I NEED ANSWERS!!!!" new_question.append(answer) if lie_1 == "": self.status = "I NEED LIE!!!!" new_question.append(lie_1) if lie_2 == "": self.status = "I NEED ANOTHER LIE!!!!" new_question.append(lie_2) if user_category == "Category": self.status = "Don't be shy, what is your category?" new_question.append(user_category) question = Question(new_question[0], new_question[1], new_question[2], new_question[3], new_question[4]) self.game.questions.append(question) self.clear_form()
def item_to_question(self, item, renamed=False): """ Transforms a ccobra item to a question class. :param item: :param renamed: :return: """ # get the original question before the item is renamed. org_quest = [item.choices[1][0][1], item.choices[1][0][2]] if not renamed: self.rename_item(item) # a list of all premises for this question premises = [] for premise in item.task: parent = Node(premise[0]) lchild = Node(premise[1], None, None, parent) rchild = Node(premise[2], None, None, parent) parent.left = lchild parent.right = rchild parent.coding = parent.visit_make_coding() premises.append(parent) # a string of the question arrangement for experiment 2 quest = [item.choices[0][0][1], item.choices[0][0][2]] question = Question(premises, quest, 2, 0, org_quest) return question
def item_to_question(self, item, rename=False): """ Transforms a ccobra item to a question class. :param item: :return: """ if not rename: self.rename_item(item) # a list of all premises for this question premises = [] for premise in item.task: parent = Node(premise[0]) lchild = Node(premise[1], None, None, parent) rchild = Node(premise[2], None, None, parent) parent.left = lchild parent.right = rchild parent.coding = parent.visit_make_coding() premises.append(parent) # a string of the question arrangement for experiment 2 quest = "" if item.choices[0][0][0] == "lefts": quest = item.choices[0][0][1] quest += item.choices[0][0][2] if item.choices[0][0][0] == "rights": quest = item.choices[0][0][2] quest += item.choices[0][0][1] question = Question(premises, quest, 2, 0) return question
def get_question(question_id): OJDataBaseAdministrator.wait_mutex() result = DataBaseLinker.getInstance().execute( "select * from Question where id='" + str(question_id) + "'") question = Question(result[0]) OJDataBaseAdministrator.release_mutex() return question
def load_question_file(self, questions_file): """ Loads the selected question file. If the selected file is empty, returns. :param questions_file: The name of the questions file. """ questions_path = QUESTIONS_LOCATION + questions_file + '.json' if not os.path.isfile(questions_path): self.update_feedback(INVALID_FILE_STRUCTURE_FEEDBACK, FEEDBACK_ERROR_COLOR) return self.questions_file = questions_file # Save the name of the current question file. questions_dict = read_json(questions_path) self.questions = [Question(question) for question in questions_dict.values()] self.total_questions_number['text'] = '/' + str(len(self.questions)) self.questions_optionmenu.configure(state=ACTIVE if self.questions else DISABLED) self.current_question_index = 0 self.file_loaded = True self.load_question(init=True) # Loads the first question self.update_question_options() if self.questions: self.update_feedback(FILE_LOADED_SUCCESSFULLY_FEEDBACK, FEEDBACK_SUCCESS_COLOR) else: self.update_feedback(EMPTY_FILE_FEEDBACK, FEEDBACK_NOTIFY_COLOR)
def GetQuestions(self): filenames = self.GetQuestionsFilenames() questions = [] for filename in filenames: question = Question(filename) questions.append(question) return questions
def populate_polls(self, directory): count = 1 polls = [] for file_name in glob.iglob('{}/*.csv'.format(directory), recursive=True): df = pandas.read_csv(file_name) poll = Poll() for i in df.itertuples(): if len(i[0]) > 3: data = [np.asarray(i[0])] for j in range(1, len(i)): if str(i[j]) != "nan": data.append(i[j]) tup = [] for obj in data[0]: tup.append(obj) else: tup = [i[0][0], i[0][1]] for index in range(1, len(i)): tup.append(i[index]) tup = np.asarray([x for x in tup if str(x) != 'nan']) if poll.if_student_exists(tup[1]): polls.append(poll) poll = Poll() for q in range(4, len(tup), 2): question = Question("".join(tup[q].split()), "".join(tup[q + 1].split())) poll.insert_question(question) poll.insert_student(self.utils.strip_accents(tup[1])) polls.append(poll) count += 1 return polls
def save_new(self): """ Creates a new question from the editor. """ if not self.check_valid(): # If not valid, returns. self.update_feedback(SAVE_FORMAT_ERROR, FEEDBACK_ERROR_COLOR) return if self.questions is None: # If no file is yet loaded. self.update_feedback(FILE_NOT_LOADED_FEEDBACK, FEEDBACK_ERROR_COLOR) return # Updates the current question's index to the last question. self.current_question_index = len(self.questions) # Generates the question's text format, creates a new Question objects ans adds it to the # questions list question_format = self.get_edited_question_format() question = Question(question_format) self.questions.append(question) self.total_questions_number['text'] = '/' + str(len(self.questions)) self.regenerate_file() self.update_question_options() self.load_question(new=True) self.update_feedback(QUESTION_SAVED_SUCCESSFULLY_FEEDBACK, FEEDBACK_SUCCESS_COLOR)
def main(self): self.idf = {} self.paras = re.split('\s{4,}', self.text) N = len(self.paras) ni = {} tf = [] self.qv = processData(self).queryVector(self.question) for para in self.paras: pd = processData(self) temp = pd.findTfVector(para) tf.append(temp) for word in temp.keys(): if not word in ni: ni[word] = 0 ni[word] += 1 for word in ni: self.idf[word] = math.log((N + 1) / ni[word]) sim = [] for tfForDoc in tf: sim.append(processData(self).findSim(self.qv, tfForDoc, self.idf)) self.sim = sorted(enumerate(sim), key=operator.itemgetter(1), reverse=True) self.question_type = Question().classifyQues(self.question) self.answer = Answer().getAnswer(self) return self.answer
class FragGabiEntry: """ Scraper for the Frag Gabi Section on maedchen.de """ answer = str() question = Question() def __init__(self, url): """ On object creation directly initiate scraping of given site :param url: URL to a question on maedchen.de (Format like https://www.maedchen.de/love/frag-gabi/<something>) """ self.scrape_site(url) def scrape_site(self, url): """ Request site and extract contents :param url: URL to a question on maedchen.de (Format like https://www.maedchen.de/love/frag-gabi/<something>) :return: True on success """ r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') question_content_raw = soup.find_all( class_='question-detail')[0].get_text().strip() self.question.content = question_content_raw.split('\n\n\n')[0].strip() self.question.title = soup.find_all( class_='question-header')[0].get_text().strip() author_date = question_content_raw.split( '\n\n\n')[1].strip()[4:].split(' / ') self.question.author = author_date[0] self.question.set_date(author_date[1]) self.answer = soup.find_all( class_='question-answers__content--expert')[0].get_text( separator='\n') return True
def getAccuracy(self, filename): if filename != None: self.readFile(filename) testPara = self.getParagraph() testPara = " ".join(testPara) drm = Main() drm.tester(testPara) questionType = Question() result = [] for index in range(0, len(self.jsonData['paragraphs'])): paras = self.jsonData['paragraphs'][index] for quesNo in range(0, len(paras['qas'])): question = paras['qas'][quesNo]['question'] #print(question) qtype = questionType.classifyQues(question) #print(qtype) if (qtype not in ["PERSON", "LOCATION", "SUMMARY", "TIME"]): continue ansr = drm.testAnswer(paras['qas'][quesNo]['question']) answers = [] for ans in paras['qas'][quesNo]['answers']: answers.append(ans['text'].lower()) ansr = ansr.lower() isMatch = False for rt in word_tokenize(ansr): if [rt in word_tokenize(ans) for ans in answers].count(True) > 0: isMatch = True break # Open the following lines to print all questions and their answers # try: # if isMatch: # print(question,r,str(answers),isMatch) # else: # print("No Match") # except: # print("Error") result.append( (index, quesNo, question, ansr, str(answers), isMatch)) noOfResult = len(result) correct = [r[5] for r in result].count(True) if noOfResult == 0: accuracy = -1 else: accuracy = correct / noOfResult return accuracy
def getData(): questions = [] leetcode = "leetcode" base = "https://www.programcreek.com/2012/11/top-10-algorithms-for-coding-interview/" base_page = urllib2.urlopen(base).read() soup = BeautifulSoup(base_page, 'html.parser') breaker_words = ["solution", "approach", "analysis"] leetcode_source = "LEETCODE" question_links = [] for anchor in soup.findAll('a', href=True): link = anchor['href'] if leetcode in link: question_links.append(link) p_strong_elements = [] p_elements = soup.findAll('p') currCategory = "" linkToCategory = {} for element in p_elements: if element.find('strong') is not None: p_strong_elements.append(element.text) currCategory = element.text else: for ele in element.find_all('a'): if leetcode in ele['href'].encode('utf-8'): # print "CATEGORY" # print currCategory # print ele['href'].encode('utf-8') linkToCategory[ele['href'].encode('utf-8')] = currCategory for link in question_links[1:]: question_page = urllib2.urlopen(link).read() soup2 = BeautifulSoup(question_page, 'html.parser') div = soup2.find('div', {"class": "entrybody"}).findAll('p') header = soup2.find('h1', {"class": "entrytitle"}).findAll('a') # QUESTIONOBJ FORMATION description = "" for head in header: name = head.text.encode('utf-8') for a in div: text = a.encode('utf-8') if any(x in text.lower() for x in breaker_words): break else: description = description + a.text + " " print name description.encode('utf-8') link.encode('utf-8') category = linkToCategory[link] category = re.sub(r'\d+', '', category) category = category.replace(".", "") category = category.replace(" ", "") questions.append(Question(name, description, link, leetcode_source, category)) return questions
def add_question(): mongoengine.connect('Words', host='mongodb://localhost/Words') if (not request.data) or (not 'tags' in request.data) or (not 'word' in request.data) or ( not 'answer' in request.data): abort(400) tagList = [] data = json.loads(request.get_data()) content = data['word'].encode('utf8') answer = data['answer'].encode('utf8') for index in data['tags']: #todo:gets_or_create getTag = Tags.objects(id=index['id']) if 0 == len(getTag): addedTag = Tags(name=index['name'].encode('utf8'), date=datetime.datetime.now).save() tagList.append(Tag(id=addedTag.id, name=addedTag.name, date=addedTag.date)) else: tagList.append(Tag(id=getTag[0].id, name=getTag[0].name, date=getTag[0].date)) questionItem = Question() questionItem.word = content questionItem.answer = answer questionItem.tags = tagList questionItem.save() js = Question.objects().to_json() resp = Response(js, status=200, mimetype='application/json', charset='utf-8') return resp
def operate(self): if not self.parse(): return True self.get_full_answer() while True: op = raw_input("Answer$ ") if op == "voteup": self.vote_up() elif op == "votedown": self.vote_down() elif op == "votecancle": self.vote_cancle() elif op == "collect": self.add_to_collections() elif op == "author": url = self.get_author_link() if not url: print termcolor.colored("作者为匿名用户", "green") else: from User import User user = User(zhihu + url) if user.operate(): return True elif op == "question": from Question import Question question = Question(zhihu + self.get_question()) if question.operate(): return True elif op == "browser": self.open_in_browser() elif op == "pwd": self.get_full_answer() elif op == "clear": clear() elif op == "break": break elif op == "quit": return True elif op == "help": self.help() else: error()
def predict_class(self, questions): ids = [] texts = [] classes = [] for q in questions: ids.append(q.id) texts.append(q.text) classes.append(q.flag_id) x_features = self.vectorizer.transform(numpy.array(texts)) predictions = self.clf.predict(x_features.toarray()) return [Question.flag_from_id(p) for p in predictions]
def main(): questions = extract_questions(questions_file) output = open(output_file,"w") for qNumber in questions: print "\n" docs = DOCS + "top_docs." + str(qNumber) + ".gz" #print questions[qNumber] question = Question(qNumber,questions[qNumber],docs) # Can do baseline here or do full process. # Get expanded question: query = BuildQuery.buildFullQuery(questions[qNumber], hyponyms=False, hypernyms=False) print query ir_results = question.search(query) #print ir_results passages = question.golden_passage_retriever(ir_results) top5 = question.top5(passages) for answer in top5: print answer output.write(str(qNumber) + " top_docs." + str(qNumber) + " " + answer + "\n") output.close() if not IS_TEST: score = MRR() print score print "done"
def load_qcollection(self, path, nfile): """ Load a collection of questions from a json file :param path: :param nfile: :return: """ fp = open(path + '/' + nfile + '.json', 'r') lquestions = self._loads(fp.read()) for question in lquestions: q = Question() for sec in question: if sec == 'text': q.set_qtext(question[sec]) if sec == 'choices': for ch in question[sec]: o = {} for op in ch: o[op] = ch[op] q.append_qchoice(o) self.append_question(q)
def get_recent_questions(self, number_of_questions=100, filter='withbody'): questions = [] for api_question in self.so.questions(order=stackex.DESC, sort=stackex.Sort.Creation, pagesize=number_of_questions, page=1, filter=filter, tagged='javascript'): if not self.is_question_already_closed(api_question): try: question = Question.from_api(api_question) questions.append(question) except KeyError: # This flag don't interest to us pass if len(questions) >= number_of_questions: break return questions
def get_init(Id): mongoengine.connect('Words', host='mongodb://localhost/Words') tagItem = Tag(id="54803d32d70d2e1a984830cf", name='Developer', date=datetime.datetime.now) tagItem2 = Tag(id="54803e2ed70d2e1e10a58135", name='Developer', date=datetime.datetime.now) questionItem = Question() questionItem.word = 'content' questionItem.answer = 'reply' questionItem.tags.append(tagItem) questionItem.tags.append(tagItem2) questionItem.save() js = Question.objects().to_json() resp = Response(js, status=200, mimetype='application/json') return resp
def test_false_question(self): question = Question('4 + 4 ?','8') self.assertFalse(question.isCorrect('Kalle Anka')) self.assertFalse(question.isCorrect('9'))
from ParserMarkerFactory import ParserMarkerFactory from Totaller import Totaller from Handler import Handler from Question import Question from sympy import * import sympy x, y = sympy.symbols("x y") q1 = Question(1444130, "DF1", ("1:1:1"), ("2:2:1"), ("3:3:1")) q2 = Question(1444130, "DF1", "sin(x)") q3 = Question(1444130, "DF2", ("0:0:0"), ("1:1:0"), ("0:0:1"), ("1:1:0")) q4 = Question(1444130, "DF3", [("1:1:0"), ("0:0:1"), ("7:7:0"), ("-1:-1:0")], [("5:5:1"), ("2:2:0")]) q5 = Question(1444130, "DF3", [("sin(x)"), ("1:1:0")], [("2:2:0")]) q6 = Question(1444130, "DF4", [("2:2:0")], [("1:1:0"), ("1:1:0")]) q7 = Question(1444130, "DF5", ("5:5:0"), ("2:2:0"), ("-3:-3:1"), ("0:0:0")) q8 = Question(1444130, "IN1", ("1:1:0"), ("3:3:1"), ("2:2:1")) """Questions made up of type and of variables representing a/b/c/d/e. First variable represents the point for question 3, solving the gradient""" """Takes the question type and makes parser with answer passed to it""" """For the marker it passed it the question and parser""" p1, m1 = ParserMarkerFactory.create_parser_marker(q1.getQuestionType(), "2x+2", q1) p2, m2 = ParserMarkerFactory.create_parser_marker(q2.getQuestionType(), "cos(x)", q2) p3, m3 = ParserMarkerFactory.create_parser_marker(q3.getQuestionType(), "0", q3) p4, m4 = ParserMarkerFactory.create_parser_marker(q4.getQuestionType(), "20x^3+6x^2+70x+9", q4) p5, m5 = ParserMarkerFactory.create_parser_marker(q5.getQuestionType(), "2*(sin(x) + x*(cos(x)))", q5) p6, m6 = ParserMarkerFactory.create_parser_marker(q6.getQuestionType(), "-2/(x+1)^2", q6) p7, m7 = ParserMarkerFactory.create_parser_marker(q7.getQuestionType(), [[-0.6, 1/3],[1, -1]], q7) p8, m8 = ParserMarkerFactory.create_parser_marker(q8.getQuestionType(), "(x^3)/3+(3x^2)/2+2x", q8)
def test_question_true(self): question = Question('4 + 4 ?','8') self.assertTrue(question.isCorrect('8')) self.assertTrue(question.isCorrect('8 ')) self.assertTrue(question.isCorrect(question.answer));
def operate(self): d = self.get_item_info() ftype = self.get_feed_type() print "\n".join(i for i in d[1] if i) + "\n" while True: global flag op = raw_input("Time Line Item$ ") if op == "answer": if ftype.startswith("ANSWER"): #print d[2][1] from Answer import Answer answer = Answer(d[2][1]) #print answer.get_full_answer() if answer.operate(): flag = False return True else: error() elif op == "question": if ftype.startswith("ANSWER") or ftype.startswith("QUESTION"): #print d[2][0] from Question import Question question = Question(d[2][0]) if question.operate(): flag = False return True else: error() elif op == "author": if d[2][2]: #print d[2][2] from User import User user = User(d[2][2], self._xsrf) if user.operate(): flag = False return True else: error() elif op == "zhuanlan": if ftype.find("ARTICLE") != -1: url = self.get_zhuanlan_link() from Zhuanlan import Zhuanlan zhuanlan = Zhuanlan(url) if zhuanlan.operate(): flag = False return True else: error() elif op == "pwd": print "\n".join(i for i in d[1] if i) + "\n" elif op == "help": self.help() elif op == "break": break elif op == "clear": clear() elif op == "quit": flag = False return True else: error()