|
12 | 12 | if (prepared) return prepared; |
13 | 13 | prepared = []; |
14 | 14 | index.documents.forEach(function (doc) { |
15 | | - var page = normalize(doc.title); |
| 15 | + var page = normalize(doc.title), parents = []; |
16 | 16 | doc.sections.forEach(function (section, i) { |
| 17 | + // 上级小节的标题:“双亲委派模型”下的“2. 好处”,搜“双亲委派的好处”时要能找到 |
| 18 | + while (parents.length && parents[parents.length - 1].level >= section.level) parents.pop(); |
| 19 | + var parent = parents.length ? parents[parents.length - 1].heading : ''; |
| 20 | + if (section.level > 1) parents.push({ level: section.level, heading: normalize(section.title) }); |
17 | 21 | // 文章开头没有正文的部分与一级标题重复,不单独作为结果 |
18 | 22 | if (i === 0 && !section.text && doc.sections.length > 1 && normalize(doc.sections[1].title) === page) return; |
19 | 23 | var heading = normalize(section.title); |
20 | 24 | var body = normalize(section.text); |
21 | 25 | // 学习路线只是阅读导读,附录是实验过程,排在讲解正文之后 |
22 | 26 | var penalty = (/^学习路线/.test(doc.title) ? 45 : 0) + (/^a\d+ /.test(heading) ? 40 : 0) + (/ - 进阶$/.test(doc.title) ? 8 : 0) + (heading === '参考资料' ? 30 : 0); |
23 | 27 | var bare = heading.replace(/^(?:[一二三四五六七八九十]+、|[a-z]?\d+(?:\.\d+)*\.?\s)\s*/, ''); |
24 | | - prepared.push({ page: page, heading: heading, body: body, text: heading + ' ' + page + ' ' + body, |
| 28 | + prepared.push({ page: page, heading: heading, parent: parent, body: body, text: heading + ' ' + parent + ' ' + page + ' ' + body, |
25 | 29 | pageParts: page.split(' - ').map(function (x) { return x.trim(); }), bare: bare, core: bare.replace(/(模型|原理|机制)$/, '').trim(), lead: body.slice(0, 120), flatHeading: heading.replace(/ /g, ''), flatBody: body.replace(/ /g, ''), penalty: penalty, pageTitle: doc.title, path: doc.path, section: section }); |
26 | 30 | }); |
27 | 31 | }); |
|
207 | 211 | got += weights[i]; |
208 | 212 | var head = entry.heading.includes(hit); |
209 | 213 | if (head) inHead++; |
210 | | - value += weights[i] * ((head ? 3 : 0) + (entry.page.includes(hit) ? 1 : 0) + Math.min(occurrences(entry.body, hit), 3) * .4); |
| 214 | + value += weights[i] * ((head ? 3 : entry.parent.includes(hit) ? 1.5 : 0) + (entry.page.includes(hit) ? 1 : 0) + Math.min(occurrences(entry.body, hit), 3) * .4); |
211 | 215 | }); |
212 | 216 | // 门槛按面经问题的开发集选定:命中的信息量不到 15% 的不算,标题中一个都没有命中的要到 40% |
213 | 217 | if (got < total * .15 || (!inHead && got < total * .4)) return; |
|
0 commit comments