-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_text.php
More file actions
41 lines (28 loc) · 887 Bytes
/
Copy pathprocess_text.php
File metadata and controls
41 lines (28 loc) · 887 Bytes
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
<?php
include 'mobydick_wo_chapters.php';
$matches = [];
preg_match_all('/[A-Z][^\.!\?]*[\.!\?]/', $mobydick, $matches);
$characters = [];
$words = [];
foreach( $matches[0] as $match ) {
// add to characters
$length = mb_strlen($match);
if (isset($words[$length])) {
$characters[$length][] = $match;
} else {
$characters[$length] = [];
$characters[$length][] = $match;
}
// add to words
$wordCount = str_word_count($match, 0, '’-');
if (isset($words[$wordCount])) {
$words[$wordCount][] = $match;
} else {
$words[$wordCount] = [];
$words[$wordCount][] = $match;
}
}
ksort($characters);
ksort($words);
file_put_contents('characters.php', '<?php $characters = ' . var_export($characters, true) . ';');
file_put_contents('words.php', '<?php $words = ' . var_export($words, true) . ';');