66
77/**
88 * Class that submits FLAC audio and retrieves recognized text
9+ *
10+ * @author Luke Kuza, Duncan Jauncey
911 */
1012public class Recognizer {
1113
1214 /**
1315 * URL to POST audio data and retrieve results
1416 */
15- private static final String GOOGLE_RECOGNIZER_URL_NO_LANG = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=" ;
17+ private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium" ;
18+
19+ private boolean profanityFilter = true ;
20+ private String language = null ;
21+
22+ public static final String LANG_US_ENGLISH = "en-US" ;
23+ public static final String LANG_UK_ENGLISH = "en-GB" ;
1624
1725 /**
1826 * Constructor
1927 */
2028 public Recognizer () {
29+ }
30+
31+ /**
32+ * Enable/disable Google's profanity filter (on by default).
33+ * @param profanityFilter
34+ */
35+ public void setProfanityFilter (boolean profanityFilter ) {
36+ this .profanityFilter = profanityFilter ;
37+ }
2138
39+ /**
40+ * Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
41+ * Setting this to null will make Google use it's own language detection.
42+ * This value is null by default.
43+ * @param language
44+ */
45+ public void setLanguage (String language ) {
46+ this .language = language ;
2247 }
2348
2449 /**
2550 * Get recognized data from a Wave file. This method will encode the wave file to a FLAC
2651 *
2752 * @param waveFile Wave file to recognize
28- * @param language Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
53+ * @param maxResults Maximum number of results to return in response
2954 * @return Returns a GoogleResponse, with the response and confidence score
3055 * @throws Exception Throws exception if something goes wrong
3156 */
32- public GoogleResponse getRecognizedDataForWave (File waveFile , String language ) throws Exception {
57+ public GoogleResponse getRecognizedDataForWave (File waveFile , int maxResults ) throws Exception {
3358 FlacEncoder flacEncoder = new FlacEncoder ();
3459 File flacFile = new File (waveFile + ".flac" );
3560
3661 flacEncoder .convertWaveToFlac (waveFile , flacFile );
3762
38- String response = rawRequest (flacFile , language );
63+ String response = rawRequest (flacFile , maxResults );
3964
4065 //Delete converted FLAC data
4166 flacFile .delete ();
4267
43- String [] parsedResponse = parseResponse (response );
44-
4568 GoogleResponse googleResponse = new GoogleResponse ();
46-
47- if (parsedResponse != null ) {
48- googleResponse .setResponse (parsedResponse [0 ]);
49- googleResponse .setConfidence (parsedResponse [1 ]);
50- } else {
51- googleResponse .setResponse (null );
52- googleResponse .setConfidence (null );
53- }
54-
69+ parseResponse (response , googleResponse );
5570 return googleResponse ;
5671 }
5772
5873 /**
5974 * Get recognized data from a Wave file. This method will encode the wave file to a FLAC
6075 *
6176 * @param waveFile Wave file to recognize
62- * @param language Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
77+ * @param maxResults the maximum number of results to return in the response
6378 * @return Returns a GoogleResponse, with the response and confidence score
6479 * @throws Exception Throws exception if something goes wrong
6580 */
66- public GoogleResponse getRecognizedDataForWave (String waveFile , String language ) throws Exception {
67- return getRecognizedDataForWave (new File (waveFile ), language );
81+ public GoogleResponse getRecognizedDataForWave (String waveFile , int maxResults ) throws Exception {
82+ return getRecognizedDataForWave (new File (waveFile ), maxResults );
6883 }
6984
7085 /**
7186 * Get recognized data from a FLAC file.
7287 *
7388 * @param flacFile FLAC file to recognize
74- * @param language Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
89+ * @param maxResults the maximum number of results to return in the response
7590 * @return Returns a GoogleResponse, with the response and confidence score
7691 * @throws Exception Throws exception if something goes wrong
7792 */
78- public GoogleResponse getRecognizedDataForFlac (File flacFile , String language ) throws Exception {
79- String response = rawRequest (flacFile , language );
80- String [] parsedResponse = parseResponse (response );
81-
93+ public GoogleResponse getRecognizedDataForFlac (File flacFile , int maxResults ) throws Exception {
94+ String response = rawRequest (flacFile , maxResults );
8295 GoogleResponse googleResponse = new GoogleResponse ();
83-
84-
85- if (parsedResponse != null ) {
86- googleResponse .setResponse (parsedResponse [0 ]);
87- googleResponse .setConfidence (parsedResponse [1 ]);
88- } else {
89- googleResponse .setResponse (null );
90- googleResponse .setConfidence (null );
91- }
92-
93-
96+ parseResponse (response , googleResponse );
9497 return googleResponse ;
9598 }
9699
97100 /**
98101 * Get recognized data from a FLAC file.
99102 *
100103 * @param flacFile FLAC file to recognize
101- * @param language Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
104+ * @param maxResults the maximum number of results to return in the response
102105 * @return Returns a GoogleResponse, with the response and confidence score
103106 * @throws Exception Throws exception if something goes wrong
104107 */
105- public GoogleResponse getRecognizedDataForFlac (String flacFile , String language ) throws Exception {
106- return getRecognizedDataForFlac (new File (flacFile ), language );
108+ public GoogleResponse getRecognizedDataForFlac (String flacFile , int maxResults ) throws Exception {
109+ return getRecognizedDataForFlac (new File (flacFile ), maxResults );
107110 }
108111
109112 /**
@@ -115,7 +118,7 @@ public GoogleResponse getRecognizedDataForFlac(String flacFile, String language)
115118 * @throws Exception Throws exception if something goes wrong
116119 */
117120 public GoogleResponse getRecognizedDataForWave (File waveFile ) throws Exception {
118- return getRecognizedDataForWave (waveFile , "en-US" );
121+ return getRecognizedDataForWave (waveFile , 1 );
119122 }
120123
121124 /**
@@ -127,7 +130,7 @@ public GoogleResponse getRecognizedDataForWave(File waveFile) throws Exception {
127130 * @throws Exception Throws exception if something goes wrong
128131 */
129132 public GoogleResponse getRecognizedDataForWave (String waveFile ) throws Exception {
130- return getRecognizedDataForWave (waveFile , "en-US" );
133+ return getRecognizedDataForWave (waveFile , 1 );
131134 }
132135
133136 /**
@@ -139,7 +142,7 @@ public GoogleResponse getRecognizedDataForWave(String waveFile) throws Exception
139142 * @throws Exception Throws exception if something goes wrong
140143 */
141144 public GoogleResponse getRecognizedDataForFlac (File flacFile ) throws Exception {
142- return getRecognizedDataForFlac (flacFile , "en-US" );
145+ return getRecognizedDataForFlac (flacFile , 1 );
143146 }
144147
145148 /**
@@ -151,7 +154,7 @@ public GoogleResponse getRecognizedDataForFlac(File flacFile) throws Exception {
151154 * @throws Exception Throws exception if something goes wrong
152155 */
153156 public GoogleResponse getRecognizedDataForFlac (String flacFile ) throws Exception {
154- return getRecognizedDataForFlac (flacFile , "en-US" );
157+ return getRecognizedDataForFlac (flacFile , 1 );
155158 }
156159
157160 /**
@@ -160,18 +163,45 @@ public GoogleResponse getRecognizedDataForFlac(String flacFile) throws Exception
160163 * @param rawResponse The raw, unparsed response from Google
161164 * @return Returns the parsed response. Index 0 is response, Index 1 is confidence score
162165 */
163- private String [] parseResponse (String rawResponse ) {
166+ private void parseResponse (String rawResponse , GoogleResponse googleResponse ) {
164167 if (!rawResponse .contains ("utterance" ))
165- return null ;
166-
167- String [] parsedResponse = new String [2 ];
168-
169- String [] strings = rawResponse .split (":" );
170-
171- parsedResponse [0 ] = strings [4 ].split ("\" " )[1 ];
172- parsedResponse [1 ] = strings [5 ].replace ("}]}" , "" );
173-
174- return parsedResponse ;
168+ return ;
169+
170+ String array = substringBetween (rawResponse , "[" , "]" );
171+ String [] parts = array .split ("}" );
172+ System .out .println (parts .length );
173+
174+ boolean first = true ;
175+ for ( String s : parts ) {
176+ if ( first ) {
177+ first = false ;
178+ String utterancePart = s .split ("," )[0 ];
179+ String confidencePart = s .split ("," )[1 ];
180+
181+ String utterance = utterancePart .split (":" )[1 ];
182+ String confidence = confidencePart .split (":" )[1 ];
183+
184+ utterance = stripQuotes (utterance );
185+ confidence = stripQuotes (confidence );
186+
187+ if ( utterance .equals ("null" ) ) {
188+ utterance = null ;
189+ }
190+ if ( confidence .equals ("null" ) ) {
191+ confidence = null ;
192+ }
193+
194+ googleResponse .setResponse (utterance );
195+ googleResponse .setConfidence (confidence );
196+ } else {
197+ String utterance = s .split (":" )[1 ];
198+ utterance = stripQuotes (utterance );
199+ if ( utterance .equals ("null" ) ) {
200+ utterance = null ;
201+ }
202+ googleResponse .getOtherPossibleResponses ().add (utterance );
203+ }
204+ }
175205 }
176206
177207 /**
@@ -182,14 +212,26 @@ private String[] parseResponse(String rawResponse) {
182212 * @return Returns the raw, unparsed response from Google
183213 * @throws Exception Throws exception if something went wrong
184214 */
185- private String rawRequest (File inputFile , String language ) throws Exception {
215+ private String rawRequest (File inputFile , int maxResults ) throws Exception {
186216 URL url ;
187217 URLConnection urlConn ;
188218 OutputStream outputStream ;
189219 BufferedReader br ;
190220
221+ StringBuilder sb = new StringBuilder (GOOGLE_RECOGNIZER_URL );
222+ if ( language != null ) {
223+ sb .append ("&lang=" );
224+ sb .append (language );
225+ }
226+ if ( !profanityFilter ) {
227+ sb .append ("&pfilter=0" );
228+ }
229+ sb .append ("&maxresults=" );
230+ sb .append (maxResults );
231+
191232 // URL of Remote Script.
192- url = new URL (GOOGLE_RECOGNIZER_URL_NO_LANG + language );
233+ url = new URL (sb .toString ());
234+
193235
194236 // Open New URL connection channel.
195237 urlConn = url .openConnection ();
@@ -229,5 +271,31 @@ private String rawRequest(File inputFile, String language) throws Exception {
229271
230272 }
231273
274+ private String substringBetween (String s , String part1 , String part2 ) {
275+ String sub = null ;
276+
277+ int i = s .indexOf (part1 );
278+ int j = s .indexOf (part2 , i + part1 .length ());
279+
280+ if (i != -1 && j != -1 ) {
281+ int nStart = i + part1 .length ();
282+ sub = s .substring (nStart , j );
283+ }
284+
285+ return sub ;
286+ }
287+
288+ private String stripQuotes (String s ) {
289+ int start = 0 ;
290+ if ( s .startsWith ("\" " ) ) {
291+ start = 1 ;
292+ }
293+ int end = s .length ();
294+ if ( s .endsWith ("\" " ) ) {
295+ end = s .length () - 1 ;
296+ }
297+ return s .substring (start , end );
298+ }
299+
232300
233301}
0 commit comments