Browse Source

fix(text-processor): preserve leading/trailing spaces in live speech chunks and punctuation (v2.14)

main
Ali Alavi 2 weeks ago
parent
commit
f28e74e98b
  1. 2
      Info.plist
  2. 32
      src/TextProcessor.swift

2
Info.plist

@ -19,7 +19,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.13</string>
<string>2.14</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1</string> <string>1</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>

32
src/TextProcessor.swift

@ -74,22 +74,26 @@ public final class TextProcessor {
/// Lightweight inline formatter applied to live streaming chunks so words, domain terms, /// Lightweight inline formatter applied to live streaming chunks so words, domain terms,
/// and numbers match the final post-processed text in real time. /// and numbers match the final post-processed text in real time.
public func processLiveChunk(_ text: String) -> String { public func processLiveChunk(_ text: String) -> String {
if text.isEmpty { return "" }
let hasLeadingSpace = text.first?.isWhitespace == true
let hasTrailingSpace = text.last?.isWhitespace == true
if text.isEmpty { return "" }
let hadLeadingSpace = text.first?.isWhitespace == true
let hadTrailingSpace = 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)
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 ? " " : "")
let resultHasLeadingSpace = result.first?.isWhitespace == true
let resultHasTrailingSpace = result.last?.isWhitespace == true
let trimmed = result.trimmingCharacters(in: .whitespaces)
if trimmed.isEmpty { return text }
let keepLeading = hadLeadingSpace || resultHasLeadingSpace
let keepTrailing = hadTrailingSpace || resultHasTrailingSpace
return (keepLeading ? " " : "") + trimmed + (keepTrailing ? " " : "")
} }
public func applyDomainVocabularyInline(_ text: String) -> String { public func applyDomainVocabularyInline(_ text: String) -> String {

Loading…
Cancel
Save