-
Notifications
You must be signed in to change notification settings - Fork 4
/
rule.go
69 lines (57 loc) · 1.37 KB
/
rule.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
package parser
import (
"fmt"
"regexp"
)
const (
Rule = Algorithm("rule")
separateConditionCount = 2
)
func NewRuleBaseParser() RuleBaseParser {
re := regexp.MustCompile(`\p{Han}+`)
return RuleBaseParser{
re: re,
}
}
type RuleBaseParser struct {
re *regexp.Regexp
}
// Parse referer: https://github.com/rskmoi/namedivider-python/blob/master/namedivider/name_divider.py#L238
func (p RuleBaseParser) Parse(fullname FullName, separator Separator) (DividedName, error) {
length := fullname.Length()
if length == minNameLength {
l, f, err := fullname.Split(1)
if err != nil {
return DividedName{}, fmt.Errorf("rule parser error: %w", err)
}
return DividedName{
FirstName: f,
LastName: l,
Separator: separator,
Score: 1,
Algorithm: Rule,
}, nil
}
isKanjiList := make([]bool, length)
for i, c := range fullname.Slice() {
isKanji := p.re.MatchString(string(c))
isKanjiList[i] = isKanji
if i >= separateConditionCount {
if isKanjiList[0] != isKanji && isKanjiList[i-1] == isKanji {
l, f, err := fullname.Split(i - 1)
if err != nil {
return DividedName{}, fmt.Errorf("rule parser error: %w", err)
}
return DividedName{
FirstName: f,
LastName: l,
Separator: separator,
Score: 1,
Algorithm: Rule,
}, nil
}
}
}
//nolint:exhaustivestruct
return DividedName{}, nil
}