-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
196 lines (169 loc) · 4.29 KB
/
parser.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
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
196
package doushu
import (
_ "embed"
"encoding/csv"
"log"
"strings"
)
//go:embed table.csv
var data string
var p *Parser
func init() {
p = &Parser{}
var frags []string
p.titles, p.headers, frags = splitData(data)
buildIndex(p, frags)
}
type Parser struct {
index []Element
titles []string
headers []string
starts []int
}
func (p *Parser) get(title string, left, top int) Element {
var i int
for ; i < len(p.titles); i++ {
if p.titles[i] == title {
break
}
}
if i == len(p.titles) {
log.Printf("titles not found: %s", title)
return -1
}
start := p.starts[i]
rows := int(p.index[start])
if left > rows || top > int(p.index[start+1]) {
log.Printf("%s out of range: %d %d", title, left, top)
return -1
}
idx := start + 2 + top*rows + left
return p.index[idx]
}
func (p *Parser) batchGet(title string, top int) []Element {
var i int
for ; i < len(p.titles); i++ {
if p.titles[i] == title {
break
}
}
if i >= len(p.titles) {
log.Printf("title not found: %s", title)
return nil
}
start := p.starts[i]
rows := int(p.index[start])
if top > int(p.index[start+1]) {
log.Printf("title %s, out of range: %d", title, top)
return nil
}
idx := start + 2 + top*rows
return p.index[idx : idx+rows]
}
func splitData(data string) (titles, headers, contents []string) {
var title, header, value string
for _, line := range strings.Split(data, "\n") {
line := strings.TrimSpace(line)
if strings.HasPrefix(line, "### ") {
if value != "" {
titles = append(titles, strings.TrimSpace(title))
headers = append(headers, strings.TrimSpace(header))
contents = append(contents, value)
}
title, header, _ = strings.Cut(line[4:], " ")
value = ""
continue
}
if len(line) == 0 || strings.HasPrefix(line, "# ") {
continue
}
if value != "" {
value += "\n" + line
} else {
value = line
}
}
if value != "" {
titles = append(titles, strings.TrimSpace(title))
headers = append(headers, strings.TrimSpace(header))
contents = append(contents, value)
}
return titles, headers, contents
}
func buildIndex(p *Parser, frags []string) {
for i, frag := range frags {
p.starts = append(p.starts, len(p.index))
table := readCSV(frag)
if len(table) < 2 {
continue
}
if p.titles[i] == "诸星在十二宫庙旺利陷表" {
buildLightIndex(p, table)
continue
}
left, top, _ := strings.Cut(p.headers[i], "/")
var multiRows = 1
if strings.HasSuffix(left, "*") {
multiRows = len(strings.Split(table[1][0], ""))
}
var multiCols = 1
if strings.HasSuffix(top, "*") {
multiCols = len(strings.Split(table[0][1], ""))
}
p.index = append(p.index, Element((len(table)-1)*multiRows), Element((len(table[0])-1)*multiCols))
for col := 1; col < len(table[0]); col++ {
for m := 0; m < multiCols; m++ { // 子丑,寅卯,辰巳,午未,申酉,戌亥
for m := 0; m < multiRows; m++ { // 子辰申,丑巳酉,寅午戌,卯未亥
for row := 1; row < len(table); row++ {
name := table[row][col]
code := ValueOf(name)
p.index = append(p.index, code)
}
}
}
}
}
}
func readCSV(fragment string) [][]string {
table, err := csv.NewReader(strings.NewReader(fragment)).ReadAll()
if err != nil {
log.Printf("%s: %s", err, fragment)
}
return table
}
func buildLightIndex(p *Parser, table [][]string) {
// /,庙,旺,得地,利益,平和,不得地,陷
// 子,机府阴相梁破,武同贪巨杀,昌曲,,紫廉,,阳羊火铃
// ==>
// /,子,丑,寅,卯,辰,巳,午,未,申,酉,戌,亥
// 紫微,平和,庙,。。。
p.index = append(p.index, Mingzhu-Ziwei, 12)
lightIndex := make([]Element, (Mingzhu-Ziwei)*12)
for light := 1; light < len(table[0]); light++ {
for zhi := 1; zhi < len(table); zhi++ {
names := table[zhi][light]
for _, name := range strings.Split(names, "") {
var has bool
for star := Ziwei; star <= Shenzhu; star++ {
if strings.Contains(star.String(), name) {
if name == "曲" && star == Wuqu {
continue
}
// if has {
// log.Printf("found duplicate %s: %s", name, star)
// continue
// }
has = true
idx := int(Mingzhu-Ziwei)*(zhi-1) + star.Value()
lightIndex[idx] = Miao.Next(light - 1)
break
}
}
if !has {
log.Printf("not found %s", name)
}
}
}
}
p.index = append(p.index, lightIndex...)
}