| 1 | package speechrecorder; |
|---|
| 2 | |
|---|
| 3 | import java.util.Random; |
|---|
| 4 | import java.util.StringTokenizer; |
|---|
| 5 | import java.io.*; |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * Localizes the speech submission applet in various languages |
|---|
| 9 | * |
|---|
| 10 | * <h1>License</h1> |
|---|
| 11 | * <p>Copyright (C) 2007-2011 Ken MacLean |
|---|
| 12 | * |
|---|
| 13 | * <p>This program is free software: you can redistribute it and/or modify |
|---|
| 14 | * it under the terms of the GNU General Public License as published by |
|---|
| 15 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 16 | * (at your option) any later version. |
|---|
| 17 | * |
|---|
| 18 | * <p>This program is distributed in the hope that it will be useful, |
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | * GNU General Public License for more details. |
|---|
| 22 | * |
|---|
| 23 | * <p>You should have received a copy of the GNU General Public License |
|---|
| 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 25 | * |
|---|
| 26 | */ |
|---|
| 27 | class Prompts { |
|---|
| 28 | //todo need to convert to use .po files... |
|---|
| 29 | /* |
|---|
| 30 | * Class variables |
|---|
| 31 | */ |
|---|
| 32 | private String [] promptList; |
|---|
| 33 | private String [] englishPromptList = { |
|---|
| 34 | "rp-01 When the sunlight strikes raindrops in the air,", |
|---|
| 35 | "rp-02 they act as a prism and form a rainbow.", |
|---|
| 36 | "rp-03 The rainbow is a division of white light into many beautiful colors.", |
|---|
| 37 | "rp-04 These take the shape of a long round arch, with its path high above,", |
|---|
| 38 | "rp-05 and its two ends apparently beyond the horizon.", |
|---|
| 39 | "rp-06 There is , according to legend, a boiling pot of gold at one end.", |
|---|
| 40 | "rp-07 People look, but no one ever finds it.", |
|---|
| 41 | "rp-08 When a man looks for something beyond his reach,", |
|---|
| 42 | "rp-09 his friends say he is looking for the pot of gold at the end of the rainbow.", |
|---|
| 43 | "rp-10 Throughout the centuries people have explained the rainbow in various ways.", |
|---|
| 44 | "rp-11 Some have accepted it as a miracle without physical explanation.", |
|---|
| 45 | "rp-12 To the Hebrews it was a token that there would be no more universal floods.", |
|---|
| 46 | "rp-13 The Greeks used to imagine that it was a sign", |
|---|
| 47 | "rp-14 from the gods to foretell war or heavy rain.", |
|---|
| 48 | "rp-15 The Norsemen considered the rainbow as a bridge", |
|---|
| 49 | "rp-16 over which the gods passed from earth to their home in the sky.", |
|---|
| 50 | "rp-17 Others have tried to explain the phenomenon physically.", |
|---|
| 51 | "rp-18 Aristotle thought that the rainbow was caused by", |
|---|
| 52 | "rp-19 reflection of the sun's rays by the rain.", |
|---|
| 53 | "rp-20 Since then physicists have found that it is not reflection,", |
|---|
| 54 | "rp-21 but refraction by the raindrops which causes the rainbows.", |
|---|
| 55 | "rp-22 Many complicated ideas about the rainbow have been formed.", |
|---|
| 56 | "rp-23 The difference in the rainbow depends considerably upon the size of the drops,", |
|---|
| 57 | "rp-24 and the width of the colored band increases as the size of the drops increases.", |
|---|
| 58 | "rp-25 The actual primary rainbow observed is said to be the effect of", |
|---|
| 59 | "rp-26 super-imposition of a number of bows.", |
|---|
| 60 | "rp-27 If the red of the second bow falls upon the green of the first,", |
|---|
| 61 | "rp-28 the result is to give a bow with an abnormally wide yellow band,", |
|---|
| 62 | "rp-29 since red and green light when mixed form yellow.", |
|---|
| 63 | "rp-30 This is a very common type of bow, one showing mainly red and yellow,", |
|---|
| 64 | "rp-31 with little or no green or blue." , |
|---|
| 65 | "ar-01 Once there was a young rat named Arthur who never could make up his mind.", |
|---|
| 66 | "ar-02 Whenever his friends asked him if he would like to go out with them,", |
|---|
| 67 | "ar-03 he would only answer, \"I don't know;\" he wouldn't say yes or no either.", |
|---|
| 68 | "ar-04 He would always shirk making a choice. His Aunt Helen said to him,", |
|---|
| 69 | "ar-05 \"Now look here! No one is going to care for you if you carry on like this.\"", |
|---|
| 70 | "ar-06 You have no more mind than a blade of grass.", |
|---|
| 71 | "ar-07 One rainy day the rats heard a great noise in the loft.", |
|---|
| 72 | "ar-08 The pine rafters were all rotten, so that the barn was rather unsafe.", |
|---|
| 73 | "ar-09 At last the joists gave way and fell to the ground.", |
|---|
| 74 | "ar-10 The walls shook, and all the rats' hair stood on end with fear and horror.", |
|---|
| 75 | "ar-11 \"This won't do,\" said the captain; \"I'll send out scouts to search for a new home.\"", |
|---|
| 76 | "ar-12 Within five hours the ten scouts came back and said,", |
|---|
| 77 | "ar-13 We found a stone house where there is room for us all.", |
|---|
| 78 | "ar-14 There is a kindly horse named Nelly, a cow, a calf, and a garden with an elm tree.", |
|---|
| 79 | "ar-15 The rats crawled out of their little houses and stood on the floor in a long line.", |
|---|
| 80 | "ar-16 Just then the old rat saw Arthur. Stop. he ordered coarsely.", |
|---|
| 81 | "ar-17 \"You are coming, of course.\" \"I'm not certain,\" said Arthur, undaunted,", |
|---|
| 82 | "ar-18 \"The roof may not come down yet.\"", |
|---|
| 83 | "ar-19 \"Well,\" said the old rat, \"we can't wait for you to join us. Right about face! March!\"", |
|---|
| 84 | "ar-20 Arthur stood and watched them hurry away.", |
|---|
| 85 | "ar-21 \"I think I'll go tomorrow,\" he said calmly to himself, \"but then again I don't know;\"", |
|---|
| 86 | "ar-22 \"it's so nice and snug here,\". That night there was a big crash.", |
|---|
| 87 | "ar-23 In the foggy morning some men with some boys and girls rode up and looked at the barn.", |
|---|
| 88 | "ar-24 One of them moved a board and saw a rat quite dead, half in and half out of his hole.", |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | private String [][] promptSubset; |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * determine which prompt list to use based on ISO 3166 country codes |
|---|
| 95 | * (see http://www.theodora.com/country_digraphs.html) |
|---|
| 96 | * |
|---|
| 97 | * @param NumberofPrompts |
|---|
| 98 | * @param Language |
|---|
| 99 | */ |
|---|
| 100 | public Prompts(int NumberofPrompts, String Language) { |
|---|
| 101 | if (Language.equals("EN")) { |
|---|
| 102 | promptList = getPromptTextFile("englishPromptList.txt", 1223); // number of prompts needs to be exact or there might be an error on a roll-over |
|---|
| 103 | } else if (Language.equals("NL")) { |
|---|
| 104 | promptList = getPromptTextFile("nl","dutchPromptList.txt", 947); |
|---|
| 105 | } else if (Language.equals("DE")) { |
|---|
| 106 | promptList = getPromptTextFile("germanPromptList.txt", 1211); |
|---|
| 107 | } else if (Language.equals("RU")) { |
|---|
| 108 | promptList = getPromptTextFile("russianPromptList.txt", 81); |
|---|
| 109 | } else if (Language.equals("IT")) { |
|---|
| 110 | promptList = getPromptTextFile("it","italianPromptList.txt", 1185); |
|---|
| 111 | } else if (Language.equals("HB")) { |
|---|
| 112 | promptList = getPromptTextFile("hebrewPromptList.txt", 55); |
|---|
| 113 | } else if (Language.equals("PT_BR")) { |
|---|
| 114 | promptList = getPromptTextFile("brazilianPromptList.txt", 218); |
|---|
| 115 | } else if (Language.equals("ES")) { |
|---|
| 116 | promptList = getPromptTextFile("es","spanishPromptList.txt", 43); |
|---|
| 117 | } else if (Language.equals("FR")) { |
|---|
| 118 | promptList = getPromptTextFile("frenchPromptList.txt", 865); |
|---|
| 119 | } else if (Language.equals("EL")) { |
|---|
| 120 | promptList = getPromptTextFile("el","greekPromptList.txt", 221); |
|---|
| 121 | } else if (Language.equals("TR")) { |
|---|
| 122 | promptList = getPromptTextFile("tr","turkishPromptList.txt", 40 ); |
|---|
| 123 | } else if (Language.equals("BG")) { |
|---|
| 124 | promptList = getPromptTextFile("bg","bulgarianPromptList.txt", 50 ); |
|---|
| 125 | } else { |
|---|
| 126 | System.err.println("getPromptTextFile error accessing prompt file for "+ Language + "using default English prompts"); |
|---|
| 127 | promptList = englishPromptList; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | promptSubset = new String [2][NumberofPrompts]; |
|---|
| 131 | |
|---|
| 132 | Random randomGenerator = new Random(); |
|---|
| 133 | int arrayLength = promptList.length - 1; |
|---|
| 134 | int nextPrompt = randomGenerator.nextInt(arrayLength); |
|---|
| 135 | for (int i=0;i<NumberofPrompts; i++) { |
|---|
| 136 | if (nextPrompt <= arrayLength) { |
|---|
| 137 | getPromptLine(nextPrompt, i); |
|---|
| 138 | } else { |
|---|
| 139 | nextPrompt = 0; |
|---|
| 140 | getPromptLine(nextPrompt, i); |
|---|
| 141 | } |
|---|
| 142 | nextPrompt++; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | public String [][] getPrompts() { |
|---|
| 147 | return promptSubset; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | private void getPromptLine(int nextPrompt, int idx) { |
|---|
| 151 | String promptID; |
|---|
| 152 | StringBuffer prompt = new StringBuffer(); |
|---|
| 153 | |
|---|
| 154 | StringTokenizer st = new StringTokenizer(promptList[nextPrompt]); |
|---|
| 155 | String [] words= new String [st.countTokens()]; |
|---|
| 156 | int i = 0; |
|---|
| 157 | while (st.hasMoreTokens()) { |
|---|
| 158 | words[i] = st.nextToken(); i++; |
|---|
| 159 | } |
|---|
| 160 | promptID = words[0]; |
|---|
| 161 | |
|---|
| 162 | int j = 0; |
|---|
| 163 | if (words.length > 0) { |
|---|
| 164 | for (i=1; i<words.length; i++,j++) { |
|---|
| 165 | prompt.append(words[i]); |
|---|
| 166 | prompt.append(" "); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | promptSubset [0][idx] = promptID; |
|---|
| 170 | promptSubset [1][idx] = prompt.toString(); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | private String [] getPromptTextFile(String File, int numberOfPrompts) { |
|---|
| 174 | String [] words= new String [numberOfPrompts]; |
|---|
| 175 | try { |
|---|
| 176 | InputStream is = getClass().getResourceAsStream(File); |
|---|
| 177 | InputStreamReader isr = new InputStreamReader(is,"UTF-8" ); |
|---|
| 178 | System.err.println(System.getProperty("line.separator") + "PromptList Character Encoding:" + isr.getEncoding()); // doesn't work ???? |
|---|
| 179 | BufferedReader br = new BufferedReader(isr); |
|---|
| 180 | String line; |
|---|
| 181 | int i = 0; |
|---|
| 182 | while ((line = br.readLine()) != null) { |
|---|
| 183 | words[i] = line; |
|---|
| 184 | i++; |
|---|
| 185 | } |
|---|
| 186 | } catch (IOException io) { |
|---|
| 187 | System.err.println("getPromptTextFile error accessing text file for "+File + "using default prompts"); |
|---|
| 188 | return englishPromptList; |
|---|
| 189 | } |
|---|
| 190 | return words; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | private String [] getPromptTextFile(String Prefix, String File, int numberOfPrompts) { |
|---|
| 194 | String [] words= new String [numberOfPrompts]; |
|---|
| 195 | try { |
|---|
| 196 | InputStream is = getClass().getResourceAsStream(File); |
|---|
| 197 | InputStreamReader isr = new InputStreamReader(is,"UTF-8" ); |
|---|
| 198 | System.err.println(System.getProperty("line.separator") + "PromptList Character Encoding:" + isr.getEncoding()); // doesn't work ???? |
|---|
| 199 | BufferedReader br = new BufferedReader(isr); |
|---|
| 200 | String line; |
|---|
| 201 | int i = 0; |
|---|
| 202 | while ((line = br.readLine()) != null) { |
|---|
| 203 | // System.err.println(Prefix + "-" + paddedZeros(i,4) + " " + line); // !!!!!! |
|---|
| 204 | words[i] = Prefix + "-" + paddedZeros(i,4) + " " + line; |
|---|
| 205 | i++; |
|---|
| 206 | } |
|---|
| 207 | } catch (IOException io) { |
|---|
| 208 | System.err.println("getPromptTextFile error accessing text file for "+File + "using default prompts"); |
|---|
| 209 | return englishPromptList; |
|---|
| 210 | } |
|---|
| 211 | return words; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | public String paddedZeros(int number, int numLength) |
|---|
| 215 | { |
|---|
| 216 | String s = Integer.toString(number); |
|---|
| 217 | if (s.length() > numLength) { |
|---|
| 218 | return s.substring(0,numLength); |
|---|
| 219 | } else if (s.length() < numLength) { |
|---|
| 220 | return "0000000000".substring(0, numLength - s.length ()) + s; |
|---|
| 221 | } else { |
|---|
| 222 | return s; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | } |
|---|