-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_han.go
73 lines (63 loc) · 1.69 KB
/
common_han.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
// Copyright 2018 Sean.ZH
package libcode
import (
"errors"
"log"
"unicode/utf8"
dio "github.com/dilfish/tools/io"
)
// CommonHanEncoder is the least priority encoder for libcode
// it just map a word's unicode point to a int32 value
type CommonHanEncoder struct {
commonHan map[rune]int
revCommonHan map[int]rune
idx int
}
var errBadHanFile = errors.New("bad common han file")
func (chen *CommonHanEncoder) _readCommon(w string) error {
r, _ := utf8.DecodeRune([]byte(w))
c := utf8.RuneCount([]byte(w))
if c != 1 {
log.Println("read common error:", c)
return errBadHanFile
}
chen.commonHan[r] = chen.idx
chen.revCommonHan[chen.idx] = r
chen.idx++
return nil
}
func (chen *CommonHanEncoder) readCommon(fn string) error {
chen.commonHan = make(map[rune]int)
chen.revCommonHan = make(map[int]rune)
return dio.ReadLine(fn, chen._readCommon)
}
// EncodeCommonHan encodes a word to int32 as it's unicode point
// using the comon 2500 Chinese characters
func (chen *CommonHanEncoder) EncodeCommonHan(code rune) int32 {
idx, ok := chen.commonHan[code]
if ok == false {
log.Println("not a valid common han:", code)
return BadCode
}
return int32(idx)
}
// DecodeCommonHan read a int32 as unicode point and map it
// to a char
func (chen *CommonHanEncoder) DecodeCommonHan(off int32) rune {
han, ok := chen.revCommonHan[int(off)]
if ok == false {
log.Println("not a valid unicode:", off)
return BadRune
}
return rune(han)
}
// NewCommonHan provide a new service
func NewCommonHan(fn string) (*CommonHanEncoder, error) {
chen := new(CommonHanEncoder)
err := chen.readCommon(fn)
if err != nil {
log.Println("read common error:", err)
return nil, err
}
return chen, nil
}