.
[HamExam.git] / statistic.py
blob0d66d86ab447f426abd4d17296a4b7bc3627ad38
1 #!/usr/bin/python
2 import sys, string
3 from xml.dom import minidom, Node
4 # should really be done with http://effbot.org/zone/element.htm
5 import os
6 import datetime
8 from xml.dom.ext import PrettyPrint
9 from StringIO import StringIO
11 def toprettyxml_fixed (node): #, encoding='utf-8'):
12 tmpStream = StringIO()
13 PrettyPrint(node, stream=tmpStream)# , encoding=encoding)
14 return tmpStream.getvalue()
16 class Statistic:
17 def IncreaseCounter(self, qid, how, answer, time):
18 """
19 Return True, is question is good
20 """
21 nq = self.FindQuestion (qid)
22 if nq >= 0:
23 for q in self.root.getElementsByTagName("question"):
24 id = q.getAttribute ("id")
25 if id == qid:
26 break
28 c = int(q.getAttribute ("c"))
29 w = int(q.getAttribute ("w"))
30 cs = int(q.getAttribute ("cs"))
31 ws = int(q.getAttribute ("ws"))
33 else:
34 print "NEU",qid
35 q = self.stat.createElement(u'question')
36 q.setAttribute("id",qid)
38 sss = self.root.getElementsByTagName("learning")[0]
39 sss.appendChild(q)
41 c=0
42 w=0
43 cs=0
44 ws=0
46 self.statistics.append ([qid, c, cs, ws, w, ""])
47 nq = self.FindQuestion (qid)
49 if how == True:
50 c += 1
51 cs += 1
52 ws = max(ws-1,0)
53 else:
54 w += 1
55 ws += 1
56 cs = min(cs-1,0)
58 if c < 0:
59 c = 0
60 if cs < 0:
61 cs = 0
62 if w < 0:
63 w = 0
64 if ws < 0:
65 ws = 0
67 # in-memory statistics
68 self.statistics[nq][1] = c
69 self.statistics[nq][2] = cs
70 self.statistics[nq][3] = ws
71 self.statistics[nq][4] = w
73 # xml file stuff
74 q.setAttribute("c",str(c))
75 q.setAttribute("w",str(w))
76 q.setAttribute("cs",str(cs))
77 q.setAttribute("ws",str(ws))
80 t = self.Timestamp()
81 a = [1,2,4,8][(["a","b","c","d"]).index(answer)]
82 nt = int(time*1000)
84 qq = self.stat.createElement("answer_clicked")
85 qq.setAttribute ("datetime", str(t))
86 qq.setAttribute ("answer_code", str(a))
87 qq.setAttribute ("needed_time", str(nt))
88 q.appendChild(qq)
90 if float(ws) > 0.:
91 rr = 0.
92 else:
93 rr = float(cs)*self.norm
95 return rr >= self.ratio
97 def WriteFile(self):
98 print "Writing statistics file",self.filename
99 ss=toprettyxml_fixed(self.root)
100 f=open(self.filename,"w")
101 print >>f,"<!DOCTYPE AFUTrainerStatistics>"
102 f.write(ss)
103 f.close()
105 def OpenFile(self):
106 print "Opening statistics file",self.filename
107 self.stat = minidom.parse (self.filename)
108 self.root = self.stat.documentElement
110 self.date = self.root.getAttribute ("date")
111 self.version = self.root.getAttribute ("version")
112 self.name = self.root.getAttribute ("name")
115 def GetStatistics(self):
116 print "Parsing statistics xml"
117 self.statistics = []
119 self.ratio = .75
120 self.mincorrect = 5.
121 self.newquestion = []
122 self.goodquestion = []
123 self.badquestion = []
124 self.norm = 1./self.mincorrect
126 for q in self.root.getElementsByTagName("question"):
127 id = q.getAttribute ("id")
128 c = q.getAttribute ("c")
129 cs = q.getAttribute ("cs")
130 ws = q.getAttribute ("ws")
131 w = q.getAttribute ("w")
133 if float(ws) > 0.:
134 rr = 0.
135 else:
136 rr = float(cs)*self.norm
138 if rr >= self.ratio:
139 self.goodquestion.append (id)
140 elif rr >= 0:
141 self.badquestion.append (id)
142 else: # never reached
143 self.newquestion.append (id)
145 answers = []
146 for a in q.childNodes:
147 if a.nodeType == Node.ELEMENT_NODE:
148 code = a.getAttribute("answer_code")
149 time = a.getAttribute("needed_time")
150 when = a.getAttribute("datetime")
151 answers.append ([code,time,when])
153 self.statistics.append ([id, c, cs, ws, w, answers])
155 def GetPriority(self,qid):
156 return
158 def MakeStatistics(self):
159 ntotal = len(self.goodquestion) + len(self.newquestion) + len(self.badquestion) + len(self.goodquestion)
160 rcorrect = float(len(self.goodquestion))/ntotal
161 rnew = float(len(self.newquestion))/ntotal
162 return [rnew, rcorrect]
164 def FindQuestion(self,qid):
165 i = 0
166 for s in self.statistics:
167 if s[0] == qid:
168 return i
169 i += 1
170 return -1
172 def ThisQuestion(self,qid):
173 q = self.FindQuestion (qid)
174 if q >= 0:
175 c = self.statistics[q][1]
176 w = self.statistics[q][4]
177 cs = self.statistics[q][2]
178 ws = self.statistics[q][3]
179 else:
180 c=str(0)
181 w=str(0)
182 cs=str(0)
183 ws=str(0)
184 return [str(c),str(w),str(cs),str(ws)]
186 def Timestamp(self):
187 return str(datetime.datetime.today().isoformat()).split(".")[0]
189 def __init__(self,filename="DL-A-2007.stat.xml"):
190 self.filename=filename
192 self.OpenFile()
193 self.GetStatistics()