feat:OCR 解析优化——抖音热榜解析条数 1→4~6 条:

- 行合并预处理(OCR 拆行修复:排名/标题/热度三行拼回一条)
- 排名容错(无空格/前缀清洗/时间戳拒绝/首字符校验)
- 热度容错(无空格提取)+ 标题热度清理 + 噪音过滤
- 真机沙盒实测解析条数 1→6 条,时间戳误匹配清零
- 315 项 JVM 全过(新增 12 项 OCR 容错用例)

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-11-17 16:43:41 +08:00
co-authored by Claude Code
parent 39913c6453
commit 9a078a24d1
4 changed files with 188 additions and 14 deletions
@@ -354,11 +354,13 @@ internal class DouyinDevicePort(
run {
val lines = ocrCurrentScreen()
collected.addAll(lines)
val parsed = DouyinHotListParser.parse(collected.toList())
val merged = com.haorui.app.crossplatform.source.douyin.DouyinHotListParser.mergeAdjacentLines(collected.toList())
val parsed = com.haorui.app.crossplatform.source.douyin.DouyinHotListParser.parse(collected.toList())
bestParsed = maxOf(bestParsed, parsed.size)
log("帧追赶第 0 屏(直读):${lines.size} 行,累计 ${collected.size},解析 ${bestParsed} 条")
// 诊断:打印前 12 行 OCR 文本(定位解析失败原因)
log("OCR 前 12 行: ${lines.take(12).joinToString(" | ")}")
log("帧追赶第 0 屏(直读):${lines.size} 行,合并后 ${merged.size} 行,解析 ${bestParsed} 条")
// 诊断:打印合并后含数字的行(定位解析失败原因)
val digitLines = merged.filter { it.any { c -> c.isDigit() } }
log("合并后含数字行(${digitLines.size} 条): ${digitLines.take(10).joinToString(" | ")}")
}
var noFreshRounds = 0
for (round in 1..maxRounds) {
@@ -27,22 +27,83 @@ object DouyinHotListParser {
/** e1 实测标签词全集;未核验的新标签词不剥离,留在标题里待取证后补充。 */
internal val LABELS = setOf("热", "新", "挑战", "辟谣")
private val RANK_PREFIX = Regex("^(\\d{1,2})\\s+(.+)$")
private val HEAT_SUFFIX = Regex("[\\s]+([\\d]+(?:\\.[\\d]+)?)万\\s*$")
private val RANK_PREFIX = Regex("^(\\d{1,2})\\s*(.+)$")
private val HEAT_SUFFIX = Regex("[\\s]*([\\d]+(?:\\.[\\d]+)?)万\\s*$")
/** 解析 OCR 文本行为热榜条目;无条目返回空表,噪音行静默跳过。 */
fun parse(ocrLines: List<String>): List<DouyinHotEntry> =
ocrLines.mapIndexedNotNull { index, line -> parseLine(line, index) }
fun parse(ocrLines: List<String>): List<DouyinHotEntry> {
val merged = mergeAdjacentLines(ocrLines)
return merged.mapIndexedNotNull { index, line -> parseLine(line, index) }
}
/**
* OCR 行合并预处理:热榜一行「排名+标题+热度」常被 OCR 拆成 2~3 行
* (排名/标题/热度各一行),单独每行都匹配不到 RANK_PREFIX+标题+热度 → 全被拒。
* 合并规则(保守,只合并确定性的拆行形态):
* 1) 纯排名行(`^\d{1,2}$`)+ 纯标题行(无排名无热度)→ 拼成「排名 标题」;
* 2) 上一步结果 + 纯热度行(`^\d+(\.\d+)?万$`)→ 拼成「排名 标题 热度」;
* 3) 纯热度行紧跟已有条目行(含排名)→ 追加到该行末尾。
* 不确定的行原样保留(交给 parseLine 按原有规则判定)。
*/
internal fun mergeAdjacentLines(ocrLines: List<String>): List<String> {
val result = mutableListOf<String>()
var i = 0
while (i < ocrLines.size) {
val line = ocrLines[i].trim()
if (line.isEmpty()) { i++; continue }
// 纯排名行(1~2 位数字)
if (RANK_ONLY.matches(line)) {
val titleCandidate = ocrLines.getOrNull(i + 1)?.trim().orEmpty()
if (titleCandidate.isNotEmpty() && !RANK_ONLY.matches(titleCandidate) && !HEAT_ONLY.matches(titleCandidate)) {
var merged = "$line $titleCandidate"
var next = i + 2
// 紧跟纯热度行则追加
val heatCandidate = ocrLines.getOrNull(next)?.trim().orEmpty()
if (HEAT_ONLY.matches(heatCandidate)) {
merged = "$merged $heatCandidate"
next++
}
result += merged
i = next
continue
}
}
// 纯热度行紧跟已有条目行(含排名)→ 追加到上一行
if (HEAT_ONLY.matches(line) && result.isNotEmpty() && RANK_PREFIX.matches(result.last())) {
result[result.size - 1] = result.last() + " " + line
i++
continue
}
result += line
i++
}
return result
}
private val RANK_ONLY = Regex("^\\d{1,2}$")
private val HEAT_ONLY = Regex("^\\d+(?:\\.\\d+)?万$")
internal fun parseLine(rawLine: String, order: Int): DouyinHotEntry? {
val line = rawLine.trim()
var line = rawLine.trim()
if (line.isEmpty()) return null
// 0) 清洗排名前非数字前缀(OCR 常把 "|"、"。" 等混入排名前)
line = line.trimStart('|', '。', '·', '、', ' ', ' ')
var rest = line
// 1) 行首排名:1~2 位数字 + 空白。置顶/官宣行无排名。
// 1) 行首排名:1~2 位数字 + 可选空白。置顶/官宣行无排名。
// 标题首字符必须非数字(防「2026年APEC…」误匹配排名「2」)。
// 标题首字符不能是「:」(防时间戳「16:06」误匹配排名「16」)。
val rank = RANK_PREFIX.matchEntire(rest)?.let { m ->
rest = m.groupValues[2]
m.groupValues[1].toIntOrNull()?.takeIf { it in 1..99 }
val titlePart = m.groupValues[2]
if (titlePart.isEmpty() || titlePart[0].isDigit() || titlePart[0] == ':') null
else {
rest = titlePart
m.groupValues[1].toIntOrNull()?.takeIf { it in 1..99 }
}
}
// 2) 行尾热度:「数值+万」。纯数值不带万不认。
@@ -61,14 +122,16 @@ object DouyinHotListParser {
if (label == null && word in LABELS) { label = word; false } else true
}
val title = titleWords.joinToString(" ").trim()
if (title.isEmpty()) return null
// 标题最短 2 字符(防单字符噪音如「G」);标题中残留热度样式文本清理
val cleanedTitle = title.replace(Regex("\\d+(?:\\.\\d+)?万"), "").trim()
if (cleanedTitle.length < 2) return null
// 无排名:正常榜单行必有排名徽章(e1 实测);置顶行/官宣行/搜索历史等均无排名 → 拒绝。
// 有热度无排名的行同样视为噪音(OCR 混入的非榜单内容),宁缺不滥。
if (rank == null) return null
return DouyinHotEntry(
title = title,
title = cleanedTitle,
heatRaw = heatRaw,
heatValue = heatValue,
rank = rank,