Skip to content

Commit 9d06a46

Browse files
author
Luke Kuza
committed
Complete issue #5 . Recognition now supports multiple languages from Google. ex. getRecognizedDataForWave(file, languagecode)
Known language codes include en-US (English), es (Spanish), ru-RU (Russian) and many others that can be found searching the web
1 parent c81a44c commit 9d06a46

3 files changed

Lines changed: 78 additions & 26 deletions

File tree

java-speech-api.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<content url="file://$MODULE_DIR$">
66
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
77
</content>
8-
<orderEntry type="jdk" jdkName="1.6.0_33" jdkType="JavaSDK" />
8+
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10-
<orderEntry type="library" name="JavaFlacEncoder" level="project" />
10+
<orderEntry type="library" name="javaFlacEncoder-0.2" level="project" />
1111
</component>
1212
</module>
1313

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public void convertWaveToFlac(File inputFile, File outputFile) {
6060
int i = 0;
6161

6262
while (audioInputStream.read(samplesIn, 0, frameSize) != -1) {
63-
if(frameSize != 1){
64-
ByteBuffer bb = ByteBuffer.wrap(samplesIn);
65-
bb.order(ByteOrder.LITTLE_ENDIAN);
66-
short shortVal = bb.getShort();
67-
sampleData[i] = shortVal;
68-
}else{
63+
if (frameSize != 1) {
64+
ByteBuffer bb = ByteBuffer.wrap(samplesIn);
65+
bb.order(ByteOrder.LITTLE_ENDIAN);
66+
short shortVal = bb.getShort();
67+
sampleData[i] = shortVal;
68+
} else {
6969
sampleData[i] = samplesIn[0];
7070
}
7171

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

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Recognizer {
1212
/**
1313
* URL to POST audio data and retrieve results
1414
*/
15-
private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US";
15+
private static final String GOOGLE_RECOGNIZER_URL_NO_LANG = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=";
1616

1717
/**
1818
* Constructor
@@ -25,16 +25,17 @@ public Recognizer() {
2525
* Get recognized data from a Wave file. This method will encode the wave file to a FLAC
2626
*
2727
* @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
2829
* @return Returns a GoogleResponse, with the response and confidence score
2930
* @throws Exception Throws exception if something goes wrong
3031
*/
31-
public GoogleResponse getRecognizedDataForWave(File waveFile) throws Exception {
32+
public GoogleResponse getRecognizedDataForWave(File waveFile, String language) throws Exception {
3233
FlacEncoder flacEncoder = new FlacEncoder();
3334
File flacFile = new File(waveFile + ".flac");
3435

3536
flacEncoder.convertWaveToFlac(waveFile, flacFile);
3637

37-
String response = rawRequest(flacFile);
38+
String response = rawRequest(flacFile, language);
3839

3940
//Delete converted FLAC data
4041
flacFile.delete();
@@ -43,10 +44,10 @@ public GoogleResponse getRecognizedDataForWave(File waveFile) throws Exception {
4344

4445
GoogleResponse googleResponse = new GoogleResponse();
4546

46-
if(parsedResponse != null){
47-
googleResponse.setResponse(parsedResponse[0]);
48-
googleResponse.setConfidence(parsedResponse[1]);
49-
}else{
47+
if (parsedResponse != null) {
48+
googleResponse.setResponse(parsedResponse[0]);
49+
googleResponse.setConfidence(parsedResponse[1]);
50+
} else {
5051
googleResponse.setResponse(null);
5152
googleResponse.setConfidence(null);
5253
}
@@ -58,31 +59,33 @@ public GoogleResponse getRecognizedDataForWave(File waveFile) throws Exception {
5859
* Get recognized data from a Wave file. This method will encode the wave file to a FLAC
5960
*
6061
* @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
6163
* @return Returns a GoogleResponse, with the response and confidence score
6264
* @throws Exception Throws exception if something goes wrong
6365
*/
64-
public GoogleResponse getRecognizedDataForWave(String waveFile) throws Exception {
65-
return getRecognizedDataForWave(new File(waveFile));
66+
public GoogleResponse getRecognizedDataForWave(String waveFile, String language) throws Exception {
67+
return getRecognizedDataForWave(new File(waveFile), language);
6668
}
6769

6870
/**
6971
* Get recognized data from a FLAC file.
7072
*
7173
* @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
7275
* @return Returns a GoogleResponse, with the response and confidence score
7376
* @throws Exception Throws exception if something goes wrong
7477
*/
75-
public GoogleResponse getRecognizedDataForFlac(File flacFile) throws Exception {
76-
String response = rawRequest(flacFile);
78+
public GoogleResponse getRecognizedDataForFlac(File flacFile, String language) throws Exception {
79+
String response = rawRequest(flacFile, language);
7780
String[] parsedResponse = parseResponse(response);
7881

7982
GoogleResponse googleResponse = new GoogleResponse();
8083

8184

82-
if(parsedResponse != null){
85+
if (parsedResponse != null) {
8386
googleResponse.setResponse(parsedResponse[0]);
8487
googleResponse.setConfidence(parsedResponse[1]);
85-
}else{
88+
} else {
8689
googleResponse.setResponse(null);
8790
googleResponse.setConfidence(null);
8891
}
@@ -95,11 +98,60 @@ public GoogleResponse getRecognizedDataForFlac(File flacFile) throws Exception {
9598
* Get recognized data from a FLAC file.
9699
*
97100
* @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
102+
* @return Returns a GoogleResponse, with the response and confidence score
103+
* @throws Exception Throws exception if something goes wrong
104+
*/
105+
public GoogleResponse getRecognizedDataForFlac(String flacFile, String language) throws Exception {
106+
return getRecognizedDataForFlac(new File(flacFile), language);
107+
}
108+
109+
/**
110+
* Get recognized data from a Wave file. This method will encode the wave file to a FLAC.
111+
* This method will automatically set the language to en-US, or English
112+
*
113+
* @param waveFile Wave file to recognize
114+
* @return Returns a GoogleResponse, with the response and confidence score
115+
* @throws Exception Throws exception if something goes wrong
116+
*/
117+
public GoogleResponse getRecognizedDataForWave(File waveFile) throws Exception {
118+
return getRecognizedDataForWave(waveFile, "en-US");
119+
}
120+
121+
/**
122+
* Get recognized data from a Wave file. This method will encode the wave file to a FLAC.
123+
* This method will automatically set the language to en-US, or English
124+
*
125+
* @param waveFile Wave file to recognize
126+
* @return Returns a GoogleResponse, with the response and confidence score
127+
* @throws Exception Throws exception if something goes wrong
128+
*/
129+
public GoogleResponse getRecognizedDataForWave(String waveFile) throws Exception {
130+
return getRecognizedDataForWave(waveFile, "en-US");
131+
}
132+
133+
/**
134+
* Get recognized data from a FLAC file.
135+
* This method will automatically set the language to en-US, or English
136+
*
137+
* @param flacFile FLAC file to recognize
138+
* @return Returns a GoogleResponse, with the response and confidence score
139+
* @throws Exception Throws exception if something goes wrong
140+
*/
141+
public GoogleResponse getRecognizedDataForFlac(File flacFile) throws Exception {
142+
return getRecognizedDataForFlac(flacFile, "en-US");
143+
}
144+
145+
/**
146+
* Get recognized data from a FLAC file.
147+
* This method will automatically set the language to en-US, or English
148+
*
149+
* @param flacFile FLAC file to recognize
98150
* @return Returns a GoogleResponse, with the response and confidence score
99151
* @throws Exception Throws exception if something goes wrong
100152
*/
101153
public GoogleResponse getRecognizedDataForFlac(String flacFile) throws Exception {
102-
return getRecognizedDataForFlac(new File(flacFile));
154+
return getRecognizedDataForFlac(flacFile, "en-US");
103155
}
104156

105157
/**
@@ -109,8 +161,8 @@ public GoogleResponse getRecognizedDataForFlac(String flacFile) throws Exception
109161
* @return Returns the parsed response. Index 0 is response, Index 1 is confidence score
110162
*/
111163
private String[] parseResponse(String rawResponse) {
112-
if(!rawResponse.contains("utterance"))
113-
return null;
164+
if (!rawResponse.contains("utterance"))
165+
return null;
114166

115167
String[] parsedResponse = new String[2];
116168

@@ -130,14 +182,14 @@ private String[] parseResponse(String rawResponse) {
130182
* @return Returns the raw, unparsed response from Google
131183
* @throws Exception Throws exception if something went wrong
132184
*/
133-
private String rawRequest(File inputFile) throws Exception {
185+
private String rawRequest(File inputFile, String language) throws Exception {
134186
URL url;
135187
URLConnection urlConn;
136188
OutputStream outputStream;
137189
BufferedReader br;
138190

139191
// URL of Remote Script.
140-
url = new URL(GOOGLE_RECOGNIZER_URL);
192+
url = new URL(GOOGLE_RECOGNIZER_URL_NO_LANG + language);
141193

142194
// Open New URL connection channel.
143195
urlConn = url.openConnection();

0 commit comments

Comments
 (0)