-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_statistics.php
257 lines (236 loc) · 8.56 KB
/
global_statistics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="style.css" type="text/css" />
<link href="filetree/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var colors=['00FF00', 'FFFF00', 'FF00FF', 'FF0000', '00FFFF', 'C00000', '00C000', 'C0C000', '0000FF'];
$(document).ready(function() {
for(var i=0;i<colors.length;i++)
{
addCSSRule(".c"+colors[i]+", "+"#c"+colors[i]+", .s"+colors[i]+", "+"#s"+colors[i],"background-color","#"+colors[i])
}
$('body').on('click','#hide_detail_view', function() {
$(".combinations").find('.detail_view').toggle();
});
});
function addCSSRule(sel, prop, val) {
for(var i = 0; i < document.styleSheets.length; i++){
var ss = document.styleSheets[i];
var rules = (ss.cssRules || ss.rules);
var lsel = sel.toLowerCase();
for(var i2 = 0, len = rules.length; i2 < len; i2++){
if(rules[i2].selectorText && (rules[i2].selectorText.toLowerCase() == lsel)){
if(val != null){
rules[i2].style[prop] = val;
return;
}
else{
if(ss.deleteRule){
ss.deleteRule(i2);
}
else if(ss.removeRule){
ss.removeRule(i2);
}
else{
rules[i2].style.cssText = '';
}
}
}
}
}
var ss = document.styleSheets[0] || {};
if(ss.insertRule) {
var rules = (ss.cssRules || ss.rules);
ss.insertRule(sel + '{ ' + prop + ':' + val + '; }', rules.length);
}
else if(ss.addRule){
ss.addRule(sel, prop + ':' + val + ';', 0);
}
};
</script>
</head>
<body>
<pre>
<?php
//atrod visus xml failus
$directory="faili/gatavie";
$files=array();
$extensions=array("xml");
$new_dirs=array($directory);
//izpilda ciklu kamēr ir atrastas jaunas direktorijas
while(!empty($new_dirs))
{
$dirs=$new_dirs;
$new_dirs=array();
//iet cauri visām atrastajām direktorijām
foreach($dirs as $dir)
{
$file = scandir($dir);
//iet cauri visiem failiem direktorijā
foreach($file as $this_file) {
if( is_dir("$dir/$this_file" ) ) //ja atrasta direktorija, peivieno to direktoriju sarakstam
{
if( $this_file != "." && $this_file != ".." ) //izvairāmies no ieciklošanās .. un . gadījumā
{
$new_dirs[] = "$dir/$this_file";
}
}
else //ja atrada failu
{
$ext = substr($this_file, strrpos($this_file, ".") + 1);
if(in_array($ext, $extensions) ) //ja faili ar šādu paplašinājumu ir jāapstrādā, pievieno to failu sarakstam
{
$files[] = "$dir/$this_file";
}
}
}
}
}
//$files=array('faili/gatavie/varet.xml');
//veidojam statistiku no visiem failiem
$cats=Array();
//izveidojam noklusēto verbu
$cats['verbs']=array();
$cats['verbs']['id']='verbs';
$cats['verbs']['name']='verbs';
$cats['verbs']['count']=0;
$cats['verbs']['words']=array();
$sentence_cats=Array();
//izveidojam noklusēto blank
$sentence_cats['blank']=array();
$sentence_cats['blank']['id']='blank';
$sentence_cats['blank']['name']='blank';
$sentence_cats['blank']['count']=0;
$sentence_cats['blank']['combinations']=array();
foreach($files as $file)
{
if(!file_exists($file))
{
echo "Nevar atvērt ".$file."<br />";
continue;
}
$saturs=file_get_contents($file);
$DOMdoc = new DOMDocument();
if(!@$DOMdoc->loadXML($saturs))
{
echo "Nevar atvērt ".$file."<br />";
}
$DOMPath = new DOMXPath( $DOMdoc );
$categories=$DOMPath->query("//CATEGORY");
$this_cats=Array();
$this_cats['verbs']=&$cats['verbs'];
foreach($categories AS $category)
{
if(!isset($cats[mb_strtolower($category->nodeValue)]))
{
$cats[mb_strtolower($category->nodeValue, 'UTF-8')]=array();
$cats[mb_strtolower($category->nodeValue, 'UTF-8')]['id']=$category->getAttribute("id");
$cats[mb_strtolower($category->nodeValue, 'UTF-8')]['name']=$category->nodeValue;
$cats[mb_strtolower($category->nodeValue, 'UTF-8')]['count']=0;
$cats[mb_strtolower($category->nodeValue, 'UTF-8')]['words']=array();
}
$this_cats[$category->getAttribute("id")]=&$cats[mb_strtolower($category->nodeValue, 'UTF-8')];
}
$sentence_categories=$DOMPath->query("//SENTENCE_CATEGORY");
$this_sentence_cats=Array();
$this_sentence_cats['blank']=&$sentence_cats['blank'];
foreach($sentence_categories AS $sentence_category)
{
if(!isset($sentence_cats[mb_strtolower($sentence_category->nodeValue)]))
{
$sentence_cats[mb_strtolower($sentence_category->nodeValue, 'UTF-8')]=array();
$sentence_cats[mb_strtolower($sentence_category->nodeValue, 'UTF-8')]['id']=$sentence_category->getAttribute("id");
$sentence_cats[mb_strtolower($sentence_category->nodeValue, 'UTF-8')]['name']=$sentence_category->nodeValue;
$sentence_cats[mb_strtolower($sentence_category->nodeValue, 'UTF-8')]['count']=0;
$sentence_cats[mb_strtolower($sentence_category->nodeValue, 'UTF-8')]['combinations']=array();
}
$this_sentence_cats[$sentence_category->getAttribute("id")]=&$sentence_cats[mb_strtolower($sentence_category->nodeValue, 'UTF-8')];
}
$sentences=$DOMPath->query("//SENTENCE");
foreach($sentences AS $sentence)
{
$text=$DOMPath->query("TEXT",$sentence)->item(0);
$key_insensitive="";
//uzģenerē teikuma virkni neņemot vērā secību
foreach($this_cats AS $key=>&$cat)
{
if(substr($cat['name'],-1)=='?') continue; //izlaižam kategorijas ar jautājuma zīmi beigās
$words=$DOMPath->query("WORD[@class='{$key}']",$text);
foreach($words AS $word)
{
$key_insensitive.="<span class=\"{$cat['id']}\">{$cat['name']}</span>";
if(!isset($cat['words'][$word->getAttribute("attribute")]))
{
$cat['words'][$word->getAttribute("attribute")]=0;
}
$cat['words'][$word->getAttribute("attribute")]++;
$cat['count']++;
}
}
//uzģenerē teikuma virkni ņemot vērā secību
$key="";
$words=$DOMPath->query("WORD",$text);
foreach($words AS $word)
{
$cat=&$this_cats[$word->getAttribute("class")];
if(substr($cat['name'],-1)=='?') continue; //izlaižam kategorijas ar jautājuma zīmi beigās
$key.="<span class=\"{$cat['id']}\">{$cat['name']}</span>";
}
//pievieno kombināciju masīvam teikumu
if(!isset($this_sentence_cats[$sentence->getAttribute('CATEGORY')]['combinations'][$key_insensitive]))
{
$this_sentence_cats[$sentence->getAttribute('CATEGORY')]['combinations'][$key_insensitive]=Array("count"=>0,"keys"=>Array());
}
if(!isset($this_sentence_cats[$sentence->getAttribute('CATEGORY')]['combinations'][$key_insensitive]['keys'][$key]))
{
$this_sentence_cats[$sentence->getAttribute('CATEGORY')]['combinations'][$key_insensitive]['keys'][$key]=0;
}
$this_sentence_cats[$sentence->getAttribute('CATEGORY')]['combinations'][$key_insensitive]['count']++;
$this_sentence_cats[$sentence->getAttribute('CATEGORY')]['combinations'][$key_insensitive]['keys'][$key]++;
$this_sentence_cats[$sentence->getAttribute('CATEGORY')]['count']++;
}
}
echo '</pre><div id="atskaite"><div class="combinations"><span id="hide_detail_view">Paslēpt detalizēto skatu</span>';
foreach($sentence_cats AS &$cat) //cikls katrai teikuma kategorijai
{
echo '<h2 class="'.$cat['id'].'">'.$cat['name'].' '.$cat['count'].'</h2>';
foreach($cat['combinations'] AS &$combination) //cikls katrai kombinācijai
{
$max=0;
$max_c=null;
foreach($combination['keys'] AS $key=>$value)//cikls lai atrastu populārāko kombināciju
{
if($value>$max)
{
$max=$value;
$max_c=$key;
}
}
echo '<h3>'.$max_c.' '.$combination['count'].'</h3>';;
echo '<table cellspacing="0" cellpadding="0" border="0" class="detail_view"><tr><th>Kombinācija</th><th>Skaits</th></tr>';
foreach($combination['keys'] AS $key=>$value)
{
echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
}
echo '</table>';
}
}
echo '</div>';
echo '<table class="countReport" cellspacing="0" cellpadding="0" border="0"><tr><th>Kategorija</th><th>Skaits</th><th>Grupas</th></tr>';
foreach($cats AS &$cat)
{
echo '<tr><td><span class="'.$cat['id'].'">'.$cat['name'].'</span></td><td>'.$cat['count'].'</td><td><table cellspacing="0" cellpadding="0" border="0"><tr><th>Grupa</th><th>Skaits</th></tr>';
foreach($cat['words'] AS $word=>$value)
{
echo '<tr><td>'.$word.'</td><td>'.$value.'</td></tr>';
}
echo '</table></td></tr>';
}
echo "</table>";
?>
</div>
</body>
</html>