From 9348757ce67062073c1a6603b23c063a27112e42 Mon Sep 17 00:00:00 2001 From: Skylion007 Date: Sat, 27 Jul 2013 15:28:27 -0400 Subject: Added Methods to automatically parse strings to an acceptable length. Google does not respond to requests longer than 99 characters. Added methods to automatically parse into smaller parse, send them individually, and return a single input stream. --- .../speech/synthesiser/Synthesiser.java | 74 +++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/src/com/darkprograms/speech/synthesiser/Synthesiser.java b/src/com/darkprograms/speech/synthesiser/Synthesiser.java index bbedb0d..78884ae 100644 --- a/src/com/darkprograms/speech/synthesiser/Synthesiser.java +++ b/src/com/darkprograms/speech/synthesiser/Synthesiser.java @@ -4,11 +4,14 @@ import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; /** * Synthesiser class that connects to Google's unoffical API to retreive data * * @author Luke Kuza + * @author Aaron Gokaslan (String parsing Methods) */ public class Synthesiser { @@ -32,7 +35,13 @@ public class Synthesiser { * @throws Exception Throws exception if it can not complete the request */ public InputStream getMP3Data(String synthText) throws Exception { - String encoded = URLEncoder.encode(synthText, "UTF-8"); //Encode + + if(synthText.length()>99){ + List fragments = stringParser(synthText); + return getMP3Data(fragments); + } + + String encoded = URLEncoder.encode(synthText, "UTF-8"); //Encode URL url = new URL(GOOGLE_SYNTHESISER_URL + encoded); //create url @@ -44,5 +53,68 @@ public class Synthesiser { return urlConn.getInputStream(); } + + /** + * Gets an InputStream to MP3Data for the returned information from a request + * @param synthText List of Strings you want to be synthesized into MP3 data + * @return Returns an input stream of all the MP3 data that is returned from Google + * @throws Exception Throws exception if it cannot complete the request + */ + public InputStream getMP3Data(List synthText) throws Exception{ + InputStream complete = getMP3Data(synthText.remove(0)); + for(String part: synthText){ + complete = new java.io.SequenceInputStream(complete, getMP3Data(part));//Concatenate with new MP3 Data + } + return complete; + } + /** + * Separates a string into smaller parts so that Google will not reject the request. + * @param input The string you want to separate + * @return A List of the String fragments from your input.. + */ + private List stringParser(String input){ + return stringParser(input, new ArrayList()); + } + + /** + * Separates a string into smaller parts so that Google will not reject the request. + * @param input The string you want to break up into smaller parts + * @param fragments List that you want to add stuff too. + * If you don't have a List already constructed "new ArrayList()" works well. + * @return A list of the fragments of the original String + */ + private List stringParser(String input, List fragments){ + if(input.length()<100){//Base Case + fragments.add(input); + return fragments; + } + else{ + int space = findLastWord(input);//Checks if a space exists + if(space<0){ + fragments.add(input.substring(0,99));//In case you sent gibberish to Google. + return stringParser(input.substring(99), fragments); + }else{ + fragments.add(input.substring(0,space));//Otherwise, adds the last word to the list for recursion. + return stringParser(input.substring(space), fragments); + } + } + } + + /** + * Finds the last word in your String (before the index of 99) by searching for spaces and ending punctuation. + * @param input The String you want to search through. + * @return The index of where the last word of the String ends before the index of 99. + */ + private int findLastWord(String input){ + if(input.length()<100) + return input.length(); + for(int i = 99; i>=0; i--){ + char tmp = input.charAt(i); + if( tmp == ' ' || tmp == '.' || tmp == '!' || tmp == '?' || tmp == ';' || tmp == ':' || tmp == '؟' + || tmp == '|'){ //Ending punctuation for all languages according to Wikipedia + return i; + } + } + } } -- cgit v1.2.3