-
Notifications
You must be signed in to change notification settings - Fork 58
/
02. personal_voice_assistant_'en'.java
94 lines (77 loc) · 3.81 KB
/
02. personal_voice_assistant_'en'.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Please note that this is a simplified version of the code and might require additional modifications and
// the inclusion of proper library dependencies to make it fully functional in a Java environment.
// You would also need to replace "YOUR_WEATHER_API_KEY", "YOUR_SPEECH_SUBSCRIPTION_KEY", and "YOUR_SPEECH_REGION" with your actual API keys and subscription information.
import com.microsoft.cognitiveservices.speech.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class VoiceAssistant {
private static final String WEATHER_API_KEY = "YOUR_WEATHER_API_KEY";
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
public static void main(String[] args) {
try {
SpeechConfig speechConfig = SpeechConfig.fromSubscription("YOUR_SPEECH_SUBSCRIPTION_KEY", "YOUR_SPEECH_REGION");
SpeechRecognizer speechRecognizer = new SpeechRecognizer(speechConfig);
initializeVoice();
System.out.println("Listening...");
Future<SpeechRecognitionResult> task = speechRecognizer.recognizeOnceAsync();
SpeechRecognitionResult speechResult = task.get();
if (speechResult.getReason() == ResultReason.RecognizedSpeech) {
String userCommand = speechResult.getText().toLowerCase();
if (userCommand.equals("exit")) {
speak("Goodbye!");
} else if (userCommand.equals("time")) {
String currentTime = getCurrentDateTime();
speak("The current time is " + currentTime);
} else if (userCommand.startsWith("weather")) {
String city = userCommand.substring(7).trim();
getWeatherReport(city);
} else {
speak("Sorry, I didn't understand that.");
}
}
speechRecognizer.close();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
private static void initializeVoice() {
// Initialize voice settings if required
}
private static void speak(String text) {
// Implement text-to-speech functionality here
}
private static String getCurrentDateTime() {
return DATE_FORMAT.format(new Date());
}
private static void getWeatherReport(String city) {
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + WEATHER_API_KEY + "&units=metric";
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
JSONObject responseBody = new JSONObject(response.body().string());
double temperature = responseBody.getJSONObject("main").getDouble("temp");
double humidity = responseBody.getJSONObject("main").getDouble("humidity");
double windSpeed = responseBody.getJSONObject("wind").getDouble("speed");
System.out.println("Temperature: " + temperature + "°C");
System.out.println("Humidity: " + humidity + "%");
System.out.println("Wind Speed: " + windSpeed + "m/s");
} else {
System.out.println("Error occurred while fetching weather data.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}