-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathidx.go
More file actions
195 lines (163 loc) · 4.4 KB
/
idx.go
File metadata and controls
195 lines (163 loc) · 4.4 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
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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/asim/reminder/hadith"
"github.com/asim/reminder/names"
"github.com/asim/reminder/quran"
"github.com/asim/reminder/search"
)
// Checkpoint tracks indexing progress for resumption
type Checkpoint struct {
QuranChapter int `json:"quran_chapter"`
QuranVerse int `json:"quran_verse"`
QuranDone bool `json:"quran_done"`
NamesDone bool `json:"names_done"`
HadithBook int `json:"hadith_book"`
HadithNum int `json:"hadith_num"`
HadithDone bool `json:"hadith_done"`
TafsirDone bool `json:"tafsir_done"`
}
func getCheckpointPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "reminder-index-checkpoint.json")
}
func loadCheckpoint() *Checkpoint {
cp := &Checkpoint{}
data, err := os.ReadFile(getCheckpointPath())
if err != nil {
return cp
}
json.Unmarshal(data, cp)
return cp
}
func saveCheckpoint(cp *Checkpoint) {
data, _ := json.MarshalIndent(cp, "", " ")
os.WriteFile(getCheckpointPath(), data, 0644)
}
func clearCheckpoint() {
os.Remove(getCheckpointPath())
}
func indexContent(idx *search.Index, md map[string]string, text string) {
// index the documents
lines := strings.Split(text, "\n")
fmt.Println("Indexing: ", md["source"], md["chapter"], md["verse"])
if err := idx.Store(md, lines...); err != nil {
fmt.Println("Error indexing", err)
}
}
func indexQuran(idx *search.Index, q *quran.Quran) {
cp := loadCheckpoint()
if cp.QuranDone {
fmt.Println("Quran already indexed, skipping")
return
}
fmt.Println("Indexing Quran")
if cp.QuranChapter > 0 {
fmt.Printf("Resuming from chapter %d, verse %d\n", cp.QuranChapter, cp.QuranVerse)
}
for _, chapter := range q.Chapters {
// Skip already indexed chapters
if chapter.Number < cp.QuranChapter {
continue
}
for _, verse := range chapter.Verses {
// Skip already indexed verses in resumed chapter
if chapter.Number == cp.QuranChapter && verse.Number <= cp.QuranVerse {
continue
}
indexContent(idx, map[string]string{
"source": "quran",
"chapter": fmt.Sprintf("%v", chapter.Number),
"verse": fmt.Sprintf("%v", verse.Number),
"name": chapter.Name,
}, verse.Text)
// Save checkpoint every 50 verses
if verse.Number%50 == 0 {
cp.QuranChapter = chapter.Number
cp.QuranVerse = verse.Number
saveCheckpoint(cp)
}
}
}
cp.QuranDone = true
saveCheckpoint(cp)
}
func indexNames(idx *search.Index, n *names.Names) {
cp := loadCheckpoint()
if cp.NamesDone {
fmt.Println("Names already indexed, skipping")
return
}
fmt.Println("Indexing Names")
for _, name := range *n {
indexContent(idx, map[string]string{
"source": "names",
"meaning": name.Meaning,
"english": name.English,
"arabic": name.Arabic,
}, strings.Join([]string{name.Meaning, name.English, name.Description}, " - "))
}
cp.NamesDone = true
saveCheckpoint(cp)
}
func indexTafsir(idx *search.Index, q *quran.Quran) {
cp := loadCheckpoint()
if cp.TafsirDone {
fmt.Println("Tafsir already indexed, skipping")
return
}
fmt.Println("Indexing Tafsir")
for _, comment := range q.Commentary {
indexContent(idx, map[string]string{
"source": "tafsir",
"chapter": fmt.Sprintf("%v", comment.Chapter),
"verse": fmt.Sprintf("%v", comment.Verse),
}, comment.Text)
}
cp.TafsirDone = true
saveCheckpoint(cp)
}
func indexHadith(idx *search.Index, b *hadith.Collection) {
cp := loadCheckpoint()
if cp.HadithDone {
fmt.Println("Hadith already indexed, skipping")
return
}
fmt.Println("Indexing Hadith")
if cp.HadithBook > 0 {
fmt.Printf("Resuming from book %d, hadith %d\n", cp.HadithBook, cp.HadithNum)
}
for bookIdx, book := range b.Books {
// Skip already indexed books
if bookIdx+1 < cp.HadithBook {
continue
}
for _, h := range book.Hadiths {
// Skip already indexed hadiths in resumed book
if bookIdx+1 == cp.HadithBook && h.Number <= cp.HadithNum {
continue
}
indexContent(idx, map[string]string{
"source": "bukhari",
"book": book.Name,
"book_num": fmt.Sprintf("%d", book.Number),
"narrator": h.Narrator,
"number": fmt.Sprintf("%d", h.Number),
}, h.English)
// Save checkpoint every 100 hadiths
if h.Number%100 == 0 {
cp.HadithBook = bookIdx + 1
cp.HadithNum = h.Number
saveCheckpoint(cp)
}
}
}
cp.HadithDone = true
saveCheckpoint(cp)
// Clear checkpoint when all done
clearCheckpoint()
}