feat(search): 新增共享中文语义词表(285 词/1303 词根),前端与 API 统一支持中文搜索并优化相关性排序

This commit is contained in:
sgp
2026-11-04 22:24:33 +08:00
parent 373e76218b
commit d95d2cccf2
10 changed files with 2156 additions and 31 deletions
+145
View File
@@ -0,0 +1,145 @@
#!/usr/bin/env node
/**
* 生成 js/data/cn-tags.js —— 中文语义标签(中文 → 英文词根)
*
* 单一真相源:前端搜索(iconlib.js)、API 索引构建(api/build-index.js)共用。
* 设计:不往每个图标里写标签(会撑大 500MB 数据),而是在搜索时把中文查询
* 展开成英文词根去匹配图标名,历史素材与新导入素材统一生效。
*
* 用法:node scripts/gen-cn-tags.js
*/
'use strict';
const fs = require('fs');
const path = require('path');
const ROOT = path.resolve(__dirname, '..');
const API_INDEX = path.join(ROOT, 'api', 'build-index.js');
const OUT = path.join(ROOT, 'js', 'data', 'cn-tags.js');
/** 从 api/build-index.js 提取 CN_ALIAS 常量(避免两处维护) */
function extractCnAlias() {
const text = fs.readFileSync(API_INDEX, 'utf8');
const start = text.indexOf('const CN_ALIAS = {');
if (start < 0) throw new Error('未找到 CN_ALIAS');
const end = text.indexOf('\n};', start);
if (end < 0) throw new Error('CN_ALIAS 未闭合');
const objText = text.slice(start + 'const CN_ALIAS = '.length, end + 3);
// eslint-disable-next-line no-new-func
return new Function('return ' + objText)();
}
/** 补充:UI / 设计 / 通用场景词(与已有词条合并,不覆盖) */
const EXTRA = {
'渐变': ['gradient'],
'纹理': ['texture', 'pattern'],
'阴影': ['shadow'],
'边框': ['border', 'frame'],
'背景': ['background'],
'圆角': ['radius', 'rounded'],
'扁平': ['flat'],
'线性': ['line', 'outline'],
'填充': ['fill', 'solid'],
'按钮': ['button'],
'卡片': ['card'],
'表单': ['form', 'input'],
'导航': ['nav', 'menu'],
'弹窗': ['dialog', 'modal'],
'加载': ['loading', 'spinner', 'loader'],
'成功': ['check', 'success', 'done'],
'失败': ['error', 'fail', 'x'],
'警告': ['warning', 'alert'],
'提示': ['info', 'tip', 'hint'],
'登录': ['login', 'signin'],
'注册': ['register', 'signup'],
'收藏': ['bookmark', 'star', 'heart'],
'点赞': ['like', 'thumbs-up', 'heart'],
'评论': ['comment', 'message'],
'定位': ['location', 'pin', 'gps'],
'日期': ['calendar', 'date'],
'团队': ['team', 'users', 'group'],
'权限': ['lock', 'shield', 'key'],
'仓储': ['warehouse', 'box', 'package'],
'药品': ['pill', 'medicine'],
'书籍': ['book', 'library', 'reading'],
'图片': ['image', 'photo', 'picture'],
'宠物': ['pet', 'dog', 'cat', 'paw'],
'植物': ['plant', 'leaf', 'tree', 'flower'],
'家居': ['home', 'furniture', 'sofa', 'bed'],
'维修': ['repair', 'wrench', 'tool'],
'合同': ['contract', 'document', 'agreement'],
'政府': ['government', 'building', 'flag'],
'礼物': ['gift', 'present'],
'节日': ['gift', 'party', 'celebration', 'cake'],
'添加': ['plus', 'add', 'new'],
'删除': ['trash', 'delete', 'remove'],
'编辑': ['edit', 'pencil', 'pen'],
'保存': ['save', 'disk'],
'刷新': ['refresh', 'reload', 'sync'],
'关闭': ['close', 'cancel'],
'菜单': ['menu', 'bars', 'list'],
'更多': ['more', 'dots', 'ellipsis'],
'全屏': ['fullscreen', 'maximize', 'expand'],
'音量': ['volume', 'speaker', 'sound'],
'播放': ['play'],
'暂停': ['pause'],
'麦克风': ['mic', 'microphone'],
'耳机': ['headphone'],
'文件夹': ['folder'],
'链接': ['link', 'chain', 'url'],
'密码': ['password', 'lock', 'key'],
'指纹': ['fingerprint'],
'人脸': ['face', 'user'],
'条形码': ['barcode', 'scan'],
'二维码扫描': ['qr', 'scan'],
'加载中': ['loading', 'spinner'],
'空状态': ['empty', 'inbox', 'box'],
'错误页': ['error', 'alert', 'warning'],
'404': ['error', 'alert'],
'品牌标志': ['logo', 'brand'],
'标志': ['logo', 'mark', 'brand'],
'字母': ['letter', 'alphabet', 'text'],
'数字': ['number', 'digit'],
'圆形': ['circle', 'round'],
'方形': ['square', 'box'],
'三角': ['triangle'],
'六边形': ['hexagon'],
'星星': ['star', 'sparkle']
};
function merge(base, extra) {
const out = {};
for (const [k, v] of Object.entries(base)) out[k] = [...new Set(v)];
for (const [k, v] of Object.entries(extra)) {
out[k] = [...new Set([...(out[k] || []), ...v])];
}
return out;
}
function main() {
const merged = merge(extractCnAlias(), EXTRA);
const words = Object.keys(merged).sort((a, b) => b.length - a.length); // 长词优先匹配
const ordered = {};
for (const w of words) ordered[w] = merged[w];
const body = JSON.stringify(ordered, null, 2);
const content = `/* 画个标 · 中文语义标签(中文 → 英文词根)
* 由 scripts/gen-cn-tags.js 生成,勿手动编辑;扩展词表请改生成器后重跑。
* 用途:搜索时把中文查询展开为英文词根,匹配图标 name(历史/新增素材统一生效)。
* 兼容:浏览器挂 window.HGB_CnTags;Node 可 require。
*/
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory();
else root.HGB_CnTags = factory();
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
return ${body};
});
`;
fs.writeFileSync(OUT, content);
console.log(`已生成 ${path.relative(ROOT, OUT)}`);
console.log(`中文词条: ${Object.keys(ordered).length},英文词根总数: ${Object.values(ordered).reduce((a, b) => a + b.length, 0)}`);
}
main();