3 from xml
.dom
import minidom
, Node
4 # should really be done with http://effbot.org/zone/element.htm
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()
17 def IncreaseCounter(self
, qid
, how
, answer
, time
):
19 Return True, is question is good
21 nq
= self
.FindQuestion (qid
)
23 for q
in self
.root
.getElementsByTagName("question"):
24 id = q
.getAttribute ("id")
28 c
= int(q
.getAttribute ("c"))
29 w
= int(q
.getAttribute ("w"))
30 cs
= int(q
.getAttribute ("cs"))
31 ws
= int(q
.getAttribute ("ws"))
35 q
= self
.stat
.createElement(u
'question')
36 q
.setAttribute("id",qid
)
38 sss
= self
.root
.getElementsByTagName("learning")[0]
46 self
.statistics
.append ([qid
, c
, cs
, ws
, w
, ""])
47 nq
= self
.FindQuestion (qid
)
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
74 q
.setAttribute("c",str(c
))
75 q
.setAttribute("w",str(w
))
76 q
.setAttribute("cs",str(cs
))
77 q
.setAttribute("ws",str(ws
))
81 a
= [1,2,4,8][(["a","b","c","d"]).index(answer
)]
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
))
93 rr
= float(cs
)*self
.norm
95 return rr
>= self
.ratio
98 print "Writing statistics file",self
.filename
99 ss
=toprettyxml_fixed(self
.root
)
100 f
=open(self
.filename
,"w")
101 print >>f
,"<!DOCTYPE AFUTrainerStatistics>"
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"
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")
136 rr
= float(cs
)*self
.norm
139 self
.goodquestion
.append (id)
141 self
.badquestion
.append (id)
142 else: # never reached
143 self
.newquestion
.append (id)
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
):
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
):
166 for s
in self
.statistics
:
172 def ThisQuestion(self
,qid
):
173 q
= self
.FindQuestion (qid
)
175 c
= self
.statistics
[q
][1]
176 w
= self
.statistics
[q
][4]
177 cs
= self
.statistics
[q
][2]
178 ws
= self
.statistics
[q
][3]
184 return [str(c
),str(w
),str(cs
),str(ws
)]
187 return str(datetime
.datetime
.today().isoformat()).split(".")[0]
189 def __init__(self
,filename
="DL-A-2007.stat.xml"):
190 self
.filename
=filename