|
|
@ -48,15 +48,15 @@ public final class TextProcessor { |
|
|
if SonioxSettings.shared.voicePunctuation { |
|
|
if SonioxSettings.shared.voicePunctuation { |
|
|
result = applyVoicePunctuation(result) |
|
|
result = applyVoicePunctuation(result) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Domain Vocabulary Enhancement |
|
|
// 2. Domain Vocabulary Enhancement |
|
|
if SonioxSettings.shared.domainVocabulary { |
|
|
if SonioxSettings.shared.domainVocabulary { |
|
|
result = applyDomainVocabulary(result) |
|
|
result = applyDomainVocabulary(result) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Number Formatting |
|
|
// 3. Number Formatting |
|
|
result = applyNumberFormatting(result, format: SonioxSettings.shared.numberFormat) |
|
|
result = applyNumberFormatting(result, format: SonioxSettings.shared.numberFormat) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3.5. Sanitize foreign hallucinations & word loops |
|
|
// 3.5. Sanitize foreign hallucinations & word loops |
|
|
result = SonioxLiveSession.sanitizeText(result) |
|
|
result = SonioxLiveSession.sanitizeText(result) |
|
|
|
|
|
|
|
|
@ -64,14 +64,35 @@ public final class TextProcessor { |
|
|
if SonioxSettings.shared.normalizeText { |
|
|
if SonioxSettings.shared.normalizeText { |
|
|
result = normalizeText(result) |
|
|
result = normalizeText(result) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Wake Word & End Trigger sanitization |
|
|
// 5. Wake Word & End Trigger sanitization |
|
|
result = stripTrailingEndTrigger(result) |
|
|
result = stripTrailingEndTrigger(result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result.trimmingCharacters(in: .whitespacesAndNewlines) |
|
|
return result.trimmingCharacters(in: .whitespacesAndNewlines) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public func applyDomainVocabularyInline(_ text: String) -> String { |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Lightweight inline formatter applied to live streaming chunks so words, domain terms, |
|
|
|
|
|
/// and numbers match the final post-processed text in real time. |
|
|
|
|
|
public func processLiveChunk(_ text: String) -> String { |
|
|
|
|
|
if text.isEmpty { return "" } |
|
|
|
|
|
let hasLeadingSpace = text.first?.isWhitespace == true |
|
|
|
|
|
let hasTrailingSpace = text.last?.isWhitespace == true |
|
|
|
|
|
|
|
|
|
|
|
var result = text |
|
|
|
|
|
if SonioxSettings.shared.voicePunctuation { |
|
|
|
|
|
result = applyVoicePunctuation(result) |
|
|
|
|
|
} |
|
|
|
|
|
if SonioxSettings.shared.domainVocabulary { |
|
|
|
|
|
result = applyDomainVocabulary(result) |
|
|
|
|
|
} |
|
|
|
|
|
result = applyNumberFormatting(result, format: SonioxSettings.shared.numberFormat) |
|
|
|
|
|
|
|
|
|
|
|
let trimmed = result.trimmingCharacters(in: .whitespaces) |
|
|
|
|
|
if trimmed.isEmpty { return text } |
|
|
|
|
|
return (hasLeadingSpace ? " " : "") + trimmed + (hasTrailingSpace ? " " : "") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public func applyDomainVocabularyInline(_ text: String) -> String { |
|
|
return applyDomainVocabulary(text) |
|
|
return applyDomainVocabulary(text) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -220,7 +241,6 @@ public final class TextProcessor { |
|
|
(" شصت ", " 60 "), (" هفتاد ", " 70 "), (" هشتاد ", " 80 "), (" نود ", " 90 "), |
|
|
(" شصت ", " 60 "), (" هفتاد ", " 70 "), (" هشتاد ", " 80 "), (" نود ", " 90 "), |
|
|
(" صد ", " 100 "), (" دویست ", " 200 "), (" سیصد ", " 300 "), (" چهارصد ", " 400 "), |
|
|
(" صد ", " 100 "), (" دویست ", " 200 "), (" سیصد ", " 300 "), (" چهارصد ", " 400 "), |
|
|
(" پانصد ", " 500 "), (" ششصد ", " 600 "), (" هفتصد ", " 700 "), (" هشتصد ", " 800 "), (" نهصد ", " 900 "), |
|
|
(" پانصد ", " 500 "), (" ششصد ", " 600 "), (" هفتصد ", " 700 "), (" هشتصد ", " 800 "), (" نهصد ", " 900 "), |
|
|
(" هزار ", " 1000 "), (" میلیون ", " 1000000 "), |
|
|
|
|
|
(" صفر ", " 0 "), (" یک ", " 1 "), (" دو ", " 2 "), (" سه ", " 3 "), (" چهار ", " 4 "), |
|
|
(" صفر ", " 0 "), (" یک ", " 1 "), (" دو ", " 2 "), (" سه ", " 3 "), (" چهار ", " 4 "), |
|
|
(" پنج ", " 5 "), (" شش ", " 6 "), (" هفت ", " 7 "), (" هشت ", " 8 "), (" نه ", " 9 ") |
|
|
(" پنج ", " 5 "), (" شش ", " 6 "), (" هفت ", " 7 "), (" هشت ", " 8 "), (" نه ", " 9 ") |
|
|
] |
|
|
] |
|
|
@ -262,9 +282,12 @@ public final class TextProcessor { |
|
|
t = t.replacingOccurrences(of: " " + p, with: p) |
|
|
t = t.replacingOccurrences(of: " " + p, with: p) |
|
|
} |
|
|
} |
|
|
t = t.replacingOccurrences(of: "...", with: "…") |
|
|
t = t.replacingOccurrences(of: "...", with: "…") |
|
|
for p in [".", "،", "؛", "؟", "!", "…"] { |
|
|
|
|
|
|
|
|
for p in ["،", "؛", "؟", "!", "…"] { |
|
|
t = t.replacingOccurrences(of: p, with: p + " ") |
|
|
t = t.replacingOccurrences(of: p, with: p + " ") |
|
|
} |
|
|
} |
|
|
|
|
|
// Only add space after "." when it is NOT between two digits (preserve 4.3, 3.8, IP addresses) |
|
|
|
|
|
let dotNotDecimal = try! NSRegularExpression(pattern: #"(\.)(?![0-9۰-۹\s\.])"#, options: []) |
|
|
|
|
|
t = dotNotDecimal.stringByReplacingMatches(in: t, options: [], range: NSRange(location: 0, length: t.utf16.count), withTemplate: ". ") |
|
|
t = t.replacingOccurrences(of: "…", with: "...") |
|
|
t = t.replacingOccurrences(of: "…", with: "...") |
|
|
let speechFixes: [(String, String)] = [ |
|
|
let speechFixes: [(String, String)] = [ |
|
|
(" ماا ", " ما "), |
|
|
(" ماا ", " ما "), |
|
|
|