address.ts
5.72 KB
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
/**
* 从「出险地点」文本读出 行政区 / 街道 / 道路。
*
* 三条口径,都是从主程序搬过来的(改动必须两边同步,否则工具写出的列值会盖住主程序的解析):
* 1) 行政区按模板位置取第 3 段,段首剥名 + 旧名归并 + 短名兜底,见 splitAddress;
* 2) 街道只在「本区街道词表 + 具体地址段开头」认,不做全文扫描;
* 3) 道路只认地址里明写的路名后缀,不猜、不做坐标吸附,见 roadOfAddress。
* 认不出就留空,交给坐标去查接口 —— 宁缺勿错。
*/
import { DISTRICT_TAIL, ROAD_HEAD, type Vocab, canonicalDistrict } from './vocab.ts';
/** 模板四层「省-市-区-具体地址」;具体地址内部的 '-' 由 joinRest 回接 */
export function splitParts(address: string): string[] {
return address.split('-').map((part) => part.trim()).filter(Boolean);
}
/**
* 区按位置取第 3 段、具体地址取第 4 段起。四处兜底与主程序一致:
* 区名粘了别的字、功能区旧名、只写短名、整行缺省/市前缀(退回扫描)。
*/
export function splitAddress(
parts: readonly string[],
vocab: Vocab,
): { district: string; rest: string } {
/** `allowTail` 只给第 3 段用:位置对上了才敢靠「××区」尾巴认名 */
const peel = (
seg: string,
tailRest: string,
allowTail: boolean,
): { district: string; rest: string } | null => {
if (!seg) return null;
const known = vocab.districtsByLen.find((n) => seg.startsWith(n));
if (known) return { district: known, rest: joinRest(seg.slice(known.length), tailRest) };
const alias = vocab.aliases.find(([from]) => seg.startsWith(from));
if (alias) return { district: alias[1], rest: joinRest(seg.slice(alias[0].length), tailRest) };
const short = vocab.shortNames.find(([from]) => seg.startsWith(from));
if (short) {
const [word, district] = short;
const rest = seg.slice(word.length);
// 「宝安大道」不是「宝安」+「大道」:短名后面紧跟路名尾巴,这一段写的就是路
if (!ROAD_HEAD.test(rest)) return { district, rest: joinRest(rest, tailRest) };
// 例外是「南山街道」:本区街道词表里真有这个名字,认下区但整段原样留给街道识别
const street = (vocab.streetsByDistrict.get(district) ?? [])
.find((s) => s.length > word.length && seg.startsWith(s));
if (street) return { district, rest: joinRest(seg, tailRest) };
}
if (allowTail && DISTRICT_TAIL.test(seg)) {
return { district: canonicalDistrict(seg), rest: tailRest };
}
return null;
};
const head = peel(parts[2] ?? '', parts.slice(3).join('-'), true);
if (head) return head;
for (let i = 0; i < parts.length; i += 1) {
const hit = peel(parts[i], parts.slice(i + 1).join('-'), false);
if (hit) return hit;
}
return { district: '', rest: parts.join('-') };
}
function joinRest(head: string, tail: string): string {
if (!head) return tail;
return tail ? `${head}-${tail}` : head;
}
/** 道路后缀,按长度从长到短排:同一位置先试长的,否则「人民大道」会被切错 */
const ROAD_SUFFIXES = [
'高速公路', '城市快速路', '快速路', '高架桥', '立交桥', '立交', '匝道', '隧道',
'大桥', '辅路', '环路', '大道', '公路', '国道', '省道', '县道', '乡道', '高速',
'街', '路', '巷',
];
/** 主名不许跨过去的字:地址层级、门牌、楼层都不是路名的一部分 */
const NAME_BREAK = '省市区县号栋幢座层室街道路巷';
/** 写着「这条路没有名字」的开头:读得出后缀,读不出是哪条路 */
const NAMELESS = ['无名', '未命名', '未名', '暂无'];
function isNameChar(c: string): boolean {
return /[一-鿿A-Za-z0-9]/.test(c);
}
function suffixAt(text: string, pos: number): number {
for (const s of ROAD_SUFFIXES) {
if (text.startsWith(s, pos)) {
// 「街道」是行政单位不是路段:街后面跟道时整档跳过
if (s === '街' && text.startsWith('道', pos + 1)) continue;
return s.length;
}
}
return 0;
}
/** 取地址里第一个写得完整的道路名,取不到返回空串 */
export function roadOfAddress(detail: string): string {
if (!detail) return '';
const len = detail.length;
for (let pos = 0; pos < len; pos += 1) {
const sl = suffixAt(detail, pos);
if (sl === 0) continue;
const floor = Math.max(0, pos - 10);
let start = pos;
for (let k = pos - 1; k >= floor; k -= 1) {
const c = detail[k];
if (NAME_BREAK.indexOf(c) >= 0 || !isNameChar(c)) break;
start = k;
}
const name = detail.slice(start, pos);
if (name.length === 0 || name.length + sl < 3) {
pos += sl - 1;
continue;
}
const road = name + detail.slice(pos, pos + sl);
if (NAMELESS.some((p) => road.startsWith(p))) {
pos += sl - 1;
continue;
}
return road;
}
return '';
}
export type ParsedAddress = {
district: string;
street: string;
road: string;
/** 剥掉区名之后的具体地址(第 4 段起,'-' 回接) */
rest: string;
/** 再剥掉街道名之后的详细地点 */
detail: string;
};
/** 一行地址的三项解析结果;空串表示「这一档地址里读不出来」 */
export function parseAddress(address: string, vocab: Vocab): ParsedAddress {
const parts = splitParts(address ?? '');
const split = splitAddress(parts, vocab);
const street = (vocab.streetsByDistrict.get(split.district) ?? [])
.find((s) => split.rest.startsWith(s)) ?? '';
const detail = street ? split.rest.slice(street.length) : split.rest;
return {
district: split.district,
street,
road: roadOfAddress(detail),
rest: split.rest,
detail,
};
}