英文:
How to make filter and word replacement method for android studio
问题
I understand your request. Here's the translated content:
欢迎。我正在制作一个应用程序,用于识别声音并将其转换为文本,然后进行比较和校正。到目前为止,从声音转换为文本并进行比较的部分都运行得很完美,但有一些词汇我想要更改或修改它们的措辞...我尝试将它们放在一个if语句下,就像代码的某个地方一样,它在应用程序方面运作良好,但会显著影响性能,所以我想知道是否可以创建类似过滤器的东西,其中包含大量的词汇,并且同时保持应用程序的良好性能。
我所做的一个示例:
如果(inputText.getText.toString.contains("A Specific Word ")) {
String before ;
String after ;
before = inputText.getText.toString;
after = before.replaceAll("A Specific Word", "ASW" );
inputText.setText(after);
}
但这会导致性能问题。
英文:
Welcome,. I am making an application to recognize the voice and convert it to text and then compare and correct it, so far everything from converting voice to text and comparing it works perfectly, but there are some words that I want to change or change their wording ... I tried to put it under an if statement As in the place of the code, and it was working well in terms of application, but it affects performance significantly So I was asking if I could make something like a filter so that it contains a large group of words and at the same time maintains a good performance for the application.
An example of what i did :
If(inputText.getText.toString.contains("A Specific Word ")){
String before ;
String after ;
before = inputText.getText.toString;
after = before.replaceAll("A Specific Word", "ASW" );
inputText.setText(after);
}
But this Causes performance problems.
答案1
得分: 0
我认为您可以通过使用以下代码简单地加以改进:
try {
String sentence = inputText.getText().toString();
inputText.setText(sentence.replaceAll("Hello", "Hi")); // 用您的词语进行替换
} catch (Exception e) {
System.out.println("找不到 Hello");
}
使用 try-catch 语句,它不会检查该词是否存在,但如果不存在,程序也会继续执行并安全地跳过该语句。不同之处在于:我们不会在句子中找到所需的单词,这将节省时间,特别是在较长的句子中。
英文:
I think you can simply enhance it by usingthis code:
try{
String sentence = inputText.getText().toString();
inputText.setText(sentence.replaceall("Hello", "Hi")); // Replace with Your words
}catch(Exception e){
System.out.println("Couldn't find Hello");
}
Using try-catch statements it will not check if the word is in there but if it is not there, then also it will continue and will safely pass over the statement.
What is different?
That we are not finding the required word in the sentence which will save time will longer sentences.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论