improve hallucination filter and integrate with parakeet transcription service

This commit is contained in:
Beingpax 2025-09-06 16:56:28 +05:45
parent 8b54be3019
commit 53d1507a53
2 changed files with 35 additions and 30 deletions

View File

@ -137,10 +137,16 @@ class ParakeetTranscriptionService: TranscriptionService {
logger.notice("🦜 Warning: Empty transcription result for \(audioSamples.count) samples - possible vocabulary issue")
}
var text = result.text
if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true {
return WhisperTextFormatter.format(result.text)
text = WhisperTextFormatter.format(text)
}
return result.text
// Apply hallucination and filler word filtering
text = WhisperHallucinationFilter.filter(text)
return text
}
private func readAudioSamples(from url: URL) throws -> [Float] {

View File

@ -4,49 +4,48 @@ import os
struct WhisperHallucinationFilter {
private static let logger = Logger(subsystem: "com.prakashjoshipax.voiceink", category: "WhisperHallucinationFilter")
// Pattern-based approach for detecting hallucinations - focusing on format indicators
private static let hallucinationPatterns = [
// Text in various types of brackets - the most reliable hallucination indicators
#"\[.*?\]"#, // [Text in square brackets]
#"\(.*?\)"#, // (Text in parentheses)
#"\{.*?\}"#, // {Text in curly braces}
#"<.*?>"#, // <Text in angle brackets>
// Text with special formatting
#"\*.*?\*"#, // *Text with asterisks*
#"_.*?_"#, // _Text with underscores_
// Time indicators often added by Whisper
#"(?i)\d{1,2}:\d{2}(:\d{2})?\s*-\s*\d{1,2}:\d{2}(:\d{2})?"# // 00:00 - 00:00 format
#"\[.*?\]"#, // Square brackets
#"\(.*?\)"#, // Parentheses
#"\{.*?\}"# // Curly braces
]
private static let fillerWords = [
"uh", "um", "uhm", "umm", "uhh", "uhhh", "er", "ah", "eh",
"hmm", "hm", "h", "m", "mmm", "mm", "mh", "ha", "ehh"
]
/// Removes hallucinations from transcription text using pattern matching
/// - Parameter text: Original transcription text from Whisper
/// - Returns: Filtered text with hallucinations removed
static func filter(_ text: String) -> String {
logger.notice("🧹 Applying pattern-based hallucination filter to transcription")
logger.notice("🧹 Filtering hallucinations and filler words")
var filteredText = text
// Remove pattern-based hallucinations
// Remove bracketed hallucinations
for pattern in hallucinationPatterns {
if let regex = try? NSRegularExpression(pattern: pattern) {
let range = NSRange(filteredText.startIndex..., in: filteredText)
filteredText = regex.stringByReplacingMatches(in: filteredText, options: [], range: range, withTemplate: "")
}
}
// Clean up extra whitespace and newlines that might be left after removing hallucinations
// Remove filler words
for fillerWord in fillerWords {
let pattern = "\\b\(NSRegularExpression.escapedPattern(for: fillerWord))\\b[,.]?"
if let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) {
let range = NSRange(filteredText.startIndex..., in: filteredText)
filteredText = regex.stringByReplacingMatches(in: filteredText, options: [], range: range, withTemplate: "")
}
}
// Clean whitespace
filteredText = filteredText.replacingOccurrences(of: #"\s{2,}"#, with: " ", options: .regularExpression)
filteredText = filteredText.trimmingCharacters(in: .whitespacesAndNewlines)
// Add logging to track effectiveness
// Log results
if filteredText != text {
logger.notice("✅ Removed hallucinations using pattern matching")
logger.notice("✅ Removed hallucinations and filler words")
} else {
logger.notice("✅ No hallucinations detected with pattern matching")
logger.notice("✅ No hallucinations or filler words found")
}
return filteredText
}
}