-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
162 lines (146 loc) · 4.07 KB
/
functions.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
<?php
/**
* @author Alexander Bresk ([email protected])
* @version 0.0.2
* @project semmap
**/
/**
* calculates correlations based on the observations
**/
function calculate_correlations($correlations){
$calculated_correlations = array();
$keys = array_keys($correlations);
for($i = 0, $n = count($keys); $i < $n; $i++){
$sum = array_sum($correlations[$keys[$i]]);
foreach($correlations[$keys[$i]] as $key => $value){
if($sum != 0){
$calculated_correlations[$keys[$i]][$key] = floatval($correlations[$keys[$i]][$key]) / floatval($sum);
}else{
$calculated_correlations[$keys[$i]][$key] = 0;
}
}
}
return $calculated_correlations;
}
/**
* counts observations of correlations
**/
function count_correlations(&$strong_correlations, &$weak_correlations, &$reverse_map, &$contents){
$tokens = explode(' ', $contents);
$num_of_tokens = count($tokens);
# first step, count occurences
$i = 0;
foreach($tokens as $token){
if(array_key_exists($token, $reverse_map)){
$frames = $reverse_map[$token];
$start_weak = (($i - WEAK_BOUND < 0)? 0 : ($i - WEAK_BOUND));
$start_strong = (($i - STRONG_BOUND < 0)? 0 : ($i - STRONG_BOUND));
$end_weak = (($i + WEAK_BOUND > $num_ok_tokens)? ($num_of_tokens - 1) : ($i + WEAK_BOUND));
$end_strong = (($i + STRONG_BOUND > $num_ok_tokens)? ($num_of_tokens - 1) : ($i + STRONG_BOUND));
# weak correlations
for(; $start_weak <= $end_weak; $start_weak++){
if(array_key_exists($tokens[$start_weak], $reverse_map)){
$frames_to = $reverse_map[$tokens[$start_weak]];
foreach($frames as $frame)
foreach($frames_to as $f)
$weak_correlations[$frame][$f]++;
}
}
# strong correlations
for(; $start_strong <= $end_strong; $start_strong++){
if(array_key_exists($tokens[$start_strong], $reverse_map)){
$frames_to = $reverse_map[$tokens[$start_strong]];
foreach($frames as $frame)
foreach($frames_to as $f)
$strong_correlations[$frame][$f]++;
}
}
}
$i++;
}
}
/**
* sets up the corrleation array with all semantic frames as matrix
**/
function build_correlations_array(&$semmap){
$array = array();
foreach($semmap as $key => $value){
$array[$key] = array();
foreach($semmap as $k => $v){
$array[$key][$k] = 0;
}
}
return $array;
}
/**
* used to replace tokens from the content
**/
function replace_tokens(&$contents, $mixed){
foreach($mixed as $k => $v){
$contents = str_replace($k, $v, $contents);
}
}
/**
* loads the semmap from json file
**/
function load_semmap($file){
$content = file($file);
$content = implode('', $content);
return json_decode($content);
}
/**
* builds a reverse semmap for a faster lookup
**/
function reverse_semmap(&$semmap){
$reverse_map = array();
foreach($semmap as $frame => $parts){
foreach($parts as $part){
if(!is_array($reverse_map[$part])){
$reverse_map[$part] = array();
}
$reverse_map[$part][] = $frame;
}
}
return $reverse_map;
}
/**
* returns an array of files from the given arguments
**/
function get_files($dir){
if (!is_dir($dir))
return array($dir);
$results = array();
$files = scandir($dir);
foreach($files as $key => $value){
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if(!is_dir($path)) {
$results[] = $path;
} else if(is_dir($path) && $value != "." && $value != "..") {
$results[] = get_files($path);
$results[] = $path;
}
}
return $results;
}
/**
* mesaures actions of the script
**/
function start_measure($msg){
echo "-> " , $msg, " ... ";
return time();
}
/**
* stops the mesaurement of the script
**/
function stop_measure($time){
echo "[" , (time() - $time) , " sec]" , PHP_EOL;
}
/**
* shows the help on cmd line
**/
function show_help($arguments){
echo "semmap " , VERSION , PHP_EOL , PHP_EOL;
echo "Use this script with one parameter:" , PHP_EOL, PHP_EOL;
echo "php " , $arguments[0] , " <file or directory>" , PHP_EOL;
}
?>