AWS Polly处理含特殊字符文本时字节偏移转字符偏移问题咨询
AWS Polly字节偏移转字符偏移实现文本准确高亮的方案
问题根源
AWS Polly返回的start、end字段为UTF-8字节偏移量,而Swift中字符串的字符计数、文本高亮所用的NSRange分别基于Unicode字符、UTF-16编码,遇到多字节Unicode字符(比如示例中的ö占2个UTF-8字节)时直接使用字节偏移必然出现错位。
解决方案
步骤1:新增字符串偏移转换工具
给Swift的String加扩展,实现UTF-8字节偏移到字符索引的转换:
extension String { /// 输入UTF8字节偏移,返回对应的字符索引 func characterIndex(from byteOffset: Int) -> String.Index? { guard byteOffset >= 0, let utf8Index = utf8.index(utf8.startIndex, offsetBy: byteOffset, limitedBy: utf8.endIndex) else { return nil } // 把UTF8视图的索引转成普通字符索引 return String.Index(utf8Index, within: self) } }
步骤2:改造高亮逻辑
核心是先通过原始输入文本(传给Polly的完整未修改文本,假设变量名为originalInputText)把字节偏移转成字符维度的范围,再转为控件可用的NSRange:
// 1. 把Polly返回的字节偏移转成字符索引 guard let startCharIndex = originalInputText.characterIndex(from: Int(pollyVoiceSentence.start)), let endCharIndex = originalInputText.characterIndex(from: Int(pollyVoiceSentence.end)) else { // 偏移异常的兜底处理 return } // 2. 构造字符维度的Range let charRange = startCharIndex..<endCharIndex // 3. 转成NSRange用于UILabel等控件的属性文本高亮 let voiceRange = NSRange(charRange, in: originalInputText) print("RANGE: \(voiceRange) - Word: \(pollyVoiceSentence.value)")
可选优化:解析阶段预转换偏移
如果不想每次高亮都重复转换,可以在解析Polly返回结果时直接把字节偏移转成字符偏移存入模型,需要先给PollyVoiceSentence新增charStart、charEnd两个Int属性:
builder.continueOnSuccessWith { (awsTask: AWSTask<NSURL>) -> Any? in if builder.error == nil { if let url = awsTask.result { do { let txtData = try Data(contentsOf: url as URL) if let txtString = String(data: txtData, encoding: .utf8) { let lines = txtString.components(separatedBy: .newlines) for line in lines { let jsonData = Data(line.utf8) let pollyVoiceSentence = try JSONDecoder().decode(PollyVoiceSentence.self, from: jsonData) // 预转换为字符偏移 if let startIdx = originalInputText.characterIndex(from: Int(pollyVoiceSentence.start)), let endIdx = originalInputText.characterIndex(from: Int(pollyVoiceSentence.end)) { pollyVoiceSentence.charStart = originalInputText.distance(from: originalInputText.startIndex, to: startIdx) pollyVoiceSentence.charEnd = originalInputText.distance(from: originalInputText.startIndex, to: endIdx) } voiceSentences.append(pollyVoiceSentence) } } } catch { print("Could not parse TXT file") } } } else { print("ParseJSON: \(builder.error!)") } completionHandler(voiceSentences) return nil }
注意事项
- 必须严格保留传给Polly的原始输入文本,不能做任何修改、裁剪、转义操作,否则偏移会完全错位
- 转换方法兼容所有Unicode字符,包括特殊字符、emoji、东亚文字等场景
内容的提问来源于stack exchange,提问作者sametbilgi
相关产品推荐
相关产品推荐

