analyze.template.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // ==============================================================================
  2. // <品类名> · VOC 数据分析模块(模板)
  3. // ==============================================================================
  4. // 职责:
  5. // - 加载 _merged.json
  6. // - 提供 getEvidence() 主查询 API
  7. // - 提供 filterByPlatform/Hypothesis/Product 等辅助筛选
  8. // - 判断 "substantive" 内容(过滤纯表情/水字)
  9. // ==============================================================================
  10. const fs = require('fs');
  11. const path = require('path');
  12. const CATEGORY = '<品类>'; // TODO: 改为实际目录名
  13. // ==========================================================
  14. // 1. 常量
  15. // ==========================================================
  16. const HYPOTHESES = {
  17. H1: '<H1 主题>', // TODO: 按 2.VOC深度思路 填写
  18. H2: '<H2 主题>',
  19. H3: '<H3 主题>',
  20. H4: '<H4 主题>',
  21. H5: '<H5 主题>',
  22. H6: '<H6 主题>',
  23. H7: '<H7 主题>',
  24. H8: '<H8 主题>',
  25. };
  26. const PLATFORMS = ['xhs', 'douyin', 'weibo', 'jd', 'tmall', 'amazon'];
  27. // ==========================================================
  28. // 2. 数据加载
  29. // ==========================================================
  30. function loadMerged() {
  31. const fp = path.resolve(__dirname, '..', '..', 'docs', CATEGORY, 'raw', '_merged.json');
  32. if (!fs.existsSync(fp)) {
  33. throw new Error(`_merged.json 不存在。请先跑 collect --merge。路径:${fp}`);
  34. }
  35. const data = JSON.parse(fs.readFileSync(fp, 'utf8'));
  36. if (!data.items || !Array.isArray(data.items)) {
  37. throw new Error(`_merged.json 格式错误:缺少 items 数组`);
  38. }
  39. return data;
  40. }
  41. // ==========================================================
  42. // 3. 内容实质性判断
  43. // ==========================================================
  44. function isSubstantive(content, minChars = 10) {
  45. const s = String(content || '').trim();
  46. if (s.length < minChars) return false;
  47. // 剥离 [表情] 标记和 unicode 表情
  48. const stripped = s
  49. .replace(/\[[^\]]+\]/g, '')
  50. .replace(/[\s\p{P}\p{Emoji_Presentation}\p{Extended_Pictographic}]/gu, '');
  51. return stripped.length >= Math.max(4, Math.floor(minChars / 2));
  52. }
  53. // ==========================================================
  54. // 4. 基础筛选
  55. // ==========================================================
  56. const filterByPlatform = (items, p) => items.filter((it) => it.platform === p);
  57. const filterByHypothesis = (items, h) => items.filter((it) => (it.hypotheses || []).includes(h));
  58. const filterByProduct = (items, prod) => items.filter((it) => it.product === prod);
  59. const filterByKeyword = (items, kw) => items.filter((it) => it.keyword === kw);
  60. const filterBySentiment = (items, s) => items.filter((it) => it.sentiment === s);
  61. // ==========================================================
  62. // 5. 内容匹配
  63. // ==========================================================
  64. function filterByContentMatch(items, regex) {
  65. if (!regex) return items;
  66. const re = regex instanceof RegExp ? regex : new RegExp(regex);
  67. return items.filter((it) => re.test(it.content || ''));
  68. }
  69. // ==========================================================
  70. // 6. 核心 API · getEvidence
  71. // ==========================================================
  72. /**
  73. * 从 items 池里取出证据卡
  74. * @param {Array<Item>} items
  75. * @param {Object} opts
  76. * @param {string} [opts.keyword]
  77. * @param {string} [opts.product]
  78. * @param {string} [opts.platform]
  79. * @param {RegExp|string} [opts.contentMatch]
  80. * @param {Array<string>} [opts.hypotheses] - 必含的 H 标签(AND 关系)
  81. * @param {string} [opts.sentiment] - positive / negative / neutral
  82. * @param {number} [opts.minChars=10]
  83. * @param {number} [opts.minLikes=0]
  84. * @param {number} [opts.top=5]
  85. * @param {number} [opts.seed=0] - 乱序种子(避免章节重复)
  86. * @returns {Array<Item>}
  87. */
  88. function getEvidence(items, opts = {}) {
  89. let pool = items;
  90. if (opts.keyword) pool = filterByKeyword(pool, opts.keyword);
  91. if (opts.product) pool = filterByProduct(pool, opts.product);
  92. if (opts.platform) pool = filterByPlatform(pool, opts.platform);
  93. if (opts.sentiment) pool = filterBySentiment(pool, opts.sentiment);
  94. if (opts.contentMatch) pool = filterByContentMatch(pool, opts.contentMatch);
  95. if (opts.hypotheses?.length) {
  96. pool = pool.filter((it) => opts.hypotheses.every((h) => (it.hypotheses || []).includes(h)));
  97. }
  98. const minChars = opts.minChars ?? 10;
  99. pool = pool.filter((it) => isSubstantive(it.content, minChars));
  100. if (opts.minLikes) pool = pool.filter((it) => (it.likes || 0) >= opts.minLikes);
  101. // 去重:前 40 字相同视为同内容
  102. const seen = new Set();
  103. pool = pool.filter((it) => {
  104. const key = String(it.content || '').slice(0, 40);
  105. if (seen.has(key)) return false;
  106. seen.add(key);
  107. return true;
  108. });
  109. // 按 likes 降序
  110. pool.sort((a, b) => (b.likes || 0) - (a.likes || 0));
  111. const top = opts.top || 5;
  112. // 取前 top*2 再按 seed 乱序选 top(避免每章都抽到同几条)
  113. const top2 = pool.slice(0, top * 2);
  114. if (opts.seed) return shuffleWithSeed(top2, opts.seed).slice(0, top);
  115. return top2.slice(0, top);
  116. }
  117. // ==========================================================
  118. // 7. 工具
  119. // ==========================================================
  120. function shuffleWithSeed(arr, seed) {
  121. const result = arr.slice();
  122. let rnd = seed || 1;
  123. for (let i = result.length - 1; i > 0; i--) {
  124. rnd = (rnd * 9301 + 49297) % 233280;
  125. const j = Math.floor((rnd / 233280) * (i + 1));
  126. [result[i], result[j]] = [result[j], result[i]];
  127. }
  128. return result;
  129. }
  130. function fmtLikes(n) {
  131. if (!n) return '0';
  132. if (n >= 10000) return (n / 10000).toFixed(1) + 'w';
  133. if (n >= 1000) return (n / 1000).toFixed(1) + 'k';
  134. return String(n);
  135. }
  136. // ==========================================================
  137. // 8. 聚合统计
  138. // ==========================================================
  139. function statsByPlatform(items) {
  140. const map = {};
  141. for (const it of items) map[it.platform] = (map[it.platform] || 0) + 1;
  142. return map;
  143. }
  144. function statsByHypothesis(items) {
  145. const map = {};
  146. for (const it of items) {
  147. for (const h of it.hypotheses || []) map[h] = (map[h] || 0) + 1;
  148. }
  149. return map;
  150. }
  151. function statsByProduct(items) {
  152. const map = {};
  153. for (const it of items) map[it.product] = (map[it.product] || 0) + 1;
  154. return map;
  155. }
  156. function statsBySentiment(items) {
  157. const map = { positive: 0, negative: 0, neutral: 0 };
  158. for (const it of items) map[it.sentiment || 'neutral']++;
  159. return map;
  160. }
  161. // ==========================================================
  162. // 9. 导出
  163. // ==========================================================
  164. module.exports = {
  165. loadMerged,
  166. getEvidence,
  167. isSubstantive,
  168. filterByPlatform,
  169. filterByHypothesis,
  170. filterByProduct,
  171. filterByKeyword,
  172. filterBySentiment,
  173. filterByContentMatch,
  174. shuffleWithSeed,
  175. fmtLikes,
  176. statsByPlatform,
  177. statsByHypothesis,
  178. statsByProduct,
  179. statsBySentiment,
  180. HYPOTHESES,
  181. PLATFORMS,
  182. };