Skip to content

Commit e654e29

Browse files
committed
String utility class added and code decomposed.
1 parent d296dc6 commit e654e29

File tree

4 files changed

+87
-75
lines changed

4 files changed

+87
-75
lines changed

src/com/darkprograms/speech/recognizer/GSpeechDuplex.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import javax.net.ssl.HttpsURLConnection;
1717

18+
import com.darkprograms.speech.util.StringUtil;
19+
1820
/**
1921
* A class for using Google's Duplex Speech API. Allows for continuous recognition. Requires an API-Key.
2022
* A duplex API opens two connections. One to an upstream and one to a downstream. The system allows
@@ -298,15 +300,15 @@ private void parseResponse(String rawResponse, GoogleResponse gr){
298300
if(rawResponse == null || !rawResponse.contains("\"result\"")
299301
|| rawResponse.equals("{\"result\":[]}")){ return; }
300302
if(rawResponse.contains("\"confidence\":")){
301-
String confidence = getBetween(rawResponse, "\"confidence\":", "}");
303+
String confidence = StringUtil.trimString(rawResponse, "\"confidence\":", "}");
302304
gr.setConfidence(confidence);
303305
}
304306
else{
305307
gr.setConfidence(String.valueOf(1d));
306308
}
307-
String array = getBetween(rawResponse, "[", "]");
309+
String array = StringUtil.trimString(rawResponse, "[", "]");
308310
if(array.contains("[")){
309-
array = getBetween(array, "[", "]");
311+
array = StringUtil.trimString(array, "[", "]");
310312
}
311313
String[] parts = array.split(",");
312314
gr.setResponse(parseTranscript(parts[0]));
@@ -325,25 +327,7 @@ private String parseTranscript(String s){
325327
if(s.endsWith("}")){
326328
tmp = tmp.substring(0, tmp.length()-1);
327329
}
328-
tmp = stripQuotes(tmp);
329-
return tmp;
330-
}
331-
332-
private String stripQuotes(String s) {
333-
int start = 0;
334-
if( s.startsWith("\"") ) {
335-
start = 1;
336-
}
337-
int end = s.length();
338-
if( s.endsWith("\"") ) {
339-
end = s.length() - 1;
340-
}
341-
return s.substring(start, end);
342-
}
343-
344-
private String getBetween(String s, String part1, String part2){
345-
String tmp = s.substring(s.indexOf(part1) + part1.length() + 1);
346-
tmp = tmp.substring(0, tmp.lastIndexOf(part2));
330+
tmp = StringUtil.stripQuotes(tmp);
347331
return tmp;
348332
}
349333

src/com/darkprograms/speech/recognizer/Recognizer.java

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.net.URLConnection;
66
import java.nio.charset.Charset;
77

8+
import com.darkprograms.speech.util.StringUtil;
9+
810
/***************************************************************
911
* Class that submits FLAC audio and retrieves recognized text
1012
*
@@ -356,7 +358,7 @@ private void parseResponse(String rawResponse, GoogleResponse googleResponse) {
356358
if (rawResponse == null || !rawResponse.contains("utterance"))
357359
return;
358360

359-
String array = substringBetween(rawResponse, "[", "]");
361+
String array = StringUtil.substringBetween(rawResponse, "[", "]");
360362
String[] parts = array.split("}");
361363

362364
boolean first = true;
@@ -369,8 +371,8 @@ private void parseResponse(String rawResponse, GoogleResponse googleResponse) {
369371
String utterance = utterancePart.split(":")[1];
370372
String confidence = confidencePart.split(":")[1];
371373

372-
utterance = stripQuotes(utterance);
373-
confidence = stripQuotes(confidence);
374+
utterance = StringUtil.stripQuotes(utterance);
375+
confidence = StringUtil.stripQuotes(confidence);
374376

375377
if( utterance.equals("null") ) {
376378
utterance = null;
@@ -383,7 +385,7 @@ private void parseResponse(String rawResponse, GoogleResponse googleResponse) {
383385
googleResponse.setConfidence(confidence);
384386
} else {
385387
String utterance = s.split(":")[1];
386-
utterance = stripQuotes(utterance);
388+
utterance = StringUtil.stripQuotes(utterance);
387389
if( utterance.equals("null") ) {
388390
utterance = null;
389391
}
@@ -462,31 +464,4 @@ private String rawRequest(File inputFile, int maxResults, int sampleRate) throws
462464

463465
}
464466

465-
private String substringBetween(String s, String part1, String part2) {
466-
String sub = null;
467-
468-
int i = s.indexOf(part1);
469-
int j = s.indexOf(part2, i + part1.length());
470-
471-
if (i != -1 && j != -1) {
472-
int nStart = i + part1.length();
473-
sub = s.substring(nStart, j);
474-
}
475-
476-
return sub;
477-
}
478-
479-
private String stripQuotes(String s) {
480-
int start = 0;
481-
if( s.startsWith("\"") ) {
482-
start = 1;
483-
}
484-
int end = s.length();
485-
if( s.endsWith("\"") ) {
486-
end = s.length() - 1;
487-
}
488-
return s.substring(start, end);
489-
}
490-
491-
492467
}

src/com/darkprograms/speech/recognizer/RecognizerChunked.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import javax.net.ssl.HttpsURLConnection;
2020
import javax.xml.ws.http.HTTPException;
2121

22+
import com.darkprograms.speech.util.StringUtil;
23+
2224
/**
2325
* This class uses Google's V2 Hook. The class is returns a chunked respones so listeners must be used.
2426
* The class also requires an API-Key (see Constructor) for details. This class is experimental and
@@ -218,15 +220,15 @@ private byte[] mapFileIn(File infile) throws IOException{
218220
private void parseResponse(String rawResponse, GoogleResponse gr){
219221
if(rawResponse == null || !rawResponse.contains("\"result\"")){ return; }
220222
if(rawResponse.contains("\"confidence\":")){
221-
String confidence = getBetween(rawResponse, "\"confidence\":", "}");
223+
String confidence = StringUtil.trimString(rawResponse, "\"confidence\":", "}");
222224
gr.setConfidence(confidence);
223225
}
224226
else{
225227
gr.setConfidence(String.valueOf(1d));
226228
}
227-
String array = getBetween(rawResponse, "[", "]");
229+
String array = StringUtil.trimString(rawResponse, "[", "]");
228230
if(array.contains("[")){
229-
array = getBetween(array, "[", "]");
231+
array = StringUtil.trimString(array, "[", "]");
230232
}
231233
String[] parts = array.split(",");
232234
gr.setResponse(parseTranscript(parts[0]));
@@ -245,7 +247,7 @@ private String parseTranscript(String s){
245247
if(s.endsWith("}")){
246248
tmp = tmp.substring(0, tmp.length()-1);
247249
}
248-
tmp = stripQuotes(tmp);
250+
tmp = StringUtil.stripQuotes(tmp);
249251
return tmp;
250252
}
251253

@@ -275,22 +277,4 @@ private synchronized void fireResponseEvent(GoogleResponse gr){
275277
}
276278
}
277279

278-
private String stripQuotes(String s) {
279-
int start = 0;
280-
if( s.startsWith("\"") ) {
281-
start = 1;
282-
}
283-
int end = s.length();
284-
if( s.endsWith("\"") ) {
285-
end = s.length() - 1;
286-
}
287-
return s.substring(start, end);
288-
}
289-
290-
private String getBetween(String s, String part1, String part2){
291-
String tmp = s.substring(s.indexOf(part1) + part1.length() + 1);
292-
tmp = tmp.substring(0, tmp.lastIndexOf(part2));
293-
return tmp;
294-
}
295-
296280
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.darkprograms.speech.util;
2+
3+
/**
4+
* A string utility class for commonly used methods.
5+
* These methods are particularly useful for parsing.
6+
* @author Skylion
7+
*/
8+
public class StringUtil {
9+
10+
private StringUtil() {};//Prevents instantiation
11+
12+
/**
13+
* Removes quotation marks from beginning and end of string.
14+
* @param s The string you want to remove the quotation marks from.
15+
* @return The modified String.
16+
*/
17+
public static String stripQuotes(String s) {
18+
int start = 0;
19+
if( s.startsWith("\"") ) {
20+
start = 1;
21+
}
22+
int end = s.length();
23+
if( s.endsWith("\"") ) {
24+
end = s.length() - 1;
25+
}
26+
return s.substring(start, end);
27+
}
28+
29+
/**
30+
* Returns the first instance of String found exclusively between part1 and part2.
31+
* @param s The String you want to substring.
32+
* @param part1 The beginning of the String you want to search for.
33+
* @param part2 The end of the String you want to search for.
34+
* @return The String between part1 and part2.
35+
* If the s does not contain part1 or part2, the method returns null.
36+
*/
37+
public static String substringBetween(String s, String part1, String part2) {
38+
String sub = null;
39+
40+
int i = s.indexOf(part1);
41+
int j = s.indexOf(part2, i + part1.length());
42+
43+
if (i != -1 && j != -1) {
44+
int nStart = i + part1.length();
45+
sub = s.substring(nStart, j);
46+
}
47+
48+
return sub;
49+
}
50+
51+
/**
52+
* Gets the string exclusively between the first instance of part1 and the last instance of part2.
53+
* @param s The string you want to trim.
54+
* @param part1 The term to trim after first instance.
55+
* @param part2 The term to before last instance of.
56+
* @return The trimmed String
57+
*/
58+
public static String trimString(String s, String part1, String part2){
59+
if(!s.contains(part1) || !s.contains(part2)){
60+
return null;
61+
}
62+
int first = s.indexOf(part1) + part1.length() + 1;
63+
String tmp = s.substring(first);
64+
int last = tmp.lastIndexOf(part2);
65+
tmp = tmp.substring(0, last);
66+
return tmp;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)