nonebot-plugin-bugu 0.2.0__py3-none-any.whl
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.
- nonebot_plugin_bugu/__init__.py +31 -0
- nonebot_plugin_bugu/api.py +282 -0
- nonebot_plugin_bugu/collection.py +166 -0
- nonebot_plugin_bugu/commands.py +234 -0
- nonebot_plugin_bugu/config.py +91 -0
- nonebot_plugin_bugu/draw.py +406 -0
- nonebot_plugin_bugu/features/__init__.py +0 -0
- nonebot_plugin_bugu/features/collection.py +247 -0
- nonebot_plugin_bugu/features/common.py +100 -0
- nonebot_plugin_bugu/features/flowers/__init__.py +0 -0
- nonebot_plugin_bugu/features/flowers/data.py +167 -0
- nonebot_plugin_bugu/features/flowers/render.py +300 -0
- nonebot_plugin_bugu/features/flowers/reply.py +123 -0
- nonebot_plugin_bugu/features/neighbors/__init__.py +0 -0
- nonebot_plugin_bugu/features/neighbors/data.py +143 -0
- nonebot_plugin_bugu/features/neighbors/render.py +225 -0
- nonebot_plugin_bugu/features/neighbors/reply.py +149 -0
- nonebot_plugin_bugu/features/recipes/__init__.py +0 -0
- nonebot_plugin_bugu/features/recipes/data.py +313 -0
- nonebot_plugin_bugu/features/recipes/render.py +371 -0
- nonebot_plugin_bugu/features/recipes/reply.py +219 -0
- nonebot_plugin_bugu/features/recipes/search.py +151 -0
- nonebot_plugin_bugu/features/starsea/__init__.py +0 -0
- nonebot_plugin_bugu/features/starsea/data.py +267 -0
- nonebot_plugin_bugu/features/starsea/render.py +579 -0
- nonebot_plugin_bugu/features/starsea/reply.py +356 -0
- nonebot_plugin_bugu/features/starsea/search.py +271 -0
- nonebot_plugin_bugu/fonts.py +152 -0
- nonebot_plugin_bugu/runtime.py +109 -0
- nonebot_plugin_bugu/values.py +87 -0
- nonebot_plugin_bugu-0.2.0.dist-info/METADATA +257 -0
- nonebot_plugin_bugu-0.2.0.dist-info/RECORD +35 -0
- nonebot_plugin_bugu-0.2.0.dist-info/WHEEL +4 -0
- nonebot_plugin_bugu-0.2.0.dist-info/entry_points.txt +3 -0
- nonebot_plugin_bugu-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
"""星海地图的文字回复与命令逻辑。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import List, Optional, Sequence, Tuple
|
|
6
|
+
|
|
7
|
+
from ...collection import KIND_STARSEA
|
|
8
|
+
from ...values import grade_text
|
|
9
|
+
from ..common import Reply, ReplyEnv, command_start, render_card, usage
|
|
10
|
+
from .data import SeaMap, SeaPoint, Species, StarSeaData
|
|
11
|
+
from .render import render_map_card, render_point_card
|
|
12
|
+
from .search import PointHit, find_maps, find_points, resolve_query, search_points
|
|
13
|
+
|
|
14
|
+
SPECIES_TAG_KINDS = ("S", "A", "B", "C")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# ------------------------------------------------------------------ 文案
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def helper_text() -> str:
|
|
21
|
+
return usage(
|
|
22
|
+
"星海 [词] —— 智能识别区域 / 点位 / 物种",
|
|
23
|
+
"星海地图 [区域] —— 区域地图卡片",
|
|
24
|
+
"星海点位 <点位名> —— 点位详情卡片",
|
|
25
|
+
"星海查 <物种 / 大类 / 点位名> —— 跨区域搜索",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def map_list_text(data: StarSeaData) -> str:
|
|
30
|
+
if not data.maps:
|
|
31
|
+
return "布谷工具箱还没有星海地图数据。"
|
|
32
|
+
lines = [f"星海地图 · {len(data.maps)} 个区域", ""]
|
|
33
|
+
for sea_map in data.maps:
|
|
34
|
+
lines.append(f"· {sea_map.display_name}({sea_map.code}){len(sea_map.points)} 个点位")
|
|
35
|
+
lines.append("")
|
|
36
|
+
lines.append(usage("星海地图 <区域代号> 查看地图,例如 星海地图 " + data.maps[0].code))
|
|
37
|
+
return "\n".join(lines)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def map_text(
|
|
41
|
+
data: StarSeaData, sea_map: SeaMap, collected: Optional[set] = None
|
|
42
|
+
) -> str:
|
|
43
|
+
lines = [f"【{sea_map.display_name}】{sea_map.code} · {len(sea_map.points)} 个点位", ""]
|
|
44
|
+
for point in sea_map.ordered_points():
|
|
45
|
+
species = data.species_of(point)
|
|
46
|
+
type_name = f" · {point.type_name}" if point.type_name else ""
|
|
47
|
+
count = f"({len(species)} 种" if species else "("
|
|
48
|
+
if species and collected:
|
|
49
|
+
done = sum(1 for sp in species if sp.id in collected)
|
|
50
|
+
count += f"·已收 {done}/{len(species)}"
|
|
51
|
+
count += ")" if species else ""
|
|
52
|
+
lines.append(f"· {point.name}{type_name}{count}")
|
|
53
|
+
if sea_map.encounters:
|
|
54
|
+
lines.append("")
|
|
55
|
+
lines.append("本区可初次偶遇:" + "、".join(e.name for e in sea_map.encounters))
|
|
56
|
+
lines.append("")
|
|
57
|
+
lines.append(usage("星海点位 <点位名> 查看详情"))
|
|
58
|
+
return "\n".join(lines)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def map_choices_text(maps: Sequence[SeaMap], query: str) -> str:
|
|
62
|
+
lines = [f"「{query}」匹配到 {len(maps)} 个区域:"]
|
|
63
|
+
for sea_map in maps:
|
|
64
|
+
lines.append(f"· {sea_map.display_name}({sea_map.code})")
|
|
65
|
+
lines.append(usage(f"星海地图 {maps[0].code} 查看"))
|
|
66
|
+
return "\n".join(lines)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def species_tag(species: Species) -> str:
|
|
70
|
+
pieces = []
|
|
71
|
+
if species.grade:
|
|
72
|
+
pieces.append(species.grade)
|
|
73
|
+
if species.islet_specialty:
|
|
74
|
+
pieces.append("特产")
|
|
75
|
+
meta = "·".join(pieces)
|
|
76
|
+
return f"{species.label}({meta})" if meta else species.label
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def point_text(
|
|
80
|
+
data: StarSeaData,
|
|
81
|
+
sea_map: SeaMap,
|
|
82
|
+
point: SeaPoint,
|
|
83
|
+
collected: Optional[set] = None,
|
|
84
|
+
) -> str:
|
|
85
|
+
groups = data.groups_of(point)
|
|
86
|
+
species_total = sum(len(lst) for _, lst in groups)
|
|
87
|
+
head = [f"【{point.name}】{point.type_name or '点位'}"]
|
|
88
|
+
if groups:
|
|
89
|
+
line = f"{sea_map.display_name} · {species_total} 种可采集物 · {len(groups)} 个大类"
|
|
90
|
+
if collected:
|
|
91
|
+
done = sum(1 for sp in data.species_of(point) if sp.id in collected)
|
|
92
|
+
line += f" · 已收集 {done}/{species_total}"
|
|
93
|
+
head.append(line)
|
|
94
|
+
elif point.is_station and sea_map.encounters:
|
|
95
|
+
head.append(f"{point.type_name or '传送点'} · 本区可初次偶遇 {len(sea_map.encounters)} 位友邻")
|
|
96
|
+
elif point.special:
|
|
97
|
+
head.append(f"{sea_map.display_name} · {point.type_name or '特殊点位'}")
|
|
98
|
+
else:
|
|
99
|
+
head.append(f"{sea_map.display_name} · 特殊点位")
|
|
100
|
+
|
|
101
|
+
if point.special and not groups:
|
|
102
|
+
if point.is_station and sea_map.encounters:
|
|
103
|
+
head.append("本区可初次偶遇:" + "、".join(e.name for e in sea_map.encounters))
|
|
104
|
+
if point.note:
|
|
105
|
+
head.append(point.note)
|
|
106
|
+
return "\n".join(head)
|
|
107
|
+
|
|
108
|
+
body: List[str] = []
|
|
109
|
+
for kind_name, species_list in groups:
|
|
110
|
+
names = "、".join(species_tag(sp) for sp in species_list)
|
|
111
|
+
body.append(f"· {kind_name} {len(species_list)}:{names}")
|
|
112
|
+
|
|
113
|
+
if not body:
|
|
114
|
+
fallback: List[str] = []
|
|
115
|
+
if point.creatures:
|
|
116
|
+
fallback.append("生物类型:" + "、".join(point.creatures))
|
|
117
|
+
if point.trees:
|
|
118
|
+
fallback.append("树类:" + "、".join(point.trees))
|
|
119
|
+
if point.flowers:
|
|
120
|
+
fallback.append("花卉:" + "、".join(point.flowers))
|
|
121
|
+
if point.specialty:
|
|
122
|
+
fallback.append(f"特产:{point.specialty}")
|
|
123
|
+
body.extend(f"· {item}" for item in fallback) if fallback else body.append(
|
|
124
|
+
"· 这个点位还没有填写采集物"
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
text = "\n".join(head + [""] + body)
|
|
128
|
+
if point.note:
|
|
129
|
+
text += f"\n\n{point.note}"
|
|
130
|
+
return text
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def point_choices_text(points: Sequence[Tuple[SeaMap, SeaPoint]], query: str) -> str:
|
|
134
|
+
lines = [f"「{query}」匹配到 {len(points)} 个点位:"]
|
|
135
|
+
for sea_map, point in points:
|
|
136
|
+
lines.append(f"· {point.name}({sea_map.display_name})")
|
|
137
|
+
lines.append(usage("星海点位 <点位名> 查看详情"))
|
|
138
|
+
return "\n".join(lines)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def species_text(data: StarSeaData, species: Species, limit: int = 10) -> str:
|
|
142
|
+
tags = [species.kind_name or species.kind, f"{grade_text(species.grade)}品质"]
|
|
143
|
+
if species.islet_specialty:
|
|
144
|
+
tags.append("星屿特产")
|
|
145
|
+
lines = [species.label, " · ".join(tags)]
|
|
146
|
+
if species.detail:
|
|
147
|
+
lines.append(f"详情:{species.detail.strip()}")
|
|
148
|
+
|
|
149
|
+
places = data.points_with_species(species.id)
|
|
150
|
+
if places:
|
|
151
|
+
lines.append(f"出现在 {len(places)} 个点位:")
|
|
152
|
+
for sea_map, point in places[:limit]:
|
|
153
|
+
lines.append(f"· {point.name}({sea_map.display_name})")
|
|
154
|
+
if len(places) > limit:
|
|
155
|
+
lines.append(f"…… 还有 {len(places) - limit} 个点位")
|
|
156
|
+
else:
|
|
157
|
+
lines.append("暂时没有点位收录这个物种。")
|
|
158
|
+
return "\n".join(lines)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def search_text(
|
|
162
|
+
data: StarSeaData, hits: Sequence[PointHit], query: str, limit: int = 12
|
|
163
|
+
) -> str:
|
|
164
|
+
if not hits:
|
|
165
|
+
return f"没找到和「{query}」有关的点位。"
|
|
166
|
+
shown = list(hits[:limit])
|
|
167
|
+
lines = [f"星海地图 · 搜索「{query}」"]
|
|
168
|
+
if len(hits) > len(shown):
|
|
169
|
+
lines.append(f"命中 {len(hits)} 个点位,只显示前 {len(shown)} 个")
|
|
170
|
+
else:
|
|
171
|
+
lines.append(f"命中 {len(hits)} 个点位")
|
|
172
|
+
lines.append("")
|
|
173
|
+
for index, hit in enumerate(shown, start=1):
|
|
174
|
+
lines.append(f"{index}. {hit.point.name}({hit.map_name})")
|
|
175
|
+
if hit.matched:
|
|
176
|
+
preview = "、".join(hit.matched[:4])
|
|
177
|
+
more = f" 等 {len(hit.matched)} 种" if len(hit.matched) > 4 else ""
|
|
178
|
+
lines.append(f" 命中 {preview}{more}")
|
|
179
|
+
else:
|
|
180
|
+
lines.append(" 点位名命中")
|
|
181
|
+
lines.append("")
|
|
182
|
+
lines.append(usage("星海点位 <点位名> 查看详情"))
|
|
183
|
+
return "\n".join(lines)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def not_found_text(query: str, suggestions: Sequence[str]) -> str:
|
|
187
|
+
lines = [f"没找到和「{query}」有关的区域 / 点位 / 物种。"]
|
|
188
|
+
if suggestions:
|
|
189
|
+
lines.append("是不是在找:" + "、".join(suggestions))
|
|
190
|
+
lines.append(usage("星海地图 看看有哪些区域"))
|
|
191
|
+
return "\n".join(lines)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def empty_text() -> str:
|
|
195
|
+
return "布谷工具箱的星海地图还没有数据,等站点更新后再试吧。"
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
# ------------------------------------------------------------------ 出图
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
async def _map_reply(data: StarSeaData, sea_map: SeaMap, env: ReplyEnv) -> Reply:
|
|
202
|
+
collected = env.collected_ids(KIND_STARSEA)
|
|
203
|
+
png = None
|
|
204
|
+
if env.can_render:
|
|
205
|
+
raw = await env.client.get_image(sea_map.image)
|
|
206
|
+
png = await render_card(
|
|
207
|
+
render_map_card,
|
|
208
|
+
data,
|
|
209
|
+
sea_map,
|
|
210
|
+
raw,
|
|
211
|
+
fonts=env.fonts,
|
|
212
|
+
collected=collected or None,
|
|
213
|
+
label="星海地图卡片",
|
|
214
|
+
)
|
|
215
|
+
return png if png is not None else map_text(data, sea_map, collected or None)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
async def _point_reply(data: StarSeaData, sea_map: SeaMap, point: SeaPoint, env: ReplyEnv) -> Reply:
|
|
219
|
+
collected = env.collected_ids(KIND_STARSEA)
|
|
220
|
+
png = None
|
|
221
|
+
if env.can_render:
|
|
222
|
+
species = data.species_of(point)
|
|
223
|
+
urls = [point.header] + [sp.icon for sp in species if sp.has_icon]
|
|
224
|
+
images = await env.client.get_images(urls)
|
|
225
|
+
icons = {sp.id: images.get(sp.icon) for sp in species}
|
|
226
|
+
png = await render_card(
|
|
227
|
+
render_point_card,
|
|
228
|
+
data,
|
|
229
|
+
sea_map,
|
|
230
|
+
point,
|
|
231
|
+
images.get(point.header),
|
|
232
|
+
icons,
|
|
233
|
+
fonts=env.fonts,
|
|
234
|
+
collected=collected or None,
|
|
235
|
+
label="星海点位卡片",
|
|
236
|
+
)
|
|
237
|
+
return png if png is not None else point_text(data, sea_map, point, collected or None)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
# ------------------------------------------------------------------ 命令
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
async def _map_command(data: StarSeaData, arg: str, env: ReplyEnv) -> Reply:
|
|
244
|
+
if not data.maps:
|
|
245
|
+
return empty_text()
|
|
246
|
+
if not arg:
|
|
247
|
+
return map_list_text(data)
|
|
248
|
+
|
|
249
|
+
maps = find_maps(data, arg)
|
|
250
|
+
if not maps:
|
|
251
|
+
return await _smart_with_data(data, arg, env)
|
|
252
|
+
if len(maps) > 1:
|
|
253
|
+
return map_choices_text(maps, arg)
|
|
254
|
+
return await _map_reply(data, maps[0], env)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
async def _point_command(data: StarSeaData, arg: str, env: ReplyEnv) -> Reply:
|
|
258
|
+
if not arg:
|
|
259
|
+
return usage(
|
|
260
|
+
"星海点位 <点位名>",
|
|
261
|
+
"例:星海点位 迷途小屋之屿",
|
|
262
|
+
"不知道点位名?先发 星海地图 看看",
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
points = find_points(data, arg)
|
|
266
|
+
if not points:
|
|
267
|
+
suggestions = resolve_query(data, arg).suggestions
|
|
268
|
+
return not_found_text(arg, suggestions)
|
|
269
|
+
if len(points) > 1:
|
|
270
|
+
exact = [item for item in points if item[1].name == arg.strip()]
|
|
271
|
+
if len(exact) == 1:
|
|
272
|
+
return await _point_reply(data, exact[0][0], exact[0][1], env)
|
|
273
|
+
return point_choices_text(points, arg)
|
|
274
|
+
return await _point_reply(data, points[0][0], points[0][1], env)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
async def _search_command(data: StarSeaData, arg: str, env: ReplyEnv) -> Reply:
|
|
278
|
+
if not arg:
|
|
279
|
+
return usage(
|
|
280
|
+
"星海查 <物种名 / 大类 / 点位名>",
|
|
281
|
+
"例:星海查 三色锦鲤",
|
|
282
|
+
" :星海查 河鱼",
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
resolution = resolve_query(data, arg, limit=env.config.bugu_search_limit)
|
|
286
|
+
if resolution.kind == "species" and resolution.species:
|
|
287
|
+
return species_text(data, resolution.species[0])
|
|
288
|
+
if resolution.kind == "point" and len(resolution.points) == 1:
|
|
289
|
+
sea_map, point = resolution.points[0]
|
|
290
|
+
return await _point_reply(data, sea_map, point, env)
|
|
291
|
+
|
|
292
|
+
hits = search_points(data, arg)
|
|
293
|
+
if not hits:
|
|
294
|
+
return not_found_text(arg, resolution.suggestions)
|
|
295
|
+
return search_text(data, hits, arg, limit=env.config.bugu_search_limit)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
async def _smart_with_data(data: StarSeaData, arg: str, env: ReplyEnv) -> Reply:
|
|
299
|
+
resolution = resolve_query(data, arg, limit=env.config.bugu_search_limit)
|
|
300
|
+
if resolution.kind == "map":
|
|
301
|
+
if len(resolution.maps) == 1:
|
|
302
|
+
return await _map_reply(data, resolution.maps[0], env)
|
|
303
|
+
return map_choices_text(resolution.maps, arg)
|
|
304
|
+
if resolution.kind == "point" and len(resolution.points) == 1:
|
|
305
|
+
sea_map, point = resolution.points[0]
|
|
306
|
+
return await _point_reply(data, sea_map, point, env)
|
|
307
|
+
if resolution.kind == "species" and resolution.species:
|
|
308
|
+
return species_text(data, resolution.species[0])
|
|
309
|
+
if resolution.kind == "search" and resolution.hits:
|
|
310
|
+
return search_text(data, resolution.hits, arg, limit=env.config.bugu_search_limit)
|
|
311
|
+
return not_found_text(arg, resolution.suggestions)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
async def reply_map(arg: str, env: ReplyEnv) -> Reply:
|
|
315
|
+
data = await env.client.get_starsea()
|
|
316
|
+
return await _map_command(data, arg, env)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
async def reply_point(arg: str, env: ReplyEnv) -> Reply:
|
|
320
|
+
if not arg:
|
|
321
|
+
return await _point_command(None, arg, env) # type: ignore[arg-type]
|
|
322
|
+
data = await env.client.get_starsea()
|
|
323
|
+
return await _point_command(data, arg, env)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
async def reply_search(arg: str, env: ReplyEnv) -> Reply:
|
|
327
|
+
if not arg:
|
|
328
|
+
return await _search_command(None, arg, env) # type: ignore[arg-type]
|
|
329
|
+
data = await env.client.get_starsea()
|
|
330
|
+
return await _search_command(data, arg, env)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
async def reply_smart(arg: str, env: ReplyEnv) -> Reply:
|
|
334
|
+
"""``星海 <任意词>``:子命令 / 智能识别。"""
|
|
335
|
+
arg = (arg or "").strip()
|
|
336
|
+
if not arg:
|
|
337
|
+
return helper_text()
|
|
338
|
+
|
|
339
|
+
for keyword, handler in (
|
|
340
|
+
("星海地图", reply_map),
|
|
341
|
+
("星海点位", reply_point),
|
|
342
|
+
("星海查", reply_search),
|
|
343
|
+
("地图", reply_map),
|
|
344
|
+
("点位", reply_point),
|
|
345
|
+
("查", reply_search),
|
|
346
|
+
("搜索", reply_search),
|
|
347
|
+
):
|
|
348
|
+
if arg.startswith(keyword):
|
|
349
|
+
return await handler(arg[len(keyword) :].strip(), env)
|
|
350
|
+
|
|
351
|
+
data = await env.client.get_starsea()
|
|
352
|
+
return await _smart_with_data(data, arg, env)
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def command_prefix() -> str:
|
|
356
|
+
return command_start()
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""查询逻辑:区域 / 点位 / 物种 / 跨区域搜索。
|
|
2
|
+
|
|
3
|
+
搜索规则照搬站点「星海地图」页面的搜索框:
|
|
4
|
+
|
|
5
|
+
* 物种名包含关键词,或
|
|
6
|
+
* 物种大类命中(``鱼`` 能搜到河鱼 / 海鱼,``生物`` 能搜到昆虫 / 河鱼 / 海鱼 / 海岸生物),或
|
|
7
|
+
* 点位名包含关键词
|
|
8
|
+
|
|
9
|
+
命中物种多的点位排前面,纯点位名命中的排后面。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import re
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
from difflib import get_close_matches
|
|
17
|
+
from typing import Dict, List, Optional, Sequence, Set, Tuple
|
|
18
|
+
|
|
19
|
+
from .data import SeaMap, SeaPoint, Species, StarSeaData
|
|
20
|
+
|
|
21
|
+
# 口语别名 → 正式大类名(跟站点 starsea.js 的 KIND_ALIAS 一致)
|
|
22
|
+
KIND_ALIAS: Dict[str, Sequence[str]] = {
|
|
23
|
+
"生物": ("昆虫", "河鱼", "海鱼", "海岸生物"),
|
|
24
|
+
"动物": ("昆虫", "河鱼", "海鱼", "海岸生物"),
|
|
25
|
+
"鱼": ("河鱼", "海鱼"),
|
|
26
|
+
"鱼类": ("河鱼", "海鱼"),
|
|
27
|
+
"虫": ("昆虫",),
|
|
28
|
+
"昆虫": ("昆虫",),
|
|
29
|
+
"树": ("树木",),
|
|
30
|
+
"树木": ("树木",),
|
|
31
|
+
"花": ("鲜花",),
|
|
32
|
+
"花卉": ("鲜花",),
|
|
33
|
+
"鲜花": ("鲜花",),
|
|
34
|
+
"灌木": ("灌木丛",),
|
|
35
|
+
"灌木丛": ("灌木丛",),
|
|
36
|
+
"灌丛": ("灌木丛",),
|
|
37
|
+
"bushes": ("灌木丛",),
|
|
38
|
+
"矿": ("矿产",),
|
|
39
|
+
"矿石": ("矿产",),
|
|
40
|
+
"作物": ("特产",),
|
|
41
|
+
"蔬菜": ("特产",),
|
|
42
|
+
"特产": ("特产",),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_SPACE_RE = re.compile(r"[\s\u3000]+")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def fold(text: str) -> str:
|
|
49
|
+
"""统一大小写 / 空白,便于比较。"""
|
|
50
|
+
return _SPACE_RE.sub("", (text or "")).casefold()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def split_region_prefix(query: str) -> Tuple[str, str]:
|
|
54
|
+
"""把「H83 迷途小屋之屿」拆成 (区域, 点位);没有区域前缀就返回 ("", query)。"""
|
|
55
|
+
parts = (query or "").strip().split(None, 1)
|
|
56
|
+
if len(parts) == 2 and parts[0]:
|
|
57
|
+
return parts[0], parts[1].strip()
|
|
58
|
+
return "", (query or "").strip()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass
|
|
62
|
+
class PointHit:
|
|
63
|
+
"""一条点位命中。"""
|
|
64
|
+
|
|
65
|
+
map_code: str
|
|
66
|
+
map_name: str
|
|
67
|
+
point: SeaPoint
|
|
68
|
+
matched: List[str] = field(default_factory=list)
|
|
69
|
+
name_hit: bool = False
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class Resolution:
|
|
74
|
+
"""一条命令参数的解析结果。
|
|
75
|
+
|
|
76
|
+
``kind`` 取值:``map`` / ``point`` / ``species`` / ``search`` / ``none``。
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
kind: str
|
|
80
|
+
query: str
|
|
81
|
+
maps: List[SeaMap] = field(default_factory=list)
|
|
82
|
+
points: List[Tuple[SeaMap, SeaPoint]] = field(default_factory=list)
|
|
83
|
+
species: List[Species] = field(default_factory=list)
|
|
84
|
+
hits: List[PointHit] = field(default_factory=list)
|
|
85
|
+
suggestions: List[str] = field(default_factory=list)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# ------------------------------------------------------------------ 基础匹配
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def match_kinds(data: StarSeaData, query: str) -> Set[str]:
|
|
92
|
+
"""关键词命中的大类名集合。"""
|
|
93
|
+
q = fold(query)
|
|
94
|
+
if not q:
|
|
95
|
+
return set()
|
|
96
|
+
kinds: Set[str] = set()
|
|
97
|
+
for species in data.species.values():
|
|
98
|
+
if q and q in fold(species.kind):
|
|
99
|
+
kinds.add(species.kind)
|
|
100
|
+
for alias, names in KIND_ALIAS.items():
|
|
101
|
+
if fold(alias) == q:
|
|
102
|
+
kinds.update(names)
|
|
103
|
+
return kinds
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def find_maps(data: StarSeaData, query: str) -> List[SeaMap]:
|
|
107
|
+
"""按代号或区域名找地图,精确优先。"""
|
|
108
|
+
q = fold(query)
|
|
109
|
+
if not q:
|
|
110
|
+
return []
|
|
111
|
+
exact = [m for m in data.maps if fold(m.code) == q or fold(m.name) == q]
|
|
112
|
+
if exact:
|
|
113
|
+
return exact
|
|
114
|
+
return [m for m in data.maps if q in fold(m.name) or q in fold(m.code)]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def find_species(data: StarSeaData, query: str) -> List[Species]:
|
|
118
|
+
"""按物种名找物种,精确优先,其次包含。"""
|
|
119
|
+
q = fold(query)
|
|
120
|
+
if not q:
|
|
121
|
+
return []
|
|
122
|
+
exact = [sp for sp in data.species.values() if fold(sp.name) == q or fold(sp.display_name) == q]
|
|
123
|
+
if exact:
|
|
124
|
+
return exact
|
|
125
|
+
contains = [
|
|
126
|
+
sp
|
|
127
|
+
for sp in data.species.values()
|
|
128
|
+
if q in fold(sp.name) or q in fold(sp.display_name) or q in fold(sp.kind_name)
|
|
129
|
+
]
|
|
130
|
+
contains.sort(key=lambda sp: (len(sp.name), sp.kind, sp.sort))
|
|
131
|
+
return contains
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def find_points(data: StarSeaData, query: str) -> List[Tuple[SeaMap, SeaPoint]]:
|
|
135
|
+
"""按点位名找点位;支持「区域 点位名」限定。
|
|
136
|
+
|
|
137
|
+
顺序:完全同名 → 包含 → 模糊近似。
|
|
138
|
+
"""
|
|
139
|
+
region_query, name_query = split_region_prefix(query)
|
|
140
|
+
if not name_query:
|
|
141
|
+
return []
|
|
142
|
+
|
|
143
|
+
candidates: List[Tuple[SeaMap, SeaPoint]] = list(data.iter_points())
|
|
144
|
+
if region_query:
|
|
145
|
+
maps = find_maps(data, region_query)
|
|
146
|
+
if not maps:
|
|
147
|
+
# 用户指定了区域却找不到,别跨区域乱给结果
|
|
148
|
+
return []
|
|
149
|
+
allowed = {m.code for m in maps}
|
|
150
|
+
candidates = [(m, p) for m, p in candidates if m.code in allowed]
|
|
151
|
+
|
|
152
|
+
q = fold(name_query)
|
|
153
|
+
exact = [(m, p) for m, p in candidates if fold(p.name) == q]
|
|
154
|
+
if exact:
|
|
155
|
+
return exact
|
|
156
|
+
contains = [(m, p) for m, p in candidates if q in fold(p.name)]
|
|
157
|
+
if contains:
|
|
158
|
+
return contains
|
|
159
|
+
|
|
160
|
+
# 模糊匹配:手打错别字的兜底
|
|
161
|
+
names = {p.name for _, p in candidates}
|
|
162
|
+
close = get_close_matches(name_query, list(names), n=5, cutoff=0.45)
|
|
163
|
+
if close:
|
|
164
|
+
return [(m, p) for m, p in candidates if p.name in close]
|
|
165
|
+
return []
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def search_points(data: StarSeaData, query: str, limit: Optional[int] = None) -> List[PointHit]:
|
|
169
|
+
"""跨区域搜索:物种名 / 大类 / 点位名。"""
|
|
170
|
+
q = fold(query)
|
|
171
|
+
if not q:
|
|
172
|
+
return []
|
|
173
|
+
kinds = match_kinds(data, query)
|
|
174
|
+
|
|
175
|
+
hits: List[PointHit] = []
|
|
176
|
+
for sea_map, point in data.iter_points():
|
|
177
|
+
matched: List[str] = []
|
|
178
|
+
for species in data.species_of(point):
|
|
179
|
+
if q in fold(species.label) or species.kind in kinds:
|
|
180
|
+
matched.append(species.label)
|
|
181
|
+
name_hit = q in fold(point.name)
|
|
182
|
+
if not matched and not name_hit:
|
|
183
|
+
continue
|
|
184
|
+
hits.append(
|
|
185
|
+
PointHit(
|
|
186
|
+
map_code=sea_map.code,
|
|
187
|
+
map_name=sea_map.display_name,
|
|
188
|
+
point=point,
|
|
189
|
+
matched=matched,
|
|
190
|
+
name_hit=name_hit,
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
hits.sort(
|
|
195
|
+
key=lambda h: (
|
|
196
|
+
-len(h.matched),
|
|
197
|
+
0 if h.name_hit else 1,
|
|
198
|
+
h.map_name,
|
|
199
|
+
h.point.name,
|
|
200
|
+
)
|
|
201
|
+
)
|
|
202
|
+
if limit is not None and limit > 0:
|
|
203
|
+
return hits[:limit]
|
|
204
|
+
return hits
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def suggest(data: StarSeaData, query: str, count: int = 5) -> List[str]:
|
|
208
|
+
"""没命中时给几个相近的点位名。"""
|
|
209
|
+
names = [p.name for _, p in data.iter_points()]
|
|
210
|
+
if not names:
|
|
211
|
+
return []
|
|
212
|
+
return get_close_matches(query, names, n=count, cutoff=0.35)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# ------------------------------------------------------------------ 智能解析
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def resolve_query(data: StarSeaData, query: str, *, limit: int = 12) -> Resolution:
|
|
219
|
+
"""按「区域 → 点位 → 物种 → 大类 → 搜索」的顺序解析一句话。"""
|
|
220
|
+
query = (query or "").strip()
|
|
221
|
+
result = Resolution(kind="none", query=query)
|
|
222
|
+
if not query:
|
|
223
|
+
return result
|
|
224
|
+
|
|
225
|
+
maps = find_maps(data, query)
|
|
226
|
+
if maps:
|
|
227
|
+
result.kind = "map"
|
|
228
|
+
result.maps = maps
|
|
229
|
+
return result
|
|
230
|
+
|
|
231
|
+
points = find_points(data, query)
|
|
232
|
+
if points:
|
|
233
|
+
exact = [item for item in points if fold(item[1].name) == fold(query)]
|
|
234
|
+
chosen = exact or points
|
|
235
|
+
if len(chosen) == 1:
|
|
236
|
+
result.kind = "point"
|
|
237
|
+
result.points = chosen
|
|
238
|
+
return result
|
|
239
|
+
result.kind = "search"
|
|
240
|
+
result.hits = search_points(data, query, limit=None)
|
|
241
|
+
if result.hits:
|
|
242
|
+
return result
|
|
243
|
+
result.points = chosen
|
|
244
|
+
return result
|
|
245
|
+
|
|
246
|
+
species = find_species(data, query)
|
|
247
|
+
exact_species = [sp for sp in species if fold(sp.name) == fold(query) or fold(sp.display_name) == fold(query)]
|
|
248
|
+
if exact_species:
|
|
249
|
+
result.kind = "species"
|
|
250
|
+
result.species = exact_species
|
|
251
|
+
result.hits = search_points(data, query, limit=None)
|
|
252
|
+
return result
|
|
253
|
+
|
|
254
|
+
hits = search_points(data, query, limit=None)
|
|
255
|
+
if hits:
|
|
256
|
+
result.kind = "search"
|
|
257
|
+
result.hits = hits[:limit] if limit else hits
|
|
258
|
+
return result
|
|
259
|
+
|
|
260
|
+
if len(species) == 1:
|
|
261
|
+
result.kind = "species"
|
|
262
|
+
result.species = species
|
|
263
|
+
return result
|
|
264
|
+
|
|
265
|
+
if species:
|
|
266
|
+
result.kind = "search"
|
|
267
|
+
result.hits = search_points(data, query, limit=limit)
|
|
268
|
+
return result
|
|
269
|
+
|
|
270
|
+
result.suggestions = suggest(data, query)
|
|
271
|
+
return result
|