In certain (quantifiable by my program) circumstances I want the tts engine to speak a string of letters as their own names rather than what they spell.
For example, in the square with corners named A, B, C, & D, I want it to speak the name of the line AB as "ay bee". At the moment it says "ab".
Can I do this? And, obviously, if so, how?
As suggested You can add spaces between alphabets and feed the it to TTS.
String a_z="A B C ...";
or if it is coming from some input source then do
input = "ABCDE".replaceAll("([A-Z])"," $1").trim()
where ([A-Z]) mean capture any capital alphabet which is represented by $1
and you can use setSpeechRate to stretch the speed to make it sounds as required.
I found a dictionary sample in GitHub that I am currently experimenting with. The sample database used hyphen between the searched word and the word's meaning. So something like this.
abbey - n. a monastery ruled by an abbot
I looked into the dictionary database java file and found the following code:
String[] strings = TextUtils.split(line, "-");
I have my own database that translates Korean words to English. However I didn't use hyphen while creating it. So is there a way to not use hyphen or any other symbols but simply spaces? Also this is part of an android app.
Edit- An example of my own dictionary would be something like
abbey a monastery ruled by an abbot
Edit-
The problem here is that the old code only differentiates and recognizes the words and the meaning only if they are separated by hyphen. How do I make this so it works with spaces alone.
To remove a character in a String use String.replace
String newString = line.replace("-","");
To replace with a space simply use
String newString = line.replace("-"," ");
String mystring = mystring 1.replace("_"," "); if you want space give space.
As I understand it, you want to split your String to get the output like
abbey - n. a monastery ruled by an abbot
[abbey][n. a monastery ruled by an abbot]
You can use String.split(String, int) to force the number of split.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times
Let's use it like :
String[] array = s.split(" ", 2);
This will split your String on the regex " " but will limit the size of the output to 2 cells. So it will only split once, put the left part on the first cell and the right part on the second cell.
Without this limit argument, the method would keep split the right part again using a bigger array.
Note: this will be a problem if your word is a sentence in the left part.
I have a very weird problem. After writing this:
for (File f : currentFile.listFiles()) {
if (f.isDirectory()){
System.out.println(f.getName()+"\t"+"Dir\t"+Command.getpremission(f)+"\t"+f.getTotalSpace());
}
else{
System.out.println(f.getName()+"\t"+"File\t"+Command.getpremission(f)+"\t"+f.getTotalSpace());
}
I see this printed:
see.txt File rw 267642728448
see1.txt File rw 267642728456
see2.txt File rw 267642728448
Why is there a problem with the tabs?
Building on this question, I use the following code to indent my messages:
String prefix1 = "short text:";
String prefix2 = "looooooooooooooong text:";
String msg = "indented";
/*
* The second string begins after 40 characters. The dash means that the
* first string is left-justified.
*/
String format = "%-40s%s%n";
System.out.printf(format, prefix1, msg);
System.out.printf(format, prefix2, msg);
This is the output:
short text: indented
looooooooooooooong text: indented
This is documented in section "Flag characters" in man 3 printf.
The "problem" with the tabs is that they indent the text to fixed tab positions, typically multiples of 4 or 8 characters (depending on the console or editor displaying them). Your first filename is 7 chars, so the next tab stop after its end is at position 8. Your subsequent filenames however are 8 chars long, so the next tab stop is at position 12.
If you want to ensure that columns get nicely indented at the same position, you need to take into account the actual length of previous columns, and either modify the number of following tabs, or pad with the required number of spaces instead. The latter can be achieved using e.g. System.out.printf with an appropriate format specification (e.g. "%1$13s" specifies a minimum width of 13 characters for displaying the first argument as a string).
The length of the text that you are providing in each line is different, this is the problem, so if the second word is too long (see2.txt is long 8 char which corresponds to a single tab lenght) it prints out a tab which goes to the next tabulation point.
One way to solve it is to programmatically add a pad to the f.getName() text so each text generated: see.txt or see2.txt has the same lenght (for example see.txt_ and see2.txt) so each tab automatically goes to the same tabulation point.
If you are developing with JDK 1.5 you can solve this using java.util.Formatter:
String format = "%-20s %5d\n";
System.out.format(format, "test", 1);
System.out.format(format, "test2", 20);
System.out.format(format, "test3", 5000);
this example will give you this print:
test 1
test2 20
test3 5000
In continuation of the comments by Péter and duncan, I normally use a quick padding method, something like -
public String rpad(String inStr, int finalLength)
{
return (inStr + " " // typically a sufficient length spaces string.
).substring(0, finalLength);
}
similarly you can have a lpad() as well
As mentioned by other folks, the variable length of the string is the issue.
Rather than reinventing the wheel, Apache Commons has a nice, clean solution for this in StringUtils.
StringUtils.rightPad("String to extend",100); //100 is the length you want to pad out to.
The problem is the length of the filenames. The first filename is only 7 chars long, so the tab occurs at char 8 (doing a tab after every 4 characters). However the next filenames are 8 chars long, so the next tab won't be until char 12. And if you had filenames longer than 11 chars, you'd run into the same problem again.
You can use this example to handle your problem:
System.out.printf( "%-15s %15s %n", "name", "lastname");
System.out.printf( "%-15s %15s %n", "Bill", "Smith");
You can play with the "%" until you find the right alignment to satisfy your needs
You can also pad a string to the required length using Guava's Strings.padEnd(String input, int minLength, char padding)
Is there a way to TTS output Pinyin via the tts class (in the correct tone)?
I tried SVOX and Pico...
I have a database where different words are stored in a form like
Ni3
Hao3
I tried:
- Ni3Hao3 -> result: NiSanHaoSan (San means 3).
- Nín hǎo -> result: N i n h o (Only speaking letters, missing the a)
I thought about using a table givin to every word - sound combination one chinese utf sign. But that seems to be absurd.
Is there a way to handle this?
U might try the jtts which depends on espeak.
I'm trying to get TextToSpeech to speak alphanumeric references in the Android emulator. If I have a string such as "31NAA123", then I insert spaces between all the characters and then submit it to the speak method. It copes OK with most letters but stumbles on the 'A's, they come out as barely audible very short "ah"s, almost as short as clicks. I've tried replaceAll("A", "AY") which comes out as "ay, why". I've tried most speech rates down to 0.3f - nothing works. I'm using a UK locale to give a British accent. I'd be grateful if anyone has any useful suggestions.
(I suspect 'i' will give similar problems, 'o' seems to be OK)
Have you tried inserting multiple points after the letters? Seems to work pretty well for me:
String s = "a.. b.. c..";
Or even slower:
String s = "a... b... c...";