Skip to content

Commit 2dba680

Browse files
committed
fix bug that not saving scoring result
1 parent 15c54ac commit 2dba680

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ go get github.com/wongoo/namer
2424
}
2525
```
2626
说明:
27+
- `last_name`: 姓
28+
- `year`: 年
29+
- `month`: 月
30+
- `day`: 日
31+
- `hour`: 时
32+
- `minute`: 分
2733
- `gender`: 0-男, 1-女
2834
- `min_candidate_score`: 最小候选分数
35+
- `first_name_key_words`: 关键字
2936

3037
3. 最后, 执行命令 `namer -c config.json`, 命令会输出分数排名前10的名字列表:
3138
```bash
@@ -44,4 +51,4 @@ score: 82, names: [王习意 王习义 王习复 王开习 王书望]
4451
## 注意
4552
- 配置可以修改以后再次执行;
4653
- 可以`ctrl+c`中断执行,立即给出已经检测的结果;
47-
- 命令会生成缓存文件 `config.json.score`,请勿删除;
54+
- 命令会在配置文件同目录下生成后缀为`.data`的缓存文件,请勿删除;

namer.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"flag"
99
"fmt"
1010
"io/ioutil"
11-
"log"
1211
"net/http"
1312
"net/url"
1413
"os"
@@ -19,6 +18,15 @@ import (
1918
"time"
2019
)
2120

21+
func fatal(err error) {
22+
fmt.Println(err)
23+
os.Exit(1)
24+
}
25+
26+
func fatalf(format string, err error) {
27+
fmt.Printf(format, err)
28+
}
29+
2230
const (
2331
dataFileSuffix = ".data"
2432
dataCandidateFileSuffix = ".candidate.data"
@@ -57,8 +65,8 @@ type Config struct {
5765
Hour int `json:"hour"`
5866
Minute int `json:"minute"`
5967
Gender int `json:"gender"`
60-
FirstNameKeyWords string `json:"first_name_key_words"`
6168
MinCandidateScore int `json:"min_candidate_score"`
69+
FirstNameKeyWords string `json:"first_name_key_words"`
6270
}
6371

6472
func buildNameTestUrlPrefix() {
@@ -89,7 +97,7 @@ func getNameScore(firstName string) (int, error) {
8997

9098
scoreStr := content[:index]
9199
score, err := strconv.Atoi(scoreStr)
92-
log.Printf("name score: %s = %d", firstName, score)
100+
fmt.Printf("name score: %s%s = %d\n", config.LastName, firstName, score)
93101
return score, err
94102

95103
}
@@ -147,11 +155,11 @@ func statCalculate(score *NameScore, name string) {
147155
func writeScoreData() {
148156
bt, err := json.Marshal(scoreDB)
149157
if err != nil {
150-
log.Fatalf("marshal err: %v", err)
158+
fatalf("marshal err: %v\n", err)
151159
}
152160
err = ioutil.WriteFile(*configFile+dataFileSuffix, bt, 0666)
153161
if err != nil {
154-
log.Fatalf("write score file error: %v", err)
162+
fatalf("write score file error: %v\n", err)
155163
}
156164
}
157165

@@ -164,11 +172,11 @@ func printCandidates() {
164172
func writeCandidateData() {
165173
bt, err := json.Marshal(candidates)
166174
if err != nil {
167-
log.Fatalf("marshal err: %v", err)
175+
fatalf("marshal err: %v\n", err)
168176
}
169177
err = ioutil.WriteFile(*configFile+dataCandidateFileSuffix, bt, 0666)
170178
if err != nil {
171-
log.Fatalf("write candidate file error: %v", err)
179+
fatalf("write candidate file error: %v\n", err)
172180
}
173181
}
174182

@@ -215,7 +223,7 @@ func oneFirstNameScoring(parent *NameScore, firstName rune) error {
215223

216224
func twoFirstNameLoopScoring() error {
217225
for i := 0; i < len(keyWords); i++ {
218-
for j := 1; j < len(keyWords); j++ {
226+
for j := 0; j < len(keyWords); j++ {
219227
firstName1 := keyWords[i]
220228
firstName2 := keyWords[j]
221229

@@ -254,11 +262,14 @@ func addToTree(nameScore *NameScore, score int, firstName ...rune) {
254262
parent.More = make(map[rune]*NameScore, 2)
255263
}
256264
name := firstName[i]
257-
if _, ok := parent.More[name]; ok {
265+
if child, ok := parent.More[name]; ok {
266+
parent = child
258267
continue
259268
}
260269
if i < size-1 {
261-
parent.More[name] = newScore(0)
270+
child := newScore(0)
271+
parent.More[name] = child
272+
parent = child
262273
continue
263274
}
264275
parent.More[name] = newScore(score)
@@ -304,7 +315,7 @@ var (
304315
func loopScoring() {
305316
defer func() {
306317
if err := recover(); err != nil {
307-
log.Printf("namer error: %v", err)
318+
fmt.Printf("namer error: %v\n", err)
308319
}
309320
finishChan <- 1
310321
}()
@@ -328,7 +339,7 @@ func printTop10() {
328339
func startCandidate() {
329340
defer func() {
330341
if err := recover(); err != nil {
331-
log.Printf("candidate error: %v", err)
342+
fmt.Printf("candidate error: %v\n", err)
332343
}
333344
finishChan <- 1
334345
}()
@@ -374,7 +385,7 @@ func startCandidate() {
374385
func main() {
375386
err := parseConfig()
376387
if err != nil {
377-
log.Fatal(err)
388+
fatal(err)
378389
}
379390

380391
go loopScoring()
@@ -384,9 +395,9 @@ func main() {
384395

385396
select {
386397
case sig := <-signalChan:
387-
log.Printf("receive stop signal: %s", sig)
398+
fmt.Printf("receive stop signal: %s\n", sig)
388399
case <-finishChan:
389-
log.Println("scoring finish")
400+
fmt.Println("scoring finish")
390401
}
391402

392403
writeScoreData()
@@ -397,9 +408,9 @@ func main() {
397408

398409
select {
399410
case sig := <-signalChan:
400-
log.Printf("receive stop signal: %s", sig)
411+
fmt.Printf("receive stop signal: %s\n", sig)
401412
case <-finishChan:
402-
log.Println("candidate finish")
413+
fmt.Println("candidate finish")
403414
}
404415

405416
writeScoreData()

0 commit comments

Comments
 (0)