.
[HamExam.git] / statistic.py
blob5fdfc0e426de3aa28695b50159e5270812ba8627
1 #!/usr/bin/python
2 import sys, string
3 from xml.dom import minidom, Node
4 import os
5 import datetime
7 from xml.dom.ext import PrettyPrint
8 from StringIO import StringIO
10 def toprettyxml_fixed (node): #, encoding='utf-8'):
11 tmpStream = StringIO()
12 PrettyPrint(node, stream=tmpStream)# , encoding=encoding)
13 return tmpStream.getvalue()
15 class Statistic:
16 def IncreaseCounter(self, qid, how, answer, time):
17 """
18 Return True, is question is good
19 """
20 nq = self.FindQuestion (qid)
21 if nq >= 0:
22 for q in self.root.getElementsByTagName("question"):
23 id = q.getAttribute ("id")
24 if id == qid:
25 break
27 c = int(q.getAttribute ("c"))
28 w = int(q.getAttribute ("w"))
29 cs = int(q.getAttribute ("cs"))
30 ws = int(q.getAttribute ("ws"))
32 else:
33 print "NEU",qid
34 q = self.stat.createElement(u'question')
35 q.setAttribute("id",qid)
37 sss = self.root.getElementsByTagName("learning")[0]
38 sss.appendChild(q)
40 c=0
41 w=0
42 cs=0
43 ws=0
45 self.statistics.append ([qid, c, cs, ws, w, ""])
46 nq = self.FindQuestion (qid)
48 if how == True:
49 c += 1
50 cs += 1
51 ws = max(ws-1,0)
52 else:
53 w += 1
54 ws += 1
55 cs = min(cs-1,0)
57 if c < 0:
58 c = 0
59 if cs < 0:
60 cs = 0
61 if w < 0:
62 w = 0
63 if ws < 0:
64 ws = 0
66 # in-memory statistics
67 self.statistics[nq][1] = c
68 self.statistics[nq][2] = cs
69 self.statistics[nq][3] = ws
70 self.statistics[nq][4] = w
72 # xml file stuff
73 q.setAttribute("c",str(c))
74 q.setAttribute("w",str(w))
75 q.setAttribute("cs",str(cs))
76 q.setAttribute("ws",str(ws))
79 t = self.Timestamp()
80 a = [1,2,4,8][(["a","b","c","d"]).index(answer)]
81 nt = int(time*1000)
83 qq = self.stat.createElement("answer_clicked")
84 qq.setAttribute ("datetime", str(t))
85 qq.setAttribute ("answer_code", str(a))
86 qq.setAttribute ("needed_time", str(nt))
87 q.appendChild(qq)
89 if float(ws) > 0.:
90 rr = 0.
91 else:
92 rr = float(cs)*self.norm
94 return rr >= self.ratio
96 def WriteFile(self):
97 print "Writing statistics file",self.filename
98 ss=toprettyxml_fixed(self.root)
99 f=open(self.filename,"w")
100 print >>f,"<!DOCTYPE AFUTrainerStatistics>"
101 f.write(ss)
102 f.close()
104 def OpenFile(self):
105 print "Opening statistics file",self.filename
106 self.stat = minidom.parse (self.filename)
107 self.root = self.stat.documentElement
109 self.date = self.root.getAttribute ("date")
110 self.version = self.root.getAttribute ("version")
111 self.name = self.root.getAttribute ("name")
114 def GetStatistics(self):
115 print "Parsing statistics xml"
116 self.statistics = []
118 self.ratio = .75
119 self.mincorrect = 5.
120 self.newquestion = []
121 self.goodquestion = []
122 self.badquestion = []
123 self.norm = 1./self.mincorrect
125 for q in self.root.getElementsByTagName("question"):
126 id = q.getAttribute ("id")
127 c = q.getAttribute ("c")
128 cs = q.getAttribute ("cs")
129 ws = q.getAttribute ("ws")
130 w = q.getAttribute ("w")
132 if float(ws) > 0.:
133 rr = 0.
134 else:
135 rr = float(cs)*self.norm
137 if rr >= self.ratio:
138 self.goodquestion.append (id)
139 elif rr >= 0:
140 self.badquestion.append (id)
141 else: # never reached
142 self.newquestion.append (id)
144 answers = []
145 for a in q.childNodes:
146 if a.nodeType == Node.ELEMENT_NODE:
147 code = a.getAttribute("answer_code")
148 time = a.getAttribute("needed_time")
149 when = a.getAttribute("datetime")
150 answers.append ([code,time,when])
152 self.statistics.append ([id, c, cs, ws, w, answers])
154 def GetPriority(self,qid):
155 return
157 def FindQuestion(self,qid):
158 i = 0
159 for s in self.statistics:
160 if s[0] == qid:
161 return i
162 i += 1
163 return -1
165 def ThisQuestion(self,qid):
166 q = self.FindQuestion (qid)
167 if q >= 0:
168 c = self.statistics[q][1]
169 w = self.statistics[q][4]
170 cs = self.statistics[q][2]
171 ws = self.statistics[q][3]
172 else:
173 c=str(0)
174 w=str(0)
175 cs=str(0)
176 ws=str(0)
177 return [str(c),str(w),str(cs),str(ws)]
179 def Timestamp(self):
180 return str(datetime.datetime.today().isoformat()).split(".")[0]
182 def __init__(self,filename="DL-A-2007.stat.xml"):
183 self.filename=filename
185 self.OpenFile()
186 self.GetStatistics()