-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_engine.go
136 lines (127 loc) · 3.76 KB
/
search_engine.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
package main
import (
"os"
"fmt"
"bytes"
"strconv"
"strings"
"net/http"
"path/filepath"
"github.com/grokify/html-strip-tags-go"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/siongui/gojianfan"
)
var dir = "/zh_sutta"
var public_ctn = make(chan string)
func searcher(stop chan string, private_ctn chan string, dir string, word string) {
select {
case <-stop:
default:
fs := assets
go walk_paths_recursive(stop, private_ctn, fs, dir, word)
}
}
func walk_paths_recursive(stop chan string, private_ctn chan string, fs http.FileSystem, dir string, word string) (err error) {
select {
case <-stop:
default:
visit := func(path string, info os.FileInfo, err error) error {
if info.IsDir() && path != dir {
go walk_paths_recursive(stop, private_ctn, fs, path, word)
return filepath.SkipDir
}
if info == nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
go parse_file(stop, private_ctn, fs, path, word)
return err
}
vfsutil.Walk(fs, dir, visit)
}
return err
}
func parse_file(stop chan string, private_ctn chan string, fs http.FileSystem, path string, word string) error {
select {
case <-stop:
default:
data, err := vfsutil.ReadFile(fs, path)
if err != nil {
return err
}
if !bytes.Contains(data, []byte(word)) {
return nil
} else {
switch filepath.Ext(path) {
case ".htm":
parse_zcj(stop, private_ctn, path, data, word)
}
}
}
return nil
}
func parse_zcj(stop chan string, private_ctn chan string, path string, data []byte, word string) {
select {
case <-stop:
default:
content_str := string(data)
sen_list := strings.Split(content_str, "<br>")
for _, n := range sen_list {
// get raw number extracted from pali lines
word_list := strings.Split(n, ".")
no := strings.Join(strings.Fields(word_list[0]), "") // remove space
if _, err := strconv.Atoi(no); err == nil { // check if number
no_pl := " " + no + "."
no_cn := "<!" + no + ">"
content_str = strings.Replace(content_str, no_pl, "^-^"+ no +".pl.", -1)
content_str = strings.Replace(content_str, no_cn, "^-^"+ no +".cn.", -1)
}
}
no := ""
nu_para_list := strings.Split(content_str, "^-^")
// 454.cn ...
for _, p := range nu_para_list {
// find hit
if strings.Contains(strip.StripTags(p), word) {
word_list := strings.Split(p, ".")
// find num
no = strings.Join(strings.Fields(word_list[0]), "") // remove space
//check if number
if _, err := strconv.Atoi(no); err != nil {
//println("sentence prefix is not number, continue \n")
continue
}
// find sutta
sutta_no := strings.Split(filepath.Base(path), ".")[0] + "."
// find para
for _, n := range nu_para_list {
select {
case <-stop:
default:
word_list_n := strings.Split(n, ".")
// find num
no_n := strings.Join(strings.Fields(word_list_n[0]), "") // remove space
if no_n == no && n != p {
//println(no_n)
hit_lg := word_list[1]
hit_para := sutta_no + strings.Join(strings.Fields(strip.StripTags(p)), " ") //remove duplicate space
//println(hit_para)
hit_para = strings.Replace(hit_para, word, "<item class='word'>"+word+"</item>", -1)
hit_para = strings.Replace(hit_para, `"`, `'`, -1)
parallel_lg := word_list_n[1]
parallel_para := sutta_no + strings.Join(strings.Fields(strip.StripTags(n)), " ") //remove duplicate space
parallel_para = strings.Replace(parallel_para, `"`, `'`, -1)
json := fmt.Sprintf(`{"type":0, "word":"%s", "lg":"%s", "%s":"%s", "%s":"%s"}`,
gojianfan.T2S(word), hit_lg, hit_lg, hit_para, parallel_lg, parallel_para)
private_ctn <- json
//println("put2ch --" + json[:100])
break //now just for two kind language ok
}
}
}
}
}
}
}