.
[HamExam.git] / statistic.py
blob44e7decd383a021133c9da5d1b5f7dbeaac64b0b
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 nq = self.FindQuestion (qid)
18 if nq >= 0:
19 for q in self.root.getElementsByTagName("question"):
20 id = q.getAttribute ("id")
21 if id == qid:
22 break
24 c = int(q.getAttribute ("c"))
25 w = int(q.getAttribute ("w"))
26 cs = int(q.getAttribute ("cs"))
27 ws = int(q.getAttribute ("ws"))
29 else:
30 print "NEU",qid
31 q = self.stat.createElement(u'question')
32 q.setAttribute("id",qid)
34 sss = self.root.getElementsByTagName("learning")[0]
35 sss.appendChild(q)
37 c=0
38 w=0
39 cs=0
40 ws=0
42 self.statistics.append ([qid, c, cs, ws, w, ""])
43 nq = self.FindQuestion (qid)
45 if how == True:
46 c += 1
47 cs += 1
48 ws -= 1
49 else:
50 w += 1
51 ws += 1
52 cs -= 1
54 # in-memory statistics
55 self.statistics[nq][1] = c
56 self.statistics[nq][2] = cs
57 self.statistics[nq][3] = ws
58 self.statistics[nq][4] = w
60 # xml file stuff
61 q.setAttribute("c",str(c))
62 q.setAttribute("w",str(w))
63 q.setAttribute("cs",str(cs))
64 q.setAttribute("ws",str(ws))
67 t = self.Timestamp()
68 a = [1,2,4,8][(["a","b","c","d"]).index(answer)]
69 nt = int(time*1000)
71 qq = self.stat.createElement("answer_clicked")
72 qq.setAttribute ("datetime", str(t))
73 qq.setAttribute ("answer_code", str(a))
74 qq.setAttribute ("needed_time", str(nt))
75 q.appendChild(qq)
77 # FIXME: UPDATE QUESTION PRIORITIES
79 def WriteFile(self):
80 print "Writing statistics file",self.filename
81 ss=toprettyxml_fixed(self.root)
82 f=open(self.filename,"w")
83 print >>f,"<!DOCTYPE AFUTrainerStatistics>"
84 f.write(ss)
85 f.close()
87 def OpenFile(self):
88 print "Opening statistics file",self.filename
89 self.stat = minidom.parse (self.filename)
90 self.root = self.stat.documentElement
92 self.date = self.root.getAttribute ("date")
93 self.version = self.root.getAttribute ("version")
94 self.name = self.root.getAttribute ("name")
97 def GetStatistics(self):
98 print "Parsing statistics xml"
99 self.statistics = []
101 self.ratio = .75
102 self.mincorrect = 5.
103 self.newquestion = []
104 self.goodquestion = []
105 self.badquestion = []
106 norm = 1./self.mincorrect
108 for q in self.root.getElementsByTagName("question"):
109 id = q.getAttribute ("id")
110 c = q.getAttribute ("c")
111 cs = q.getAttribute ("cs")
112 ws = q.getAttribute ("ws")
113 w = q.getAttribute ("w")
115 if float(ws) > 0.:
116 rr = 0.
117 else:
118 rr = float(cs)*norm
120 if rr >= self.ratio:
121 self.goodquestion.append (id)
122 elif rr >= 0:
123 self.badquestion.append (id)
124 else: # never reache # never reachedd
125 self.newquestion.append (id)
127 answers = []
128 for a in q.childNodes:
129 if a.nodeType == Node.ELEMENT_NODE:
130 code = a.getAttribute("answer_code")
131 time = a.getAttribute("needed_time")
132 when = a.getAttribute("datetime")
133 answers.append ([code,time,when])
135 self.statistics.append ([id, c, cs, ws, w, answers])
137 def GetPriority(self,qid):
138 return
140 def FindQuestion(self,qid):
141 i = 0
142 for s in self.statistics:
143 if s[0] == qid:
144 return i
145 i += 1
146 return -1
148 def ThisQuestion(self,qid):
149 q = self.FindQuestion (qid)
150 if q >= 0:
151 c = self.statistics[q][1]
152 w = self.statistics[q][4]
153 cs = self.statistics[q][2]
154 ws = self.statistics[q][3]
155 else:
156 c=str(0)
157 w=str(0)
158 cs=str(0)
159 ws=str(0)
160 return [str(c),str(w),str(cs),str(ws)]
162 def Timestamp(self):
163 return str(datetime.datetime.today().isoformat()).split(".")[0]
165 def __init__(self,filename="DL-A-2007.stat.xml"):
166 self.filename=filename
168 self.OpenFile()
169 self.GetStatistics()