-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapterSplicer.php
155 lines (123 loc) · 4.64 KB
/
ChapterSplicer.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
<?php
class ChapterSplicer
{
// ----------------------------------------------------------------
// vars
// string
// holds the string read from the file name passed in at init
public $fileText;
// array
// holds the chapter names to be searched for
public $chapterNames;
// array
// holds the positions of the chapter names found
public $positionsFound;
// array
// holds the substrings of the chapters
public $chapterTexts;
// ----------------------------------------------------------------
// fn: constructor
// takes a file location and list of chapter names
public function __construct($fileName, $chptNames)
{
$this->fileText = $this->getTextFile($fileName);
$this->chapterNames = $chptNames;
$this->positionsFound = [];
$this->chapterTexts = [];
}
// fn: get the text file
// returns a string of the file text
public function getTextFile($fileName)
{
return file_get_contents($fileName);
}
// fn: look for occurence of a keyword
// returns an int
public function hasChapterName($text, $chapterName)
{
return strpos($text, $chapterName);
}
// fn: look for occurence of each chapter name
// sets this $positionsFound array
public function hasChapters()
{
$chapterStrPositions = [];
foreach ($this->chapterNames as $chptName)
{
$loc = $this->hasChapterName($this->fileText, $chptName);
array_push($chapterStrPositions, $loc);
$loc = 0;
}
$this->positionsFound = $chapterStrPositions;
}
// fn: get positions found
public function getPositionsFound()
{
return $this->positionsFound;
}
// fn: get length of text file
public function getLengthTextFile()
{
return strlen($this->fileText);
}
// fn: make a substring
// returns a string
public function makeSubString($start, $stop)
{
return substr($this->fileText, $start, $stop);
}
// report on start and stop positions of the chapters locations
public function seeChapterResults()
{
$numChpts = count($this->positionsFound);
for ($i = 0; $i < $numChpts; $i++)
{
if ( ($i + 1) < $numChpts )
{
echo "ready to make substring from pos " . $this->positionsFound[$i] . " of len " . ( $this->positionsFound[$i+1] - $this->positionsFound[$i]);
echo ". i.e. chapter " . ($i + 1);
echo "\n";
}
else
{
// echo "working with the last chapter";
echo "ready to make substring from pos " . $this->positionsFound[$numChpts - 1] . " of len " . ($this->getLengthTextFile() - $this->positionsFound[$numChpts - 1] );
echo ". i.e. chapter " . ($i + 1);
echo "\n";
}
}
} // end seeChapterResults()
// fn: make substrings from text and chapter positions
// pushes substrings to this->chapterTexts
public function makeChapterSubstrings()
{
$numChpts = count($this->positionsFound);
for ($i = 0; $i < $numChpts; $i++)
{
if ( ($i + 1) < $numChpts )
{
// make a substring from the start of the chapter to the length of the chapter
// make substring from pos $this->positionsFound[$i] to pos ( $this->positionsFound[$i+1] - 1 );
$start = $this->positionsFound[$i];
$lenSubStr = ( $this->positionsFound[$i+1] - $start);
$chapter = substr($this->fileText, $start, $lenSubStr);
array_push($this->chapterTexts, $chapter);
}
else
{
// working with the last chapter
// make substring from pos $this->positionsFound[$numChpts - 1] to pos $this->getLengthTextFile();
$chapter = substr($this->fileText, $this->positionsFound[$numChpts - 1], ($this->getLengthTextFile() - $this->positionsFound[$numChpts - 1] ) );
array_push($this->chapterTexts, $chapter);
}
}
} // end makeChapterSubstrings()
// fn: make chapter files from substrings collected
public function makeChapterFiles($targetFileNames)
{
for($i = 0; $i < count($targetFileNames); $i++)
{
file_put_contents($targetFileNames[$i], $this->chapterTexts[$i]);
}
}
}