feat(monitor): 按关键词自动打标签 —— 命中几个话题就打几个标签
标签上配一串关键词(逗号分隔),作品标题里出现其中任意一个就自动贴上。一条标题可以 命中多条规则,命中几条贴几个。关键词和标签名是两回事:标签叫「大字报」,关键词可能是 `szgtdzb`。 三条刻意的设计: * **只贴不摘。** 手动摘掉的标签不会被下一轮采集又贴回来 —— 这也是为什么它只在「作品 第一次入库」和「有人改了规则」这两个时机跑,而不是每轮都跑。 * **改哪条规则就只重跑哪条。** 全都重跑的话,改 A 会把用户从 B 上摘掉的标签也贴回来。 新建/改关键词时立刻对已有作品跑一遍,否则「刚配好关键词,一条都没贴上」会被当成坏了。 * **匹配标题里的子串、不分大小写**,不是只匹配 `#话题#` —— 话题本来就长在标题里,而 `szgtdzb` 这种代号未必包在井号里。代价是关键词定太宽会误伤,设置页里写了这句提醒。 规则单独放 `tagrules.py`:`ingest` 和路由都要用它,而 `ingest` 不能 import `service` (service → runner → ingest 会成环)。 测试验过关键那条会拦:把「只在第一次入库时收」改成每轮都收, `test_a_tag_removed_by_hand_does_not_come_back_next_run` 立刻变红。
This commit is contained in:
@@ -26,17 +26,24 @@ export function TagSettings() {
|
||||
|
||||
const [draftName, setDraftName] = useState('')
|
||||
const [draftColor, setDraftColor] = useState<string>(TAG_COLOR_CHOICES[0])
|
||||
const [draftKeywords, setDraftKeywords] = useState('')
|
||||
// 正在改名的那一个(标签 id)。
|
||||
const [editingId, setEditingId] = useState<number | null>(null)
|
||||
const [editName, setEditName] = useState('')
|
||||
const [editColor, setEditColor] = useState<string>(TAG_COLOR_CHOICES[0])
|
||||
const [editKeywords, setEditKeywords] = useState('')
|
||||
|
||||
const submit = () => {
|
||||
const name = draftName.trim()
|
||||
if (!name) return
|
||||
createTag.mutate(
|
||||
{ name, color: draftColor },
|
||||
{ onSuccess: () => setDraftName('') },
|
||||
{ name, color: draftColor, keywords: draftKeywords },
|
||||
{
|
||||
onSuccess: () => {
|
||||
setDraftName('')
|
||||
setDraftKeywords('')
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -44,13 +51,14 @@ export function TagSettings() {
|
||||
setEditingId(tag.id)
|
||||
setEditName(tag.name)
|
||||
setEditColor(tag.color)
|
||||
setEditKeywords(tag.keywords)
|
||||
}
|
||||
|
||||
const saveEdit = () => {
|
||||
if (editingId === null) return
|
||||
const name = editName.trim()
|
||||
if (!name) return
|
||||
updateTag.mutate({ id: editingId, name, color: editColor })
|
||||
updateTag.mutate({ id: editingId, name, color: editColor, keywords: editKeywords })
|
||||
setEditingId(null)
|
||||
}
|
||||
|
||||
@@ -71,7 +79,7 @@ export function TagSettings() {
|
||||
|
||||
{tags?.map((tag) =>
|
||||
editingId === tag.id ? (
|
||||
<div key={tag.id} className="flex items-center gap-2">
|
||||
<div key={tag.id} className="flex flex-wrap items-center gap-2">
|
||||
<input
|
||||
autoFocus
|
||||
value={editName}
|
||||
@@ -80,9 +88,20 @@ export function TagSettings() {
|
||||
if (event.key === 'Enter') saveEdit()
|
||||
if (event.key === 'Escape') setEditingId(null)
|
||||
}}
|
||||
className="w-40 rounded border border-cyber-neon-cyan/40 bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none"
|
||||
className="w-32 rounded border border-cyber-neon-cyan/40 bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none"
|
||||
/>
|
||||
<ColorPicker value={editColor} onChange={setEditColor} />
|
||||
<input
|
||||
value={editKeywords}
|
||||
placeholder="关键词,逗号分隔:szgtdzb,探店"
|
||||
title="标题里出现其中任意一个就自动贴上这条标签"
|
||||
onChange={(event) => setEditKeywords(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') saveEdit()
|
||||
if (event.key === 'Escape') setEditingId(null)
|
||||
}}
|
||||
className="w-64 rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
|
||||
/>
|
||||
<Button variant="ghost" size="sm" className="h-6 px-1.5" onClick={saveEdit}>
|
||||
<Check className="w-3 h-3 text-cyber-neon-green" />
|
||||
</Button>
|
||||
@@ -103,8 +122,18 @@ export function TagSettings() {
|
||||
>
|
||||
{tag.name}
|
||||
</Badge>
|
||||
<span className="text-[10px] font-mono text-cyber-text-muted">
|
||||
{tag.note_count > 0 ? `用在 ${tag.note_count} 条作品上` : '还没用过'}
|
||||
{/* 规则直接摆出来 —— 「这条标签为什么会自动贴上」得一眼看得出来。 */}
|
||||
{tag.keywords ? (
|
||||
<span className="text-[10px] font-mono text-cyber-text-secondary truncate max-w-[240px]">
|
||||
命中「{tag.keywords}」
|
||||
</span>
|
||||
) : (
|
||||
<span className="text-[10px] font-mono text-cyber-text-muted">
|
||||
没配关键词(只能手动贴)
|
||||
</span>
|
||||
)}
|
||||
<span className="text-[10px] font-mono text-cyber-text-muted flex-shrink-0">
|
||||
{tag.note_count > 0 ? `${tag.note_count} 条作品` : '还没用过'}
|
||||
</span>
|
||||
<div className="ml-auto flex items-center gap-1">
|
||||
<Button
|
||||
@@ -144,22 +173,40 @@ export function TagSettings() {
|
||||
),
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2 pt-2 mt-1 border-t border-cyber-border-subtle">
|
||||
<div className="flex flex-wrap items-center gap-2 pt-2 mt-1 border-t border-cyber-border-subtle">
|
||||
<input
|
||||
value={draftName}
|
||||
placeholder="新标签,比如「重点」"
|
||||
placeholder="新标签,比如「大字报」"
|
||||
onChange={(event) => setDraftName(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') submit()
|
||||
}}
|
||||
className="w-40 rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
|
||||
className="w-32 rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
|
||||
/>
|
||||
<ColorPicker value={draftColor} onChange={setDraftColor} />
|
||||
<input
|
||||
value={draftKeywords}
|
||||
placeholder="关键词,逗号分隔:szgtdzb,探店"
|
||||
title="标题里出现其中任意一个就自动贴上这条标签"
|
||||
onChange={(event) => setDraftKeywords(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') submit()
|
||||
}}
|
||||
className="w-64 rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
|
||||
/>
|
||||
<Button size="sm" disabled={!draftName.trim() || createTag.isPending} onClick={submit}>
|
||||
<Plus className="w-3 h-3 mr-1" />
|
||||
添加
|
||||
</Button>
|
||||
</div>
|
||||
<p className="text-[10px] font-mono text-cyber-text-muted leading-relaxed">
|
||||
配了关键词的标签会
|
||||
<span className="text-cyber-text-secondary"> 自动贴上 </span>
|
||||
:作品标题里出现其中任意一个就贴,命中几条规则就贴几个标签。匹配的是标题里的子串、
|
||||
不分大小写,所以关键词别定得太宽(只写「苏州」的话几乎每条都会被贴上)。
|
||||
<span className="text-cyber-text-secondary"> 只贴不摘 </span>
|
||||
—— 你手动摘掉的,下一轮采集不会又长回来。
|
||||
</p>
|
||||
</div>
|
||||
</Section>
|
||||
)
|
||||
|
||||
@@ -148,15 +148,16 @@ function useTagMutation<TArgs>(
|
||||
|
||||
export function useCreateTag() {
|
||||
return useTagMutation(
|
||||
({ name, color }: { name: string; color: string }) => monitorApi.createTag(name, color),
|
||||
({ name, color, keywords }: { name: string; color: string; keywords: string }) =>
|
||||
monitorApi.createTag(name, color, keywords),
|
||||
'标签已创建',
|
||||
)
|
||||
}
|
||||
|
||||
export function useUpdateTag() {
|
||||
return useTagMutation(
|
||||
({ id, name, color }: { id: number; name: string; color: string }) =>
|
||||
monitorApi.updateTag(id, name, color),
|
||||
({ id, name, color, keywords }: { id: number; name: string; color: string; keywords: string }) =>
|
||||
monitorApi.updateTag(id, name, color, keywords),
|
||||
'标签已保存',
|
||||
)
|
||||
}
|
||||
|
||||
@@ -303,10 +303,10 @@ export const monitorApi = {
|
||||
|
||||
/** 标签词表(设置页维护),带各自被用在多少条作品上。 */
|
||||
listTags: () => api.get<{ tags: MonitorTag[] }>('/monitor/tags'),
|
||||
createTag: (name: string, color = '') =>
|
||||
api.post<MonitorTag>('/monitor/tags', { name, color }),
|
||||
updateTag: (id: number, name: string, color = '') =>
|
||||
api.put<MonitorTag>(`/monitor/tags/${id}`, { name, color }),
|
||||
createTag: (name: string, color = '', keywords = '') =>
|
||||
api.post<MonitorTag>('/monitor/tags', { name, color, keywords }),
|
||||
updateTag: (id: number, name: string, color = '', keywords = '') =>
|
||||
api.put<MonitorTag>(`/monitor/tags/${id}`, { name, color, keywords }),
|
||||
deleteTag: (id: number) => api.delete<{ removed: number }>(`/monitor/tags/${id}`),
|
||||
/** 把一条作品的标签**整体替换**成这一组。空数组 = 全摘掉。 */
|
||||
setNoteTags: (noteId: string, tagIds: number[], platform?: string) =>
|
||||
|
||||
@@ -188,6 +188,13 @@ export interface TagRef {
|
||||
/** 词表里的标签,额外带一个「被用在多少条作品上」—— 删之前要能看清代价。 */
|
||||
export interface MonitorTag extends TagRef {
|
||||
note_count: number
|
||||
/**
|
||||
* 自动打标签的规则:**逗号分隔的关键词**。作品标题里出现其中任意一个,这条标签就
|
||||
* 自动贴上;命中几条规则就贴几个标签。空串 = 这条标签只能手动贴。
|
||||
*
|
||||
* 关键词和标签名是两回事 —— 标签叫「大字报」,关键词可能是 `szgtdzb`。
|
||||
*/
|
||||
keywords: string
|
||||
}
|
||||
|
||||
export interface MetricPoint {
|
||||
|
||||
Reference in New Issue
Block a user