文本到语音的发音数字,例如"4th","8ths"或"2nd". [英] Text to speech pronouncing numbers like "4th", "8ths", or "2nd"

查看:69
本文介绍了文本到语音的发音数字,例如"4th","8ths"或"2nd".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我写了一些代码,将Double转换为String,其中该字符串被格式化为可读分数.

A while back I wrote some code that would convert a Double into a String, where the string was formatted as a readable fraction.

例如

4.75 => "4 and 3 4ths"
1.5 => "1 and 1 half"
1.33 => "1 and 1 3rd"

大多数数字都按预期发音,但有一些值得注意的例外.而不是将文本"4ths"发音为"fourths",而是将其发音为"four tee ache ess".这是一个演示此情况的示例.

The majority of numbers are pronounced as intended with a few notable exceptions. Instead of the text "4ths" being pronounced as "fourths" it is pronounced "four tee ache ess". Here is an example demonstrating this.

//this works
tts.speak("1 and 3 fourths", TextToSpeech.QUEUE_FLUSH, null);    
//this works
tts.speak("1 and 1 3rd", TextToSpeech.QUEUE_FLUSH, null); 
//this works
tts.speak("1 and 1 4th", TextToSpeech.QUEUE_FLUSH, null); 

//this does not work
tts.speak("1 and 3 4ths", TextToSpeech.QUEUE_FLUSH, null);
//this does not work
tts.speak("1 and 3 4thes", TextToSpeech.QUEUE_FLUSH, null);
//this does not work
tts.speak("1 and 3 4th-s", TextToSpeech.QUEUE_FLUSH, null);

最奇怪的是,大约一年前,当我第一次编写代码时,它的效果很好,正如人们所期望的那样,"ths"后缀被发音了.也许我在这一点上误会了...

The strangest this is that this worked fine about a year back when I first wrote the code, the "ths" postfix was pronounced as one might expect. Perhaps I am mistaken on that point...

无论如何,问题似乎是,跟着2个字母的数字读起来像是一个完整的单词,而跟着3个或更多字母的数字读起来像是一系列数字.我可以通过将所有数字替换为对应的单词来增加算法的复杂性,但是我从事的时间越长,我就越觉得自己正在重新发明轮子.

Regardless, the issue seems to be that numbers followed by 2 letters are read like a complete word, while numbers followed by 3 or more are read like a series of digits instead. I could add to the complexity of the algorithm by substituting all the numbers for their word counterparts however the longer I work at this the more I begin to think that I am reinventing the wheel. The API did not seem to denote a way of specifying pronunciation for the speak() method. Am I missing something?

推荐答案

TextToSpeech引擎之间的这种行为将有所不同-例如,Google TTS引擎的行为将不同于SVOX PICO(模拟器)< API 24)引擎...因此,每个引擎的行为方式略有不同并不是您的错.如果有任何发音控件,则引擎负责通过设置将其直接提供给最终用户.

This sort of behavior is going to vary between TextToSpeech Engines -- the Google TTS engine, for example, will behave differently than, say, the SVOX PICO (emulator < API 24) engine... so it's not your fault how each engine behaves slightly differently... and if there are any pronunciation controls, then the engine is responsible for supplying them directly to the end user via settings.

您可能只是在测试与以前不同的引擎上...甚至是对同一引擎的更新.

You're probably just testing on a different engine than you were before... or even an update to the same engine.

您可以仅测试一些主要引擎,例如Samsung,Google和PICO,然后尝试找到行为的共同标准.我怀疑您是对的:在这种情况下,拼写单词是最好的选择.

You could just test some major engines like Samsung, Google, and PICO and try to find a common denominator of behavior. I suspect that you're right: spelling out the words is the best option in this case.

您可以指定要用作TextToSpeech构造函数的最后一个参数(字符串)的引擎,并且可以通过转到(home \ settings \ language& locale \ TTS)来查看在任何特定设备上安装了哪些引擎.或像这样的代码:

You can specify what engine you want to use as the last argument (String) of the TextToSpeech constructor, and you can see what engines are installed on any particular device by going to (home\settings\language&locale\TTS) or in code like this:

private ArrayList<String> whatEnginesAreInstalled(Context context) {
    final Intent ttsIntent = new Intent();
    ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    final PackageManager pm = context.getPackageManager();
    final List<ResolveInfo> list = pm.queryIntentActivities(ttsIntent, PackageManager.GET_META_DATA);
    ArrayList<String> installedEngineNames = new ArrayList<>();
    for (ResolveInfo r : list) {
        String engineName = r.activityInfo.applicationInfo.packageName;
        installedEngineNames.add(engineName);

        // just logging the version number out of interest
        String version = "null";
        try {
            version = pm.getPackageInfo(engineName,
            PackageManager.GET_META_DATA).versionName;
            } catch (Exception e) {
                Log.i("XXX", "try catch error");
            }
        Log.i("XXX", "we found an engine: " + engineName);
        Log.i("XXX", "version: " + version);
    }
    return installedEngineNames;
}

这篇关于文本到语音的发音数字,例如"4th","8ths"或"2nd".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆