-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
154 lines (127 loc) · 2.97 KB
/
main.go
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
package main
import (
"bufio"
"database/sql"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/harunalfat/quran-clean-fts/specialcase"
_ "github.com/mattn/go-sqlite3"
)
const (
dbPath = "./data/lafzi.sqlite"
fathah = "َ"
kasrah = "ِ"
dhamah = "ُ"
fathahtain = "ً"
kasrahtain = "ٍ"
dhamahtain = "ٌ"
syaddah = "ّ"
sukun = "ْ"
)
func writeSQLiteFile() {
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
d1 := []byte("")
err := ioutil.WriteFile(dbPath, d1, 0644)
checkErr(err)
println("DB file created")
}
}
func setupDB() *sql.DB {
db, err := sql.Open("sqlite3", dbPath)
checkErr(err)
return db
}
func prepareIndex(db *sql.DB) {
_, err := db.Exec("DROP TABLE IF EXISTS quran_text")
checkErr(err)
query := "CREATE VIRTUAL TABLE quran_text USING fts4(text_reverse)"
_, err = db.Exec(query)
checkErr(err)
println("table 'quran_text' created")
}
func removeDiacritic(in string) string {
replacer := strings.NewReplacer(syaddah, "", sukun, "", fathah, "", fathahtain, "",
kasrah, "", kasrahtain, "", dhamah, "", dhamahtain, "")
return replacer.Replace(in)
}
func insertQuranVerse(db *sql.DB) {
file, err := os.Open("./data/quran-simple-clean.txt")
checkErr(err)
file2, err := os.Open("./data/quran-uthmani.txt")
checkErr(err)
defer file.Close()
defer file2.Close()
scanner := bufio.NewScanner(file)
scanner2 := bufio.NewScanner(file2)
i := 1
replacer := strings.NewReplacer("أ", "ا", "إ", "ا", "ة", "ه", "آ", "ا")
for scanner.Scan() {
scanner2.Scan()
line := scanner.Text()
line2 := scanner2.Text()
split := strings.Split(line, "|")
surahNum := split[0]
ayatNum := split[1]
ayat := split[2]
split2 := strings.Split(line2, "|")
ayatFull := split2[2]
if ayatNum == "1" && surahNum != "1" {
ayat = ayat[42:len(ayat)]
}
ayat = replacer.Replace(ayat)
sn, err := strconv.Atoi(surahNum)
checkErr(err)
an, err := strconv.Atoi(ayatNum)
checkErr(err)
if specialcase.IsSpecialCase(sn, an) {
ayat = specialcase.ReplaceSpecialCase(ayat, an, sn)
}
ayatReverse := reverse(ayat)
query := fmt.Sprintf(`
INSERT INTO quran_text VALUES('%s')
`, ayatReverse)
queryUpdate := fmt.Sprintf(`
UPDATE ayat_quran SET arabic_text_length = %d, short_arabic_text_length = %d WHERE _id = %d
`, len(ayatFull), len(ayat), i)
_, err = db.Exec(query)
checkErr(err)
_, err = db.Exec(queryUpdate)
checkErr(err)
i++
if i > 6236 {
break
}
}
println("Done indexing quran.")
/*
_, err = db.Exec(`
UPDATE
ayat_quran
SET short_arabic_text_length = (
SELECT LENGTH(text_reverse) FROM quran_text WHERE docid = _id
)
`)
checkErr(err)
*/
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func reverse(s string) string {
chars := []rune(s)
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
chars[i], chars[j] = chars[j], chars[i]
}
return string(chars)
}
func main() {
writeSQLiteFile()
db := setupDB()
prepareIndex(db)
insertQuranVerse(db)
}