reservation_image_parser.dart
9.68 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/// 从 OCR 文本中提取预订录入字段。
///
/// 针对微信聊天截图做了清洗:过滤顶部状态栏、微信消息时间戳、
/// 常见确认语等噪声,优先从“宴会日期/用餐时间/用餐时段”等标签中取值。
class ReservationImageParser {
const ReservationImageParser._();
static const List<String> _mealWords = <String>[
'早餐', '午餐', '中餐', '午宴', '晚餐', '晚宴', '夜宵', '宵夜',
'早上', '上午', '中午', '下午', '晚上',
];
static ReservationImageParseResult parse(String text) {
final String cleaned = _clean(text);
final String normalized = cleaned
.replaceAll(RegExp(r'\s+'), ' ')
.replaceAll(':', ':')
.trim();
if (normalized.isEmpty) {
return const ReservationImageParseResult.empty();
}
final String name = _extractName(cleaned);
final String sex = _extractSex(normalized, name);
return ReservationImageParseResult(
name: _stripSexSuffix(name, sex),
sex: sex,
phone: _extractPhone(normalized),
party: _extractParty(normalized),
date: _extractDate(normalized),
meal: _extractMeal(normalized),
time: _extractTime(normalized),
table: _extractTable(normalized),
remark: _extractRemark(normalized),
rawText: text,
);
}
static String _clean(String text) {
final List<String> kept = <String>[];
for (final String rawLine in text.split(RegExp(r'[\n\r]+'))) {
final String line = rawLine.trim();
if (line.isEmpty) continue;
if (_isWeChatNoise(line)) continue;
kept.add(line);
}
return kept.join('\n');
}
static bool _isWeChatNoise(String line) {
// 微信消息时间戳,例如:9/5下午06:29
if (RegExp(
r'^\d{1,2}/\d{1,2}\s*(?:上午|下午|晚上|中午)?\s*\d{1,2}[::]\d{2}$',
).hasMatch(line)) {
return true;
}
// 顶部状态栏常见符号
if (line.contains('®') || line.contains('"') || line.contains('“')) {
return true;
}
// 常见的无信息量确认语
const Set<String> acknowledgements = <String>{
'好的收到', '好的', '收到', '嗯', '好', '谢谢',
};
if (acknowledgements.contains(line)) {
return true;
}
return false;
}
static String _extractName(String text) {
final RegExp labelPattern = RegExp(
r'(?:预定贵宾|预订贵宾|预订人|预定人|订位人|联系人|客户姓名|顾客姓名|姓名|称呼)[::\s]*([^\s,,。;;]+)',
);
final Match? match = labelPattern.firstMatch(text);
if (match != null && match.group(1) != null) {
return match.group(1)!.trim();
}
final Match? nameBeforePhone = RegExp(
r'([\u4e00-\u9fa5]{2,4}(?:先生|女士|小姐)?)\s*(?=1[3-9]\d{9})',
).firstMatch(text);
if (nameBeforePhone != null) {
final String candidate = nameBeforePhone.group(1)!.trim();
const Set<String> ignored = <String>{'联系', '电话', '手机', '号码', '微信'};
if (!ignored.contains(candidate)) {
return candidate;
}
}
final Match? leadingName = RegExp(
r'^([\u4e00-\u9fa5]{2,4}(?:先生|女士|小姐)?)(?=\d)',
).firstMatch(text);
if (leadingName != null) {
return leadingName.group(1)!;
}
final RegExp nameLine = RegExp(r'^[\u4e00-\u9fa5]{2,4}(?:先生|女士|小姐)?$');
for (final String line in text.split(RegExp(r'[\n\r]+'))) {
if (nameLine.hasMatch(line.trim())) {
return line.trim();
}
}
return '';
}
static String _extractSex(String text, String name) {
for (final String suffix in const <String>['先生', '女士', '小姐', '公司', '贵宾']) {
if (name.endsWith(suffix)) {
return _normalizeSex(suffix);
}
}
final Match? label = RegExp(
r'(?:称谓|性别|称呼)[::\s]*(先生|女士|公司|贵宾|男|女)',
).firstMatch(text);
if (label != null) {
return _normalizeSex(label.group(1)!);
}
final Match? generic = RegExp(r'(先生|女士)').firstMatch(text);
if (generic != null) {
return generic.group(1)!;
}
return '';
}
static String _normalizeSex(String value) {
if (value == '男') return '先生';
if (value == '女' || value == '小姐') return '女士';
return value;
}
static String _stripSexSuffix(String name, String sex) {
if (name.isEmpty || sex.isEmpty) return name;
final String suffix = sex == '女士' ? '女士' : sex;
if (name.endsWith(suffix)) {
return name.substring(0, name.length - suffix.length);
}
if (sex == '女士' && name.endsWith('小姐')) {
return name.substring(0, name.length - 2);
}
return name;
}
static String _extractPhone(String text) {
final Match? match = RegExp(r'1[3-9]\d{9}').firstMatch(text);
return match?.group(0) ?? '';
}
static String _extractParty(String text) {
final Match? match = RegExp(r'(\d+)\s*个?\s*(?:人|位)').firstMatch(text);
return match?.group(1) ?? '';
}
static String _extractDate(String text) {
final Match? fullDate = RegExp(
r'(\d{4})[年\-/.](\d{1,2})[月\-/.](\d{1,2})日?',
).firstMatch(text);
if (fullDate != null) {
return '${fullDate.group(1)}-${fullDate.group(2)}-${fullDate.group(3)}';
}
final Match? monthDay = RegExp(r'(\d{1,2})月(\d{1,2})日').firstMatch(text);
if (monthDay != null) {
return '${monthDay.group(1)}月${monthDay.group(2)}日';
}
final Match? dayOnly = RegExp(
r'(?:宴会日期|预订日期|预定日期|用餐日期|日期)[::\s]*(\d{1,2}日(?:[((][^))]{1,8}[))])?)',
).firstMatch(text);
if (dayOnly != null) {
return dayOnly.group(1)!;
}
final Match? relative = RegExp(r'(今天|今日|明天|明日|后天)').firstMatch(text);
if (relative != null) {
return relative.group(1)!;
}
return '';
}
static String _extractMeal(String text) {
final String mealPattern = _mealWords.join('|');
final Match? label = RegExp(
r'(?:用餐时段|用餐时间|餐段|时段)[::\s]*(' + mealPattern + r')',
).firstMatch(text);
if (label != null) {
return _normalizeMeal(label.group(1)!);
}
final Match? nearClock = RegExp(
r'(' + mealPattern + r')\s*(\d{1,2}[::]\d{2})',
).firstMatch(text);
if (nearClock != null) {
return _normalizeMeal(nearClock.group(1)!);
}
final Match? generic = RegExp(r'(' + mealPattern + r')').firstMatch(text);
return generic == null ? '' : _normalizeMeal(generic.group(1)!);
}
static String _normalizeMeal(String value) {
if (value.contains('早') || value.contains('上午') || value.contains('早上')) {
return '早餐';
}
if (value.contains('晚') || value.contains('夜') || value.contains('宵') || value.contains('下午')) {
return '晚餐';
}
if (value.contains('午') || value.contains('中')) {
return '午餐';
}
return value;
}
static String _extractTime(String text) {
final Match? label = RegExp(
r'(?:用餐时间|预订时间|预定时间|时间)[::\s]*(?:(?:' +
_mealWords.join('|') +
r')[::\s]*)?(\d{1,2}[::]\d{2})',
).firstMatch(text);
if (label != null) {
return label.group(1)!.replaceAll(':', ':');
}
final Match? clock = RegExp(r'(\d{1,2}):(\d{2})').firstMatch(text);
if (clock != null) {
return clock.group(0)!;
}
final Match? hourPoint = RegExp(r'(\d{1,2})\s*点(?:半)?').firstMatch(text);
if (hourPoint != null) {
final int hour = int.parse(hourPoint.group(1)!);
final String minute = hourPoint.group(0)!.contains('半') ? '30' : '00';
return '${hour.toString().padLeft(2, '0')}:$minute';
}
return '';
}
static String _extractTable(String text) {
final RegExp tableLabel = RegExp(
r'(?:桌位|桌号|桌台|桌|包间)[::\s]*([A-Za-z]?\d{1,4})',
);
final Match? match = tableLabel.firstMatch(text);
if (match != null && match.group(1) != null) {
return match.group(1)!.trim();
}
final Match? suffix =
RegExp(r'([A-Za-z]{1,2}\d{1,4})\s*(?:桌|包间)').firstMatch(text);
if (suffix != null) {
return suffix.group(1)!;
}
final Match? partyTable =
RegExp(r'(\d{1,4})\s*人?\s*(?:包间|包厢)').firstMatch(text);
if (partyTable != null) {
return '${partyTable.group(1)}人包间';
}
return '';
}
static String _extractRemark(String text) {
final RegExp labelPattern = RegExp(
r'(?:备注|要求|特殊说明)[::\s]*(.*?)(?=(?:预订人|联系人|电话|人数|时间|桌位|$))',
);
final Match? match = labelPattern.firstMatch(text);
final String? value = match?.group(1);
return value == null ? '' : value.trim();
}
}
class ReservationImageParseResult {
const ReservationImageParseResult({
required this.name,
required this.sex,
required this.phone,
required this.party,
required this.date,
required this.meal,
required this.time,
required this.table,
required this.remark,
required this.rawText,
});
const ReservationImageParseResult.empty()
: name = '',
sex = '',
phone = '',
party = '',
date = '',
meal = '',
time = '',
table = '',
remark = '',
rawText = '';
final String name;
final String sex;
final String phone;
final String party;
final String date;
final String meal;
final String time;
final String table;
final String remark;
final String rawText;
bool get hasAny =>
name.isNotEmpty ||
sex.isNotEmpty ||
phone.isNotEmpty ||
time.isNotEmpty ||
party.isNotEmpty ||
date.isNotEmpty ||
meal.isNotEmpty ||
table.isNotEmpty;
}