def test_answer(self): self.logger.info("Test Student Answer Analysis") testStandardAnswerFile = "ans_Q1.txt" stdFilePath = os.path.join("algo/testdata/raw/Q1", testStandardAnswerFile) self.logger.info("stdanswer filepath:%s" % stdFilePath) if not os.path.isfile(stdFilePath): self.logger.error("Standard Test file doesn't exist:%s" % testStandardAnswerFile) assert False fh = file(stdFilePath, "r") stdtext = fh.read() fh.close() sinst = Standard() pointlist, textfdist, slist = sinst.Analysis(stdtext) std_pointlist_no = [point['Point_No'] for point in pointlist] self.logger.info("Points:%s" % std_pointlist_no) testAnswerFile = "ans_Q1.txt" ansFilePath = os.path.join("algo/testdata/raw/Q1", testAnswerFile) self.logger.info("answer filepath:%s" % ansFilePath) if not os.path.isfile(ansFilePath): self.logger.error("Answer file doesn't exist:%s" % testAnswerFile) assert False fh = file(ansFilePath, "r") anstext = fh.read() fh.close() mockrulelist = [{ 'Mark': 10, 'Point': ['P1.1', 'P1.2', 'P1.3', 'P2', 'P3', 'P4', 'P5'] }, { 'Mark': 7, 'Point': ['P1.1', 'P2', 'P3', 'P4', 'P5'] }, { 'Mark': 6, 'Point': ['P1.1', 'P2', 'P3', 'P4'] }, { 'Mark': 5, 'Point': ['P1.1', 'P2', 'P3'] }, { 'Mark': 3, 'Point': ['P1.1', 'P2'] }, { 'Mark': 2, 'Point': ['P1.1'] }] pprint.pprint(mockrulelist) ans = Answer() mark, marklist, ommited = ans.Analysis(anstext, textfdist, slist, pointlist, mockrulelist) pprint.pprint(mark) pprint.pprint(ommited) self.logger.info("Test Student Answer Analysis Finished")
def get_student_text_distribution(anstext, std_textfdist): debug_print( "Note: Deriving alternative global frequency distribution (from student text) for use with Answer.Analysis()" ) sinst = Standard() stu_pointlist, stu_textfdist, stu_slist = sinst.Analysis(anstext) debug_print("\tstandard dist: " + str(std_textfdist), level=4) debug_print("\tstudent dist: " + str(stu_textfdist), level=4) return stu_textfdist
def __traversal_process(self, testdir): ans = Answer() for root, dirs, files in os.walk(testdir): if 'Standard' in dirs: dirs.remove('Standard') for stdfile in files: # Check for answer file (e.g., "ans_q8.txt") if 'ans' in stdfile: testno = stdfile[4:-4] self.logger.info("no:%s" % testno) stdPath = os.path.join(root, stdfile) if not os.path.isfile(stdPath): self.logger.error("Test file doesn't exist:%s" % stdfile) assert False fh = file(stdPath, "r") filetext = fh.read() fh.close() sinst = Standard() pointlist, textfdist, slist = sinst.Analysis(filetext) # Check schema file (e.g., "scheme_q8.txt") schemename = 'scheme_' + testno + '.txt' schemepath = os.path.join(root, schemename) fr = file(schemepath, 'r') scheme = self.__parsescheme(fr.read()) fr.close() rulelist = self.__updaterulelist(scheme, pointlist) print("ansfile\tmark\tmarklist") for idx in range(0, 10): # Check student response file (e.g., "stud9_q8.txt") ansfile = 'stud' + str(idx + 1) + '_' + testno + '.txt' ansPath = os.path.join(root, ansfile) if os.path.isfile(ansPath): fa = file(ansPath, 'r') anstext = fa.read() fa.close() if anstext: # TODO: Always use freq dist for student text (not standard) if (USE_STUDENT_TEXT_DIST): textfdist = get_student_text_distribution( anstext, textfdist) debug_print("Calling ans.Analysis%s" % str( (anstext, textfdist, slist, pointlist, rulelist)), level=4) mark, marklist, ommited = ans.Analysis( anstext, textfdist, slist, pointlist, rulelist) else: mark = 0 marklist = [] print("%s\t%d\t%s" % (ansfile, mark, marklist))
def test_standard(self): self.logger.info("Test Standard Answer Analysis") testStandardAnswerFile = "ans_Q1.txt" filePath = os.path.join("algo/testdata/raw/Q1", testStandardAnswerFile) self.logger.info("filepath:%s" % filePath) if not os.path.isfile(filePath): self.logger.error("Test file doesn't exist:%s" % testStandardAnswerFile) assert False fh = file(filePath, "r") filetext = fh.read() fh.close() sinst = Standard() pointlist, textfdist, slist = sinst.Analysis(filetext) #for word,freq in textfdist.items(): # print "%s:%d" % (word,freq) pprint.pprint(slist) self.logger.info("Test Standard Answer Analysis finished")
def __traversal_process(self, testdir): ans = Answer() for root, dirs, files in os.walk(testdir): if 'Standard' in dirs: dirs.remove('Standard') for stdfile in files: if 'ans' in stdfile: testno = stdfile[4:-4] self.logger.info("no:%s" % testno) stdPath = os.path.join(root, stdfile) if not os.path.isfile(stdPath): self.logger.error("Test file doesn't exist:%s" % stdfile) assert False fh = file(stdPath, "r") filetext = fh.read() fh.close() sinst = Standard() pointlist, textfdist, slist = sinst.Analysis(filetext) schemename = 'scheme_' + testno + '.txt' schemepath = os.path.join(root, schemename) fr = file(schemepath, 'r') scheme = self.__parsescheme(fr.read()) fr.close() rulelist = self.__updaterulelist(scheme, pointlist) for idx in range(0, 10): ansfile = 'stud' + str(idx + 1) + '_' + testno + '.txt' ansPath = os.path.join(root, ansfile) if os.path.isfile(ansPath): fa = file(ansPath, 'r') anstext = fa.read() fa.close() if anstext: mark, marklist, ommited = ans.Analysis( anstext, textfdist, slist, pointlist, rulelist) else: mark = 0 marklist = [] print("%s\t%d\t%s" % (ansfile, mark, marklist))
def test_standard(self): self.logger.info("Test Standard Answer Analysis") testStandardAnswerFile = "ans_Q1.txt" filePath = os.path.join("algo/testdata/raw/Q1", testStandardAnswerFile) self.logger.info("filepath:%s" % filePath) if not os.path.isfile(filePath): self.logger.error("Test file doesn't exist:%s" % testStandardAnswerFile) assert False fh = file(filePath, "r") filetext = fh.read() fh.close() sinst = Standard() pointlist, textfdist, slist = sinst.Analysis(filetext) if __debug__: print "Word frequencies" for word, freq in textfdist.items(): print "%s:%d" % (word, freq) pprint.pprint(slist) if __debug__ and sinst.apply_grammar_checking: print("standard critique: %s" % sinst.critique_results) self.logger.info("Test Standard Answer Analysis finished")
def test_answer(self): self.logger.info("Test Student Answer Analysis") # Read in the correct answer to first question # TODO: Create helper function for reading question info as same code sequence used elsewhere. testStandardAnswerFile = "ans_Q1.txt" stdFilePath = os.path.join("algo/testdata/raw/Q1", testStandardAnswerFile) self.logger.info("stdanswer filepath:%s" % stdFilePath) if not os.path.isfile(stdFilePath): self.logger.error("Standard Test file doesn't exist:%s" % testStandardAnswerFile) assert False fh = file(stdFilePath, "r") stdtext = fh.read() fh.close() # Perform text processing analysis over correct answer sinst = Standard() pointlist, textfdist, slist = sinst.Analysis(stdtext) std_pointlist_no = [point['Point_No'] for point in pointlist] self.logger.info("Points:%s" % std_pointlist_no) # Read in the standard as if it were # TODO: Just do an assignment for crying out loud! Such needless code repetiton! # ex: anstext = stdtext testAnswerFile = "ans_Q1.txt" ansFilePath = os.path.join("algo/testdata/raw/Q1", testAnswerFile) self.logger.info("answer filepath:%s" % ansFilePath) if not os.path.isfile(ansFilePath): self.logger.error("Answer file doesn't exist:%s" % testAnswerFile) assert False fh = file(ansFilePath, "r") anstext = fh.read() fh.close() # Create some dummy grading rules mockrulelist = [{ 'Mark': 10, 'Point': ['P1.1', 'P1.2', 'P1.3', 'P2', 'P3', 'P4', 'P5'] }, { 'Mark': 7, 'Point': ['P1.1', 'P2', 'P3', 'P4', 'P5'] }, { 'Mark': 6, 'Point': ['P1.1', 'P2', 'P3', 'P4'] }, { 'Mark': 5, 'Point': ['P1.1', 'P2', 'P3'] }, { 'Mark': 3, 'Point': ['P1.1', 'P2'] }, { 'Mark': 2, 'Point': ['P1.1'] }] pprint.pprint(mockrulelist) # Create the answer class instance and optionally override global frequency distribution from answer text. # TODO: Always use freq dist for student text (not standard). ans = Answer() if (USE_STUDENT_TEXT_DIST): textfdist = get_student_text_distribution(anstext, textfdist) # Preprocess the student answer and then compare resulting vectors against standard # TODO: Raise an exception if the result is not as expected mark, marklist, ommited = ans.Analysis(anstext, textfdist, slist, pointlist, mockrulelist) pprint.pprint(mark) pprint.pprint(ommited) self.logger.info("Test Student Answer Analysis Finished")