|
|
|
@ -34,6 +34,7 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
private var lastIntermediateResult = "" |
|
|
|
|
|
|
|
private var currentLanguage = "zh-CN" |
|
|
|
private var supportedLanguages: List<String> = emptyList() |
|
|
|
private var isAutoDetectLanguage = false |
|
|
|
|
|
|
|
// 是否第去除一个文字的标点符号 |
|
|
|
@ -58,7 +59,13 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
return appId.isNotBlank() && accessKeyId.isNotBlank() && accessKeySecret.isNotBlank() |
|
|
|
} |
|
|
|
|
|
|
|
fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback, language: String, isAutoDetect: Boolean, isRemoveFirstPunctuation: Boolean) { |
|
|
|
fun start( |
|
|
|
callback: AzureAsrHelper.ContinuousRecognizeCallback, |
|
|
|
language: String, |
|
|
|
supportedLanguages: List<String>, |
|
|
|
isAutoDetect: Boolean, |
|
|
|
isRemoveFirstPunctuation: Boolean |
|
|
|
) { |
|
|
|
try { |
|
|
|
webSocket?.cancel() |
|
|
|
webSocket = null |
|
|
|
@ -70,6 +77,7 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
lastIntermediateResult = "" |
|
|
|
|
|
|
|
currentLanguage = language |
|
|
|
this.supportedLanguages = supportedLanguages |
|
|
|
isAutoDetectLanguage = isAutoDetect |
|
|
|
removeFirstPunctuation = isRemoveFirstPunctuation |
|
|
|
|
|
|
|
@ -99,15 +107,7 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { |
|
|
|
Log.d(tag, "WebSocket Closed: $code / $reason") |
|
|
|
if (currentSessionId.isNotEmpty() && lastIntermediateResult.isNotEmpty()) { |
|
|
|
val detectedLanguage = if (isAutoDetectLanguage) { |
|
|
|
if (lastIntermediateResult.contains(Regex("[\\u4e00-\\u9fa5]"))) { |
|
|
|
"zh-CN" |
|
|
|
} else { |
|
|
|
"en-US" |
|
|
|
} |
|
|
|
} else { |
|
|
|
currentLanguage |
|
|
|
} |
|
|
|
val detectedLanguage = selectDetectedLanguage(lastIntermediateResult) |
|
|
|
callback.onResult(currentSessionId, lastIntermediateResult, detectedLanguage) |
|
|
|
} |
|
|
|
callback.onSessionStopped(currentSessionId) |
|
|
|
@ -219,15 +219,7 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
val detectedLanguage = if (isAutoDetectLanguage) { |
|
|
|
if (resultTextTemp.contains(Regex("[\\u4e00-\\u9fa5]"))) { |
|
|
|
"zh-CN" |
|
|
|
} else { |
|
|
|
"en-US" |
|
|
|
} |
|
|
|
} else { |
|
|
|
currentLanguage |
|
|
|
} |
|
|
|
val detectedLanguage = selectDetectedLanguage(resultTextTemp) |
|
|
|
|
|
|
|
if (st.optInt("type") == 0) { |
|
|
|
callback.onResult(currentSessionId, resultTextTemp, detectedLanguage) |
|
|
|
@ -254,6 +246,52 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private fun normalizeLanguageCode(code: String): String { |
|
|
|
val trimmed = code.trim() |
|
|
|
if (trimmed.isEmpty()) return trimmed |
|
|
|
|
|
|
|
val parts = trimmed.split("-") |
|
|
|
val lang = parts.getOrNull(0)?.lowercase() ?: trimmed.lowercase() |
|
|
|
val region = parts.getOrNull(1)?.uppercase() |
|
|
|
return if (region.isNullOrEmpty()) lang else "$lang-$region" |
|
|
|
} |
|
|
|
|
|
|
|
private fun findSupportedExact(target: String): String? { |
|
|
|
val targetNorm = normalizeLanguageCode(target) |
|
|
|
return supportedLanguages.firstOrNull { normalizeLanguageCode(it) == targetNorm } |
|
|
|
} |
|
|
|
|
|
|
|
private fun findSupportedPrefix(prefix: String): String? { |
|
|
|
val p = prefix.lowercase() |
|
|
|
return supportedLanguages.firstOrNull { normalizeLanguageCode(it).startsWith(p) } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun pickPreferredChineseLanguage(): String { |
|
|
|
return findSupportedExact("zh-CN") |
|
|
|
?: findSupportedExact("zh-HK") |
|
|
|
?: findSupportedExact("zh-TW") |
|
|
|
?: findSupportedPrefix("zh") |
|
|
|
?: currentLanguage |
|
|
|
} |
|
|
|
|
|
|
|
private fun pickPreferredEnglishLanguage(): String { |
|
|
|
return findSupportedExact("en-US") |
|
|
|
?: findSupportedPrefix("en") |
|
|
|
?: currentLanguage |
|
|
|
} |
|
|
|
|
|
|
|
// 选择检测到的语言 |
|
|
|
// 如果开启了自动检测语言,根据文本内容判断是否为中文 |
|
|
|
// 如果是中文,选择中文语言;否则选择英文语言 |
|
|
|
// 如果未开启自动检测语言,返回当前语言 |
|
|
|
private fun selectDetectedLanguage(text: String): String { |
|
|
|
if (!isAutoDetectLanguage) return currentLanguage |
|
|
|
|
|
|
|
val hasChinese = text.contains(Regex("[\\u4e00-\\u9fa5]")) |
|
|
|
return if (hasChinese) pickPreferredChineseLanguage() else pickPreferredEnglishLanguage() |
|
|
|
} |
|
|
|
|
|
|
|
private fun getWebSocketUrl(): String { |
|
|
|
val baseWsUrl = "wss://office-api-ast-dx.iflyaisol.com/ast/communicate/v1" |
|
|
|
|
|
|
|
|