phsycholic
phsycholic

Reputation: 33

Google Text-To-Speech ordinal numbers output

I would like the Google Text-To-Speech Engine to speak a sentence like this:

Today is the 25th of July.

But with the current Version 3.3.13.1635260.arm and updated language packages the output is like this:

Today is the 25 of July.

Time time = new Time(Time.getCurrentTimezone());
time.setToNow();
today = time.monthDay;
String output = "Today is the "+ today + ". of July.";
speech.speak(output, TextToSpeech.QUEUE_FLUSH, null);

I tried it with int and String values of today, same results.

Upvotes: 2

Views: 522

Answers (2)

Umit Kaya
Umit Kaya

Reputation: 5961

 static String[] suffixes =
 //    0     1     2     3     4     5     6     7     8     9
 { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
 //    10    11    12    13    14    15    16    17    18    19
   "th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
 //    20    21    22    23    24    25    26    27    28    29
   "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
 //    30    31
   "th", "st" };

Date date = new Date();
SimpleDateFormat formatDayOfMonth  = new SimpleDateFormat("d");
int day = Integer.parseInt(formatDateOfMonth.format(date));
String dayStr = day + suffixes[day];

and then:

String output = "Today is the "+ dayStr + ". of July.";
speech.speak(output, TextToSpeech.QUEUE_FLUSH, null);

Upvotes: 1

Lachlan Goodhew-Cook
Lachlan Goodhew-Cook

Reputation: 1101

It's probably because it's interpreting the string exactly as you're passing it in e.g."Today is the 25 of July."

Something like this should help out https://stackoverflow.com/a/4011232/3724365.

// http://code.google.com/p/guava-libraries 
import static com.google.common.base.Preconditions.*; 

String getDayOfMonthSuffix(final int n) {
    checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);
    if (n >= 11 && n <= 13) {
        return "th"; 
    } 
    switch (n % 10) {
        case 1:  return "st"; 
        case 2:  return "nd"; 
        case 3:  return "rd"; 
        default: return "th"; 
    } 
} 

So you'd get

String daySuffix = getDayOfMonthSuffix(today);
String ordinalToday = today + daySuffix;
String output = "Today is the "+ ordinalToday + ". of July.";
speech.speak(output, TextToSpeech.QUEUE_FLUSH, null);

Upvotes: 0

Related Questions