.
[HamExam.git] / statistic.py
blobcdc510cb2fec47c88f6519e4101ac64c3e5f7b4a
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):
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 else:
49 w += 1
50 ws += 1
52 # in-memory statistics
53 self.statistics[nq][1] = c
54 self.statistics[nq][2] = cs
55 self.statistics[nq][3] = ws
56 self.statistics[nq][4] = w
58 # xml file stuff
59 q.setAttribute("c",str(c))
60 q.setAttribute("w",str(w))
61 q.setAttribute("cs",str(cs))
62 q.setAttribute("ws",str(ws))
65 t = self.Timestamp()
66 a = [1,2,4,8][(["a","b","c","d"]).index(answer)]
67 nt = 15000 # FIXME
69 qq = self.stat.createElement("answer_clicked")
70 qq.setAttribute ("datetime", str(t))
71 qq.setAttribute ("answer_code", str(a))
72 qq.setAttribute ("needed_time", str(nt))
73 q.appendChild(qq)
75 # FIXME: UPDATE QUESTION PRIORITIES
77 def WriteFile(self):
78 print "Writing statistics file",self.filename
79 ss=toprettyxml_fixed(self.root)
80 f=open(self.filename,"w")
81 print >>f,"<!DOCTYPE AFUTrainerStatistics>"
82 f.write(ss)
83 f.close()
85 def OpenFile(self):
86 print "Opening statistics file",self.filename
87 self.stat = minidom.parse (self.filename)
88 self.root = self.stat.documentElement
90 self.date = self.root.getAttribute ("date")
91 self.version = self.root.getAttribute ("version")
92 self.name = self.root.getAttribute ("name")
95 def GetStatistics(self):
96 print "Parsing statistics xml"
97 self.statistics = []
99 self.ratio = .75
100 self.new = []
101 self.good = []
102 self.bad = []
103 self.nnew = 0
104 self.ngood = 0
105 self.nbad = 0
107 for q in self.root.getElementsByTagName("question"):
108 id = q.getAttribute ("id")
109 c = q.getAttribute ("c")
110 cs = q.getAttribute ("cs")
111 ws = q.getAttribute ("ws")
112 w = q.getAttribute ("w")
114 if ws > 0:
115 rr = 1.*cs/ws
116 else:
117 rr = .5
119 if rr >= self.ratio:
120 self.good.append (id)
121 self.ngood += 1
122 elif rr >= 0:
123 self.bad.append (id)
124 self.nbad += 1
125 else:
126 self.new.append (id)
127 self.nnew += 1
129 answers = []
130 for a in q.childNodes:
131 if a.nodeType == Node.ELEMENT_NODE:
132 code = a.getAttribute("answer_code")
133 time = a.getAttribute("needed_time")
134 when = a.getAttribute("datetime")
135 answers.append ([code,time,when])
137 self.statistics.append ([id, c, cs, ws, w, answers])
139 def FindQuestion(self,qid):
140 i = 0
141 for s in self.statistics:
142 if s[0] == qid:
143 return i
144 i += 1
145 return -1
147 def ThisQuestion(self,qid):
148 q = self.FindQuestion (qid)
149 if q >= 0:
150 c = self.statistics[q][1]
151 w = self.statistics[q][4]
152 else:
153 c=str(0)
154 w=str(0)
155 return [c,w]
157 def Timestamp(self):
158 return str(datetime.datetime.today().isoformat()).split(".")[0]
160 def __init__(self,filename="DL-A-2007.stat.xml"):
161 self.filename=filename
163 self.OpenFile()
164 self.GetStatistics()