taiwan-logistics-skill 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -132
- package/assets/taiwan-logistics/CLAUDE.md +147 -137
- package/assets/taiwan-logistics/EXAMPLES.md +3258 -3258
- package/assets/taiwan-logistics/README.md +132 -132
- package/assets/taiwan-logistics/SKILL.md +845 -845
- package/assets/taiwan-logistics/data/field-mappings.csv +23 -27
- package/assets/taiwan-logistics/data/logistics-types.csv +29 -25
- package/assets/taiwan-logistics/data/operations.csv +32 -32
- package/assets/taiwan-logistics/data/providers.csv +20 -15
- package/assets/taiwan-logistics/data/status-codes.csv +89 -35
- package/assets/taiwan-logistics/examples/ecpay-logistics-cvs-example.py +175 -175
- package/assets/taiwan-logistics/examples/newebpay-logistics-cvs-example.py +524 -524
- package/assets/taiwan-logistics/examples/payuni-logistics-cvs-example.py +605 -605
- package/assets/taiwan-logistics/examples/store-map-integration-example.html +573 -573
- package/assets/taiwan-logistics/references/NEWEBPAY_LOGISTICS_REFERENCE.md +774 -774
- package/assets/taiwan-logistics/references/carrier-direct-access.md +134 -0
- package/assets/taiwan-logistics/references/ecpay-logistics-api.md +536 -536
- package/assets/taiwan-logistics/references/ezship-logistics-api.md +103 -0
- package/assets/taiwan-logistics/references/hct-logistics-api.md +1102 -1102
- package/assets/taiwan-logistics/references/lalamove-logistics-api.md +138 -0
- package/assets/taiwan-logistics/references/paynow-logistics-api.md +1670 -1670
- package/assets/taiwan-logistics/references/payuni-logistics-api.md +712 -712
- package/assets/taiwan-logistics/references/pchomepay-logistics-api.md +1154 -1154
- package/assets/taiwan-logistics/references/smilepay-logistics-api.md +1332 -1332
- package/assets/taiwan-logistics/scripts/core.py +276 -276
- package/assets/taiwan-logistics/scripts/generate-logistics-service.py +1248 -1248
- package/assets/taiwan-logistics/scripts/recommend.py +370 -364
- package/assets/taiwan-logistics/scripts/search.py +127 -127
- package/assets/taiwan-logistics/scripts/test_logistics.py +300 -300
- package/assets/templates/base/quick-reference.md +74 -74
- package/assets/templates/base/skill-content.md +845 -845
- package/assets/templates/platforms/agent.json +26 -26
- package/assets/templates/platforms/claude.json +26 -26
- package/assets/templates/platforms/codebuddy.json +25 -25
- package/assets/templates/platforms/codex.json +26 -26
- package/assets/templates/platforms/continue.json +25 -25
- package/assets/templates/platforms/copilot.json +25 -25
- package/assets/templates/platforms/cursor.json +26 -26
- package/assets/templates/platforms/gemini.json +25 -25
- package/assets/templates/platforms/kiro.json +25 -25
- package/assets/templates/platforms/opencode.json +25 -25
- package/assets/templates/platforms/qoder.json +25 -25
- package/assets/templates/platforms/roocode.json +25 -25
- package/assets/templates/platforms/trae.json +25 -25
- package/assets/templates/platforms/windsurf.json +25 -25
- package/package.json +65 -65
|
@@ -1,364 +1,370 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
Taiwan Logistics Skill - 物流服務商推薦系統
|
|
4
|
-
|
|
5
|
-
基於 BM25 算法的智能推薦引擎
|
|
6
|
-
根據使用者需求自動推薦最適合的物流服務商
|
|
7
|
-
|
|
8
|
-
使用方式:
|
|
9
|
-
python recommend.py "超商取貨 冷凍配送 高交易量"
|
|
10
|
-
python recommend.py "7-11 B2C 穩定" --format json
|
|
11
|
-
python recommend.py "生鮮電商 溫控" --format simple
|
|
12
|
-
|
|
13
|
-
作者: Taiwan E-Commerce Toolkit
|
|
14
|
-
版本: 1.0.0
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
import sys
|
|
18
|
-
import csv
|
|
19
|
-
import json
|
|
20
|
-
import math
|
|
21
|
-
import
|
|
22
|
-
|
|
23
|
-
from
|
|
24
|
-
from
|
|
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
|
-
def
|
|
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
|
-
lines = []
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
lines.append("")
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if result.
|
|
301
|
-
lines.append("
|
|
302
|
-
for
|
|
303
|
-
lines.append(f" • {
|
|
304
|
-
lines.append("")
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
#
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Taiwan Logistics Skill - 物流服務商推薦系統
|
|
4
|
+
|
|
5
|
+
基於 BM25 算法的智能推薦引擎
|
|
6
|
+
根據使用者需求自動推薦最適合的物流服務商
|
|
7
|
+
|
|
8
|
+
使用方式:
|
|
9
|
+
python recommend.py "超商取貨 冷凍配送 高交易量"
|
|
10
|
+
python recommend.py "7-11 B2C 穩定" --format json
|
|
11
|
+
python recommend.py "生鮮電商 溫控" --format simple
|
|
12
|
+
|
|
13
|
+
作者: Taiwan E-Commerce Toolkit
|
|
14
|
+
版本: 1.0.0
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import sys
|
|
18
|
+
import csv
|
|
19
|
+
import json
|
|
20
|
+
import math
|
|
21
|
+
import re
|
|
22
|
+
import argparse
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from typing import List, Dict, Tuple
|
|
25
|
+
from dataclasses import dataclass
|
|
26
|
+
|
|
27
|
+
sys.path.insert(0, str(Path(__file__).parent))
|
|
28
|
+
from core import tokenize as core_tokenize
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class LogisticsProvider:
|
|
33
|
+
"""物流服務商資料"""
|
|
34
|
+
provider: str
|
|
35
|
+
display_name: str
|
|
36
|
+
auth_method: str
|
|
37
|
+
encryption: str
|
|
38
|
+
test_url: str
|
|
39
|
+
prod_url: str
|
|
40
|
+
content_type: str
|
|
41
|
+
features: List[str]
|
|
42
|
+
market_share: str
|
|
43
|
+
api_style: str
|
|
44
|
+
logistics_types: List[str]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class RecommendResult:
|
|
49
|
+
"""推薦結果"""
|
|
50
|
+
provider: str
|
|
51
|
+
display_name: str
|
|
52
|
+
score: float
|
|
53
|
+
match_reasons: List[str]
|
|
54
|
+
features: List[str]
|
|
55
|
+
warnings: List[str]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class BM25:
|
|
59
|
+
"""BM25 搜尋算法"""
|
|
60
|
+
|
|
61
|
+
def __init__(self, k1: float = 1.5, b: float = 0.75):
|
|
62
|
+
self.k1 = k1
|
|
63
|
+
self.b = b
|
|
64
|
+
|
|
65
|
+
def score(self, query_terms: List[str], doc_terms: List[str],
|
|
66
|
+
avg_doc_len: float, doc_len: int, total_docs: int,
|
|
67
|
+
doc_freq: Dict[str, int]) -> float:
|
|
68
|
+
"""計算 BM25 分數"""
|
|
69
|
+
score = 0.0
|
|
70
|
+
|
|
71
|
+
for term in query_terms:
|
|
72
|
+
if term not in doc_terms:
|
|
73
|
+
continue
|
|
74
|
+
|
|
75
|
+
tf = doc_terms.count(term)
|
|
76
|
+
df = doc_freq.get(term, 0)
|
|
77
|
+
|
|
78
|
+
if df == 0:
|
|
79
|
+
continue
|
|
80
|
+
|
|
81
|
+
idf = math.log((total_docs - df + 0.5) / (df + 0.5) + 1)
|
|
82
|
+
score += idf * (tf * (self.k1 + 1)) / \
|
|
83
|
+
(tf + self.k1 * (1 - self.b + self.b * (doc_len / avg_doc_len)))
|
|
84
|
+
|
|
85
|
+
return score
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class LogisticsRecommender:
|
|
89
|
+
"""物流服務商推薦引擎"""
|
|
90
|
+
|
|
91
|
+
# 關鍵字權重
|
|
92
|
+
KEYWORD_WEIGHTS = {
|
|
93
|
+
# ECPay 綠界
|
|
94
|
+
'ecpay': 3.0, '綠界': 3.0, '穩定': 2.5, '市佔': 2.0, '高交易量': 2.0,
|
|
95
|
+
'電商': 1.8, '文檔': 2.0, 'sdk': 2.0, '完整': 1.5, '超商': 1.5,
|
|
96
|
+
'7-11': 1.5, '全家': 1.5, '萊爾富': 1.5, 'ok': 1.5, '黑貓': 1.5,
|
|
97
|
+
'宅配': 1.5, 'c2c': 1.5, 'b2c': 1.5, '取貨付款': 1.5,
|
|
98
|
+
|
|
99
|
+
# NewebPay 藍新
|
|
100
|
+
'newebpay': 3.0, '藍新': 3.0, '多元': 2.0, '會員': 1.8, '整合': 2.0,
|
|
101
|
+
'記憶': 1.5, '便利': 1.5, '簡單': 1.8, '快速': 1.8, '小型': 1.5,
|
|
102
|
+
|
|
103
|
+
# PAYUNi 統一
|
|
104
|
+
'payuni': 3.0, '統一': 3.0, '溫控': 3.5, '冷凍': 3.0, '冷藏': 3.0,
|
|
105
|
+
'生鮮': 3.0, 'api': 2.0, 'json': 2.0, 'restful': 2.0, '新創': 1.8,
|
|
106
|
+
'現代': 1.5, '設計': 1.5,
|
|
107
|
+
|
|
108
|
+
# 通用關鍵字
|
|
109
|
+
'物流': 1.0, '配送': 1.0, '取貨': 1.0, '超商': 1.2, '宅配': 1.2,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
# 反模式 (不建議使用的場景)
|
|
113
|
+
ANTI_PATTERNS = {
|
|
114
|
+
'ecpay': ['無技術資源', '極簡需求', 'api優先', '現代化'],
|
|
115
|
+
'newebpay': ['簡單api', '單一支付', '極簡', '最小化'],
|
|
116
|
+
'payuni': ['大型專案', '完整文檔', '傳統', 'php']
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
def __init__(self, data_dir: Path = None):
|
|
120
|
+
"""初始化推薦引擎"""
|
|
121
|
+
if data_dir is None:
|
|
122
|
+
data_dir = Path(__file__).parent.parent / 'data'
|
|
123
|
+
|
|
124
|
+
self.data_dir = data_dir
|
|
125
|
+
self.providers: List[LogisticsProvider] = []
|
|
126
|
+
self.bm25 = BM25(k1=1.5, b=0.75)
|
|
127
|
+
self.load_data()
|
|
128
|
+
|
|
129
|
+
def load_data(self):
|
|
130
|
+
"""載入服務商資料"""
|
|
131
|
+
providers_file = self.data_dir / 'providers.csv'
|
|
132
|
+
|
|
133
|
+
if not providers_file.exists():
|
|
134
|
+
raise FileNotFoundError(f"找不到資料檔案: {providers_file}")
|
|
135
|
+
|
|
136
|
+
with open(providers_file, 'r', encoding='utf-8') as f:
|
|
137
|
+
reader = csv.DictReader(f)
|
|
138
|
+
for row in reader:
|
|
139
|
+
# providers.csv 的實際欄位為 name_zh/type/coverage/doc_access…
|
|
140
|
+
# 這裡做寬鬆對應,缺欄位時以合理預設值代替,避免 KeyError
|
|
141
|
+
features = [s for s in re.split(r'\s*[|,]\s*', row.get('features', '')) if s]
|
|
142
|
+
self.providers.append(
|
|
143
|
+
LogisticsProvider(
|
|
144
|
+
provider=row['provider'],
|
|
145
|
+
display_name=row.get('display_name') or row.get('name_zh') or row['provider'],
|
|
146
|
+
auth_method=row.get('auth_method', ''),
|
|
147
|
+
encryption=row.get('encryption', ''),
|
|
148
|
+
test_url=row.get('test_url', ''),
|
|
149
|
+
prod_url=row.get('prod_url', ''),
|
|
150
|
+
content_type=row.get('content_type', ''),
|
|
151
|
+
features=features,
|
|
152
|
+
market_share=row.get('market_share') or row.get('coverage', ''),
|
|
153
|
+
api_style=row.get('api_style') or row.get('type', ''),
|
|
154
|
+
logistics_types=features,
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
def tokenize(self, text: str) -> List[str]:
|
|
159
|
+
"""分詞(沿用 core.py 的中英混合分詞,避免中文查詢整段當成單一 token)"""
|
|
160
|
+
return core_tokenize(text)
|
|
161
|
+
|
|
162
|
+
def build_document(self, provider: LogisticsProvider) -> str:
|
|
163
|
+
"""建立服務商文件 (用於搜尋)"""
|
|
164
|
+
parts = [
|
|
165
|
+
provider.provider,
|
|
166
|
+
provider.display_name,
|
|
167
|
+
provider.auth_method,
|
|
168
|
+
provider.api_style,
|
|
169
|
+
provider.market_share,
|
|
170
|
+
' '.join(provider.features),
|
|
171
|
+
' '.join(provider.logistics_types)
|
|
172
|
+
]
|
|
173
|
+
return ' '.join(parts)
|
|
174
|
+
|
|
175
|
+
def calculate_weighted_score(self, query: str, provider: LogisticsProvider) -> Tuple[float, List[str]]:
|
|
176
|
+
"""計算加權分數"""
|
|
177
|
+
query_terms = self.tokenize(query)
|
|
178
|
+
doc_text = self.build_document(provider)
|
|
179
|
+
doc_terms = self.tokenize(doc_text)
|
|
180
|
+
|
|
181
|
+
# 計算 BM25 基礎分數
|
|
182
|
+
total_docs = len(self.providers)
|
|
183
|
+
avg_doc_len = sum(len(self.tokenize(self.build_document(p))) for p in self.providers) / total_docs
|
|
184
|
+
doc_len = len(doc_terms)
|
|
185
|
+
|
|
186
|
+
doc_freq = {}
|
|
187
|
+
for term in set(query_terms):
|
|
188
|
+
doc_freq[term] = sum(1 for p in self.providers if term in self.tokenize(self.build_document(p)))
|
|
189
|
+
|
|
190
|
+
base_score = self.bm25.score(query_terms, doc_terms, avg_doc_len, doc_len, total_docs, doc_freq)
|
|
191
|
+
|
|
192
|
+
# 應用關鍵字權重
|
|
193
|
+
weighted_score = base_score
|
|
194
|
+
match_reasons = []
|
|
195
|
+
|
|
196
|
+
for term in query_terms:
|
|
197
|
+
if term in doc_terms:
|
|
198
|
+
weight = self.KEYWORD_WEIGHTS.get(term, 1.0)
|
|
199
|
+
if weight > 1.0:
|
|
200
|
+
weighted_score += base_score * (weight - 1.0) * 0.1
|
|
201
|
+
match_reasons.append(f"關鍵字匹配: {term} (權重 {weight:.1f})")
|
|
202
|
+
|
|
203
|
+
return weighted_score, match_reasons
|
|
204
|
+
|
|
205
|
+
def check_anti_patterns(self, query: str, provider_key: str) -> List[str]:
|
|
206
|
+
"""檢查反模式"""
|
|
207
|
+
warnings = []
|
|
208
|
+
query_lower = query.lower()
|
|
209
|
+
|
|
210
|
+
if provider_key in self.ANTI_PATTERNS:
|
|
211
|
+
for pattern in self.ANTI_PATTERNS[provider_key]:
|
|
212
|
+
if pattern in query_lower:
|
|
213
|
+
warnings.append(f"注意: {provider_key.upper()} 可能不適合「{pattern}」場景")
|
|
214
|
+
|
|
215
|
+
return warnings
|
|
216
|
+
|
|
217
|
+
def recommend(self, query: str, top_k: int = 3) -> List[RecommendResult]:
|
|
218
|
+
"""
|
|
219
|
+
推薦物流服務商
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
query: 需求描述
|
|
223
|
+
top_k: 回傳前 K 個推薦結果
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
推薦結果清單
|
|
227
|
+
"""
|
|
228
|
+
results = []
|
|
229
|
+
|
|
230
|
+
for provider in self.providers:
|
|
231
|
+
score, match_reasons = self.calculate_weighted_score(query, provider)
|
|
232
|
+
warnings = self.check_anti_patterns(query, provider.provider.lower())
|
|
233
|
+
|
|
234
|
+
results.append(
|
|
235
|
+
RecommendResult(
|
|
236
|
+
provider=provider.provider,
|
|
237
|
+
display_name=provider.display_name,
|
|
238
|
+
score=score,
|
|
239
|
+
match_reasons=match_reasons,
|
|
240
|
+
features=provider.features,
|
|
241
|
+
warnings=warnings
|
|
242
|
+
)
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
# 依分數排序
|
|
246
|
+
results.sort(key=lambda x: x.score, reverse=True)
|
|
247
|
+
|
|
248
|
+
return results[:top_k]
|
|
249
|
+
|
|
250
|
+
def format_output(self, results: List[RecommendResult], format_type: str = 'detailed') -> str:
|
|
251
|
+
"""
|
|
252
|
+
格式化輸出
|
|
253
|
+
|
|
254
|
+
Args:
|
|
255
|
+
results: 推薦結果
|
|
256
|
+
format_type: 輸出格式 ('detailed', 'simple', 'json')
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
格式化的推薦結果
|
|
260
|
+
"""
|
|
261
|
+
if format_type == 'json':
|
|
262
|
+
return json.dumps(
|
|
263
|
+
[
|
|
264
|
+
{
|
|
265
|
+
'provider': r.provider,
|
|
266
|
+
'display_name': r.display_name,
|
|
267
|
+
'score': round(r.score, 2),
|
|
268
|
+
'match_reasons': r.match_reasons,
|
|
269
|
+
'features': r.features,
|
|
270
|
+
'warnings': r.warnings
|
|
271
|
+
}
|
|
272
|
+
for r in results
|
|
273
|
+
],
|
|
274
|
+
ensure_ascii=False,
|
|
275
|
+
indent=2
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
elif format_type == 'simple':
|
|
279
|
+
lines = []
|
|
280
|
+
for i, result in enumerate(results, 1):
|
|
281
|
+
lines.append(f"{i}. {result.display_name} ({result.provider.upper()}) - 分數: {result.score:.2f}")
|
|
282
|
+
return '\n'.join(lines)
|
|
283
|
+
|
|
284
|
+
else: # detailed
|
|
285
|
+
lines = []
|
|
286
|
+
lines.append("=" * 70)
|
|
287
|
+
lines.append("物流服務商推薦結果")
|
|
288
|
+
lines.append("=" * 70)
|
|
289
|
+
lines.append("")
|
|
290
|
+
|
|
291
|
+
for i, result in enumerate(results, 1):
|
|
292
|
+
lines.append(f"推薦 #{i}: {result.display_name} ({result.provider.upper()})")
|
|
293
|
+
lines.append(f" 匹配分數: {result.score:.2f}")
|
|
294
|
+
lines.append("")
|
|
295
|
+
lines.append(" 特色功能:")
|
|
296
|
+
for feature in result.features:
|
|
297
|
+
lines.append(f" • {feature}")
|
|
298
|
+
lines.append("")
|
|
299
|
+
|
|
300
|
+
if result.match_reasons:
|
|
301
|
+
lines.append(" 匹配原因:")
|
|
302
|
+
for reason in result.match_reasons[:5]: # 只顯示前 5 個
|
|
303
|
+
lines.append(f" • {reason}")
|
|
304
|
+
lines.append("")
|
|
305
|
+
|
|
306
|
+
if result.warnings:
|
|
307
|
+
lines.append(" ⚠️ 注意事項:")
|
|
308
|
+
for warning in result.warnings:
|
|
309
|
+
lines.append(f" • {warning}")
|
|
310
|
+
lines.append("")
|
|
311
|
+
|
|
312
|
+
lines.append("-" * 70)
|
|
313
|
+
lines.append("")
|
|
314
|
+
|
|
315
|
+
return '\n'.join(lines)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
# ============================================================================
|
|
319
|
+
# CLI 介面
|
|
320
|
+
# ============================================================================
|
|
321
|
+
|
|
322
|
+
def main():
|
|
323
|
+
parser = argparse.ArgumentParser(
|
|
324
|
+
description='Taiwan Logistics Skill - 物流服務商推薦系統',
|
|
325
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
326
|
+
epilog="""
|
|
327
|
+
範例:
|
|
328
|
+
python recommend.py "超商取貨 冷凍配送"
|
|
329
|
+
python recommend.py "7-11 B2C 大量訂單" --format json
|
|
330
|
+
python recommend.py "生鮮電商 溫控 穩定" --format simple
|
|
331
|
+
python recommend.py "新創公司 API 設計" --top 2
|
|
332
|
+
|
|
333
|
+
關鍵字建議:
|
|
334
|
+
ECPay: 穩定、市佔、高交易量、電商、文檔、SDK、超商、宅配
|
|
335
|
+
NewebPay: 多元、會員、整合、簡單、快速、便利
|
|
336
|
+
PAYUNi: 溫控、冷凍、冷藏、生鮮、API、JSON、RESTful、新創
|
|
337
|
+
"""
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
parser.add_argument('query', type=str, help='需求描述 (關鍵字以空格分隔)')
|
|
341
|
+
parser.add_argument('--format', type=str, choices=['detailed', 'simple', 'json'],
|
|
342
|
+
default='detailed', help='輸出格式 (預設: detailed)')
|
|
343
|
+
parser.add_argument('--top', type=int, default=3, help='回傳前 K 個推薦 (預設: 3)')
|
|
344
|
+
|
|
345
|
+
args = parser.parse_args()
|
|
346
|
+
|
|
347
|
+
try:
|
|
348
|
+
# 初始化推薦引擎
|
|
349
|
+
recommender = LogisticsRecommender()
|
|
350
|
+
|
|
351
|
+
# 執行推薦
|
|
352
|
+
results = recommender.recommend(args.query, top_k=args.top)
|
|
353
|
+
|
|
354
|
+
# 輸出結果
|
|
355
|
+
output = recommender.format_output(results, format_type=args.format)
|
|
356
|
+
print(output)
|
|
357
|
+
|
|
358
|
+
return 0
|
|
359
|
+
|
|
360
|
+
except FileNotFoundError as e:
|
|
361
|
+
print(f"錯誤: {e}", file=sys.stderr)
|
|
362
|
+
return 1
|
|
363
|
+
|
|
364
|
+
except Exception as e:
|
|
365
|
+
print(f"執行錯誤: {e}", file=sys.stderr)
|
|
366
|
+
return 1
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
if __name__ == '__main__':
|
|
370
|
+
sys.exit(main())
|