Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
xuning
/
sherpaonnx
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Fangjun Kuang
2025-05-14 10:58:35 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2025-05-14 10:58:35 +0800
Commit
0dfafed7d0faa263602040cff5bf67f408924d6f
0dfafed7
1 parent
fdda292d
Support homophone replacer in Android asr demo. (#2210)
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
76 行增加
和
2 行删除
android/SherpaOnnx/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt
sherpa-onnx/csrc/homophone-replacer.cc
android/SherpaOnnx/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt
查看文件 @
0dfafed
...
...
@@ -12,6 +12,9 @@ import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import kotlin.concurrent.thread
private const val TAG = "sherpa-onnx"
...
...
@@ -199,8 +202,22 @@ class MainActivity : AppCompatActivity() {
var ruleFsts : String?
ruleFsts = null
val useHr = false
val hr = HomophoneReplacerConfig(
// Used only when useHr is true
// Please download the following 3 files from
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/hr-files
//
// dict and lexicon.txt can be shared by different apps
//
// replace.fst is specific for an app
dictDir = "dict",
lexicon = "lexicon.txt",
ruleFsts = "replace.fst",
)
Log.i(TAG, "Select model type $type")
va
l
config = OnlineRecognizerConfig(
va
r
config = OnlineRecognizerConfig(
featConfig = getFeatureConfig(sampleRate = sampleRateInHz, featureDim = 80),
modelConfig = getModelConfig(type = type)!!,
// lmConfig = getOnlineLMConfig(type = type),
...
...
@@ -212,9 +229,66 @@ class MainActivity : AppCompatActivity() {
config.ruleFsts = ruleFsts
}
if (useHr) {
if (hr.dictDir.isNotEmpty() && hr.dictDir.first() != '/') {
// We need to copy it from the assets directory to some path
val newDir = copyDataDir(hr.dictDir)
hr.dictDir = "$newDir/${hr.dictDir}"
}
config.hr = hr
}
recognizer = OnlineRecognizer(
assetManager = application.assets,
config = config,
)
}
private fun copyDataDir(dataDir: String): String {
Log.i(TAG, "data dir is $dataDir")
copyAssets(dataDir)
val newDataDir = application.getExternalFilesDir(null)!!.absolutePath
Log.i(TAG, "newDataDir: $newDataDir")
return newDataDir
}
private fun copyAssets(path: String) {
val assets: Array<String>?
try {
assets = application.assets.list(path)
if (assets!!.isEmpty()) {
copyFile(path)
} else {
val fullPath = "${application.getExternalFilesDir(null)}/$path"
val dir = File(fullPath)
dir.mkdirs()
for (asset in assets.iterator()) {
val p: String = if (path == "") "" else path + "/"
copyAssets(p + asset)
}
}
} catch (ex: IOException) {
Log.e(TAG, "Failed to copy $path. $ex")
}
}
private fun copyFile(filename: String) {
try {
val istream = application.assets.open(filename)
val newFilename = application.getExternalFilesDir(null).toString() + "/" + filename
val ostream = FileOutputStream(newFilename)
// Log.i(TAG, "Copying $filename to $newFilename")
val buffer = ByteArray(1024)
var read = 0
while (read != -1) {
ostream.write(buffer, 0, read)
read = istream.read(buffer)
}
istream.close()
ostream.flush()
ostream.close()
} catch (ex: Exception) {
Log.e(TAG, "Failed to copy $filename, $ex")
}
}
}
...
...
sherpa-onnx/csrc/homophone-replacer.cc
查看文件 @
0dfafed
...
...
@@ -296,7 +296,7 @@ HomophoneReplacer::HomophoneReplacer(Manager *mgr,
HomophoneReplacer
::~
HomophoneReplacer
()
=
default
;
std
::
string
HomophoneReplacer
::
Apply
(
const
std
::
string
&
text
)
const
{
return
impl_
->
Apply
(
text
);
return
RemoveInvalidUtf8Sequences
(
impl_
->
Apply
(
text
)
);
}
#if __ANDROID_API__ >= 9
...
...
请
注册
或
登录
后发表评论