WhisperLiveKit 转写结果过滤方法
基于置信度阈值过滤
检查转写结果的置信度分数(confidence score),设定阈值(如低于0.5)直接丢弃低置信度片段。可通过Whisper API返回的confidence字段实现:
def filter_by_confidence(transcript, threshold=0.5):
return [seg for seg in transcript if seg.get('confidence', 0) >= threshold]
静音与非语音片段剔除
利用音频能量检测或Whisper返回的no_speech_prob字段。若该值超过0.8(示例值),判定为非语音内容:
def remove_silence(transcript, speech_threshold=0.8):
return [seg for seg in transcript if seg.get('no_speech_prob', 1) <= speech_threshold]
无效文本模式识别
正则表达式匹配垃圾内容
针对常见无效模式(如重复字符、无意义词)编写正则规则:
import re
def clean_garbage(text):
patterns = [
r'[\*\.\_]{3,}', # 连续符号
r'\b(uh|um|ah)\b', # 填充词
r'^[\W\d]+$' # 纯符号/数字
]
for pat in patterns:
text = re.sub(pat, '', text)
return text.strip()
语言模型辅助过滤
使用轻量级语言模型(如FastText)计算文本连贯性得分,剔除低分片段:
from fasttext import load_model
ft_model = load_model('lid.176.bin')
def is_valid_text(text, min_coherence=0.7):
words = text.split()
if len(words) < 3: return False
return ft_model.predict(text)[1][0] > min_coherence
实时流处理优化
滑动窗口内容验证
在流式场景中维护一个滑动窗口(如最近5秒内容),当窗口内无效片段占比超过50%时触发清理:
from collections import deque
window = deque(maxlen=5) # 5个片段窗口
def stream_filter(segment):
window.append(segment)
if sum(1 for s in window if not is_valid_text(s['text'])) / len(window) > 0.5:
window.clear() # 重置污染窗口
return None
return segment
上下文关联性检查
通过BLEU分数或编辑距离验证当前片段与历史内容的关联性:
from nltk.translate.bleu_score import sentence_bleu
def context_check(new_seg, history, min_bleu=0.3):
refs = [h['text'].split() for h in history[-3:]]
score = sentence_bleu(refs, new_seg['text'].split())
return score >= min_bleu
性能优化建议
- 对短文本(<3词)直接跳过深度处理
- 使用缓存机制存储最近的有效词汇表
- 并行化处理流水线(如Confidence过滤与语言校验同步执行)
注:具体阈值需通过实际数据测试调整,建议使用混淆矩阵评估过滤效果。
WhisperLiveKit转写过滤方案

被折叠的 条评论
为什么被折叠?



