aboutsummaryrefslogtreecommitdiffhomepage
path: root/quran.py
blob: a34f9da55cc509cb904bff0f74909b0b0022e357 (plain)
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
# -*- coding: utf-8 -*-
import re
from random import randint
import xml.etree.ElementTree as ET


def parse_quran_trans():
    """Parse translation text files (with ayah numbers) from
    http://tanzil.net/trans/.
    """
    quran = []
    surah = []
    s = 1

    def process_verse(verse):
        """Add verse and replace for Arabic ligatures (salawat)"""
        return (verse.strip()
                .replace("– peace and blessings be upon him", 'ﷺ‎'))

    with open("en.ahmedraza", 'r') as f:
        for line in f.readlines():
            if line == '\n': continue
            if line[:3] == "#==": break
            verse = line.split('|')
            assert len(verse) == 3
            if int(verse[0]) == s:
                surah.append(process_verse(verse[2]))
            else:
                quran.append(surah)
                surah = [process_verse(verse[2])]
                s += 1
    quran.append(surah)
    assert sum([len(s) for s in quran]) == 6236, "Missing verses!"
    return quran


def parse_quran_tafsir():
    """Parse tafsir al-Jalalayn from http://www.altafsir.com/Al-Jalalayn.asp, after
    that PDF was processed with ``pdftotext -nopgbrk Al_Jalalain_Eng.pdf``.
    """
    quran = []
    surah = []
    s, v = 1, 1
    in_verse = False
    verse = []

    def add_verse(verse, surah):
        surah.append(' '.join(verse))

    def add_line(line, verse):
        """Add line and replace for Arabic ligatures (salawat)"""
        verse.append(line.strip().replace("(s)", 'ﷺ‎'))

    with open("Al_Jalalain_Eng.txt", 'r') as f:
        for line in f.readlines():
            if line == '\n': continue
            if re.match("\d+\w*", line): continue
            elif line.startswith("[%d:%d]" % (s, v)):
                in_verse = True
            elif line.startswith("[%d:%d]" % (s, v + 1)):
                add_verse(verse, surah)
                verse = []
                v += 1
                in_verse = True
            elif line.startswith("[%d:1]" % (s + 1)):
                add_verse(verse, surah)
                verse = []
                quran.append(surah)
                surah = []
                s += 1
                v = 1
                in_verse = True
            elif (line.startswith("Medinese") or line.startswith("Meccan") or
                  line.startswith("[Consists") or  # [Consists end surah 5
                  line.startswith("Mecca, consisting") or  # end surah 73
                  line.startswith("This was revealed")):
                if s == 26 and v == 200:
                    # only line starting with "Meccan" that is part of a verse
                    add_line(line, verse)
                else:
                    in_verse = False
            elif in_verse:
                add_line(line, verse)
    add_verse(verse, surah)
    quran.append(surah)
    assert sum([len(s) for s in quran]) == 6236, "Missing verses!"
    return quran


class Quran(object):
    """Interface to get ayahs from the Quran."""
    surah_lengths = [0, 7, 286, 200, 176, 120, 165, 206, 75, 129, 109, 123, 111, 43, 52, 99, 128, 111, 110, 98, 135, 112, 78, 118, 64, 77, 227, 93, 88, 69, 60, 34, 30, 73, 54, 45, 83, 182, 88, 75, 85, 54, 53, 89, 59, 37, 35, 38, 29, 18, 45, 60, 49, 62, 55, 78, 96, 29, 22, 24, 13, 14, 11, 11, 18, 12, 12, 30, 52, 52, 44, 28, 28, 20, 56, 40, 31, 50, 40, 46, 42, 29, 19, 36, 25, 22, 17, 19, 26, 30, 20, 15, 21, 11, 8, 8, 19, 5, 8, 8, 11, 11, 8, 3, 9, 5, 4, 7, 3, 6, 3, 5, 4, 5, 6]
    # [0] for normal numbering

    def __init__(self, data):
        if data == "translation":
            self.text = parse_quran_trans()
        elif data == "tafsir":
            self.text = parse_quran_tafsir()

    def getSurah(self, surah):
        """Get surah by number."""
        return self.text[surah - 1]

    def getAyah(self, surah, ayah):
        """Get verse by surah and ayah numbers."""
        return self.text[surah - 1][ayah - 1] + " (%d:%d)" % (surah, ayah)

    def getAyahs(self, surah, a, b):
        """Get range of Ayahs as tuple """
        return ' '.join(self.text[surah - 1][a - 1:b]) + " (%d:%d-%d)" % (surah, a, b)

    @staticmethod
    def getRandomAyah():
        surah = randint(1, 114)
        ayah = randint(1, Quran.surah_lengths[surah])
        return surah, ayah

    @staticmethod
    def getNextAyah(s, a):
        length = Quran.surah_lengths[s]
        if a == length:
            s = s + 1 if s < 114 else 1
            a = 1
        else:
            a += 1
        return s, a

    @staticmethod
    def getPreviousAyah(s, a):
        if a == 1:
            s = s - 1 if s > 1 else 114
            a = Quran.surah_lengths[s]
        else:
            a -= 1
        return s, a


def make_index():
    """An index of the Surahs in the Quran, formatted to send over Telegram."""
    suras = ET.parse("quran-data.xml").getroot().find("suras")
    chapters = [s.attrib["tname"] for s in suras]
    # padding...
    for i in range(9):
        chapters[i] = ' ' + chapters[i] + ' ' * (14 - len(chapters[i]))
    for i in range(9, 58):
        chapters[i] += ' ' * (14 - len(chapters[i]))

    index = []
    left = range(1, 58)
    right = range(58, 115)
    for i, j in zip(left, right):
        index.append("/{} <code>{}</code>/{} {}"
                     .format(i, chapters[i - 1], j, chapters[j - 1]))
    return '\n'.join(index)


def save_json(quran):
    """Save Quran to a json file."""
    import json

    with open("quran.json", 'w') as f:
        json.dump(quran, f, ensure_ascii=False)