-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate-ayat.php
More file actions
67 lines (52 loc) · 2.11 KB
/
populate-ayat.php
File metadata and controls
67 lines (52 loc) · 2.11 KB
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
<?php
define('AYAT_FILE', '/home/ubuntu/apps/quran-finder.com/app/quranText.php');
define('AYAT_DIACRITIZED_FILE', '/home/ubuntu/apps/quran-finder.com/app/quranTextSigned.php');
ob_start();
// open each file for text reading
$ayat = fopen(AYAT_FILE, 'r');
$ayatDiacritized = fopen(AYAT_DIACRITIZED_FILE, 'r');
/*
For $ayatFile:
ignoring the first line, for every line, the format is:
suraNum/ayaNum:ayaText
so we need to extract the ayaNum, suraNum, and ayaText, then save ayaText to the file "app/text/ar/$suraNum/$ayaNum/index.txt"
For $ayatDiacritizedFile:
ignoring the first line, for every line, the format is:
suraNum/ayaNum:ayaText
so we need to extract the ayaNum, suraNum, and ayaText, then save ayaText to the file "app/text/ar/$suraNum/$ayaNum/diacritized/index.txt"
*/
// read the first line of each file
fgets($ayat);
fgets($ayatDiacritized);
// read each line of the file
while (!feof($ayat) && !feof($ayatDiacritized)) {
// read the line
$line = trim(fgets($ayat));
$lineDiacritized = trim(fgets($ayatDiacritized));
// if line is empty, skip
if (empty($line) || empty($lineDiacritized)) continue;
// extract the ayaNum, suraNum, and ayaText
$line = explode(':', $line);
$lineDiacritized = explode(':', $lineDiacritized);
list($ayaNum, $suraNum) = explode('/', $line[0]);
$ayaText = trim($line[1]);
$ayaTextDiacritized = trim($lineDiacritized[1]);
// Prepend unicode BOM to the text
$ayaText = "\xEF\xBB\xBF" . $ayaText;
$ayaTextDiacritized = "\xEF\xBB\xBF" . $ayaTextDiacritized;
// save ayaText to the file "app/text/ar/$suraNum/$ayaNum/index.txt"
$dir = __DIR__ . "/app/text/ar/$suraNum/$ayaNum";
if (!file_exists($dir)) continue;
file_put_contents("$dir/index.txt", $ayaText);
// save ayaTextDiacritized to the file "app/text/ar/$suraNum/$ayaNum/diacritized/index.txt"
$dir = __DIR__ . "/app/text/ar/$suraNum/$ayaNum/diacritized";
if (!file_exists($dir)) continue;
file_put_contents("$dir/index.txt", $ayaTextDiacritized);
// single-line progress to stdout
printf("\rProcessing Aya %03d of Sura %03d", $ayaNum, $suraNum);
ob_flush();
flush();
// sleep for 1ms
usleep(1000);
}
echo "\nDone!";