-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
150 lines (139 loc) · 3.95 KB
/
parse.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
package address_parse
import (
"encoding/json"
"regexp"
"strings"
)
// 数据
type AddressList struct {
ID int `json:"id"`
Name string `json:"name"`
ZipCode string `json:"zipcode"`
Children []AddressList `json:"child"`
}
func init() {
shippingAddress = make([]AddressList, 0)
loadData()
}
func loadData() {
err := json.Unmarshal([]byte(data), &shippingAddress)
if err != nil {
panic(err)
}
// 记录简写
for _, item := range shippingAddress {
name := item.Name
for _, s := range provinceKey {
name = strings.ReplaceAll(name, s, "")
}
provinces[item.Name] = name
for _, subItem := range item.Children {
name = subItem.Name
for _, s := range cityKey {
name = strings.ReplaceAll(name, s, "")
}
cities[subItem.Name] = name
}
}
}
type Address struct {
Name string `json:"name"`
Mobile string `json:"mobile"`
Phone string `json:"phone"`
//省
Province string `json:"province"`
City string `json:"city"`
County string `json:"county"`
Addr string `json:"addr"`
ZipCode string `json:"zip_code"`
}
var (
shippingAddress []AddressList
search = []string{"地址", "收货地址", "收货人", "收件人", "收货", "邮编", "电话", ":", ":", ";", ";", ",", ",", "。", " "}
provinceKey = []string{"特别行政区", "古自治区", "维吾尔自治区", "壮族自治区", "回族自治区", "自治区", "省省直辖", "省", "市"}
// 存储省份简写
provinces = make(map[string]string)
cityKey = []string{"布依族苗族自治州", "苗族侗族自治州", "自治州", "州", "市", "县"}
// 存储区简写
cities = make(map[string]string)
)
func Parse(address string) Address {
p := Address{}
for _, s := range search {
address = strings.ReplaceAll(address, s, " ")
}
//整理电话格式
reg := regexp.MustCompile(`(\d{3})-(\d{4})-(\d{4})`)
address = reg.ReplaceAllString(address, `${1}${2}${3}`)
reg = regexp.MustCompile(`(\d{3}) (\d{4}) (\d{4})`)
address = reg.ReplaceAllString(address, `${1}${2}${3}`)
// 获取手机号
reg = regexp.MustCompile(`(86-[1][0-9]{10})|(86[1][0-9]{10})|([1][0-9]{10})`)
as := reg.FindAllString(address, 1)
if len(as) == 1 {
p.Mobile = as[0]
address = strings.ReplaceAll(address, as[0], " ")
}
// 获取电话号码
reg = regexp.MustCompile(`(([0-9]{3,4}-)[0-9]{7,8})|([0-9]{12})|([0-9]{11})|([0-9]{10})|([0-9]{9})|([0-9]{8})|([0-9]{7})`)
as = reg.FindAllString(address, 1)
if len(as) == 1 {
p.Phone = as[0]
address = strings.ReplaceAll(address, as[0], " ")
}
//多个空格replace为一个
reg = regexp.MustCompile(` {2,}`)
address=strings.Trim(address," ")
//address = reg.ReplaceAllString(address, " ")
DetailParseForward(address, &p)
return p
}
// 正向解析
func DetailParseForward(address string, p *Address) {
AddressRead(shippingAddress, p, address, 0, make([]string, 0))
}
func AddressRead(al []AddressList, p *Address, address string, index int, repStr []string) {
for _, item := range al {
name := item.Name
if index == 0 {
name = provinces[item.Name]
} else if index == 1 {
name = cities[item.Name]
} else {
for _, k := range cityKey {
name = strings.ReplaceAll(name, k, "")
}
}
if pindex := strings.Index(address, name); pindex > -1 {
switch index {
// 省
case 0:
p.Province = item.Name
if pindex == 0 {
as := strings.Split(address, " ")
p.Name = as[len(as)-1]
} else {
p.Name = strings.Split(address, " ")[0]
}
address = strings.ReplaceAll(address, p.Name, "")
repStr = append(repStr, item.Name, name)
//市
case 1:
p.City = item.Name
for _, k := range repStr {
address = strings.ReplaceAll(address, k, "")
}
//区
case 2:
p.County = item.Name
p.ZipCode = item.ZipCode
address = strings.ReplaceAll(address, item.Name, "")
address = strings.ReplaceAll(address, name, "")
p.Addr = address
return
}
AddressRead(item.Children, p, address, index+1, repStr)
break
}
}
}