Files
douyi_guaji_main/douyin_text_utils.py
T
Your Name 499002d3b1 fix: Clean up blank lines and enhance readability across multiple files
- Removed excessive blank lines from various markdown and Python files to improve readability and maintainability.
- Ensured consistent formatting across documentation and code files, enhancing overall project clarity.
- Updated logging mechanisms for better consistency and debugging support.
2025-12-26 21:59:54 +08:00

154 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""福袋相关文本解析辅助函数。
本模块提供若干轻量级的解析函数,主要用于:
- 从 OCR 文本中抽取福袋内容/倒计时/价值/参与人数
- 方便在单元测试中独立验证文本处理逻辑
"""
from __future__ import annotations
from typing import Optional
import re
from douyin_utils import normalize_countdown_text, extract_prize_value_yuan
def _safe_str(text: Optional[str]) -> str:
"""将输入安全地转换为字符串,None -> 空字符串。"""
if text is None:
return ""
return str(text)
def extract_fudai_content(text: Optional[str]) -> str:
"""从 OCR 文本中提取“福袋内容”相关描述。
当前实现比较宽松:
- 优先截取 “福袋内容:xxx” 或 “内容:xxx” 之后的部分
- 否则返回原始文本(去掉首尾空白)
"""
t = _safe_str(text).strip()
if not t:
return ""
# 常见前缀:福袋内容:xxx / 奖品内容:xxx / 内容:xxx
m = re.search(r"(福袋内容|奖品内容|内容)[::]\s*(.+)", t)
if m:
return m.group(2).strip()
# 如果包含“福袋”关键字,简单返回整句,后续需要时可再细化
if "福袋" in t:
return t
return t
def extract_fudai_countdown(text: Optional[str]) -> str:
"""从 OCR 文本中提取倒计时字符串。
返回值示例:
- ``"05:30"`` / ``"05:30后开奖"``
- 若无法解析则返回空字符串。
"""
t = _safe_str(text)
if not t:
return ""
norm = normalize_countdown_text(t)
return norm or ""
def extract_fudai_value(text: Optional[str]) -> float:
"""从 OCR 文本中提取福袋价值金额(元)。
直接复用 ``douyin_utils.extract_prize_value_yuan``。
"""
return float(extract_prize_value_yuan(_safe_str(text)))
def extract_participants_count(text: Optional[str]) -> int:
"""从 OCR 文本中提取“参与人数”。
支持的常见格式示例:
- ``"共1份1173人已参与"``
- ``"已有1000人参与"``
- ``"1173人已参与"``
- 若无法解析则返回 0。
"""
t = _safe_str(text)
if not t:
return 0
# 优先匹配“xxxx人已参与 / xxxx人参与 / xxxx人”
m = re.search(r"(\d+)\s*人(?:已参与|参与)?", t)
if m:
try:
return int(m.group(1))
except ValueError:
return 0
# 退化:查找所有数字,取中等大小的一个,避免把“份数”等误当成人数
nums = [int(n) for n in re.findall(r"\d+", t) if n.isdigit()]
if not nums:
return 0
# 简化策略:返回最大值(通常人数是文本里最大的数字)
return max(nums)