-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.php
278 lines (250 loc) · 6.43 KB
/
util.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
// Set the file location
define('FILE', 'words.txt');
/**
* Adds a new key if it does not exist, and a new word
* that belongs in a chain to that key, again, if it does
* not exist. If the key already exists, just add the new
* word
*
* @param string $parent
* the key, the "parent" word to be added or added to
* @param string $child
* the "child" word to be added to the "parent"'s list.
* @param array &$words
* the word list
*/
function add($parent, $child, &$words) {
if ($parent == $child) {
echo "No infinite loops! Nice try!\n";
}
// If the parent already exists
else if (array_key_exists($parent, $words)) {
// Check if the child exists
if(array_search($child,$words[$parent]) === FALSE) {
// Add the new word after suggesting other words.
array_push( $words[$parent] , $child );
}
// Suggest other words
$suggest_string = "Existing " . $parent . " words: ";
$suggest_string .= print_children_nicely($words[$parent], $child);
echo $suggest_string . "\n";
}
else {
echo "New root word!\n";
$words[$parent] = [$child];
}
}
/**
* Load the word list from the file.
*/
function load() {
$words = [];
$handle = fopen(FILE, "r") or die("Unable to open file for reading.\n");
if ($handle) {
while (($line = fgetcsv($handle)) !== false) {
// process the line read.
$key = array_shift($line);
$words[$key] = $line;
}
fclose($handle);
}
echo "Loaded word list.\n";
return $words;
}
/**
* Save the word list to the file.
*
* @param array $words
* The word list to save.
*/
function save(&$words) {
$handle = fopen(FILE, "w") or die("Unable to open file for writing.\n");
foreach($words as $parent => $child) {
array_unshift($child,$parent);
fputcsv($handle, $child);
}
fclose($handle);
}
/**
* Find words that don't have children.
* @param array $words
* word list
* @param boolean $print
* do I print the words as well as return them?
*/
function find_childless(&$words,$print = FALSE) {
$childless_words = [];
foreach ($words as $parent) {
foreach ($parent as $child) {
if (!array_key_exists($child,$words)) {
array_push($childless_words,$child);
}
}
}
if ($print) {
foreach($childless_words as $nk) {
echo $nk . "\n";
}
}
return($childless_words);
}
/**
* Get all words in list.
*
* @param array &$words
* The regular list of words and their children.
* @return array
* The entire list of words, including children. The words are the key,
* as an added bonus, the number of times they appear is the value.
*/
function get_all_words(&$words) {
$all_words = [];
foreach ($words as $parent => $children) {
if (array_key_exists($parent, $all_words)) {
$all_words[$parent] += 1;
}
else {
$all_words[$parent] = 1;
}
foreach ($children as $child) {
if (array_key_exists($child, $all_words)) {
$all_words[$child] += 1;
}
else {
$all_words[$child] = 1;
}
}
}
array_multisort($all_words);
return $all_words;
}
/**
* Remove duplicate children, just in case.
*
* @param array &$words
* The regular list of words and their children.
*
* @return string
* The number of duplicates removed.
*/
function dedup_children(&$words) {
$removed = 0;
foreach ($words as $key => $parent) {
$before = count($parent);
$words[$key] = array_unique($parent, SORT_STRING);
$removed += $before - count($words[$key]);
}
save($words);
return $removed;
}
/**
* Count all word pairs.
*
* @param array &$words
* The regular list of words and their children.
*
* @return int
* The number of word pairs.
*/
function count_word_pairs(&$words) {
$count = 0;
foreach ($words as $word) {
$count += (count($word));
}
return $count;
}
/**
* Count distinct words
*
* @param array &$words
* The regular list of words and their children.
*
* @return int
* The number of unique words.
*/
function count_words(&$words) {
$all = get_all_words($words);
return count($all);
}
/**
* Get children of a single parent.
*
* @param array &$words
* The regular list of words and their children.
* @param string $parent
* The parent word for whose kids we are searching.
*
* @return array
* An array of strings representing the children of that word, or FALSE
* if there are no children.
*/
function get_children(&$words,$parent) {
if (array_key_exists($parent, $words)) {
return $words[$parent];
}
else
return FALSE;
}
/**
* Print children nicely.
*
* @param array $children
* An array of strings.
*
* @param string $candidate
* Optional. A possibly new word. Make fully uppercase to distinguish.
*
* @return string
* A nice string of children, separated by a comma and a space.
* FALSE on error.
*/
function print_children_nicely($children, $candidate="") {
$results = '';
if (count($children) == 0) {
echo "Error: No children\n";
return FALSE;
}
foreach ($children as $child) {
if ($candidate == $child) {
$results .= strtoupper($child) . ", ";
}
else {
$results .= $child . ", ";
}
}
$results = rtrim($results,', ');
return $results;
}
/**
* Get the parent word(s) with the highest number of children.
*
* @param array &$words
* The regular list of words and their children.
*
* @return array
* An array containing the word(s) and their children quantity.
*/
function get_most_children(&$words) {
$sorted_words = [];
$max = 0;
$top_words = [];
foreach($words as $parent => $child) {
$sorted_words[$parent] = count($child);
if (count($child) > $max) {
$max = count($child);
}
}
// Sort the array by value.
arsort($sorted_words);
foreach ($sorted_words as $sorted_word => $size) {
if ($size == $max) {
//array_push($top_words,array($sorted_word, $size));
array_push($top_words, $sorted_word);
}
}
//$top_words['size'] = $max;
$results['words'] = $top_words;
$results['size'] = $max;
return $results;
}