-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
87 lines (73 loc) · 2.95 KB
/
Program.cs
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
using System;
using System.Speech.Recognition;
using System.Threading;
namespace DS
{
internal class Program
{
private static SpeechRecognitionEngine recognizer;
static void Main(string[] args)
{
// Initialize the Speech Recognizer
InitializeSpeechRecognition();
// Keep the console open and listen for commands
Console.WriteLine("Virtual Assistant: Listening for commands...");
Console.WriteLine("Say 'hello' to get started!");
// Prevent the application from closing immediately
Thread.Sleep(Timeout.Infinite);
}
private static void InitializeSpeechRecognition()
{
// Create a new SpeechRecognitionEngine object
recognizer = new SpeechRecognitionEngine();
// Set the input to the default microphone
recognizer.SetInputToDefaultAudioDevice();
// Define a set of commands the recognizer will recognize
var commands = new Choices("hello", "open notepad", "exit");
// Create a Grammar based on the defined commands
var grammar = new Grammar(new GrammarBuilder(commands));
// Load the grammar into the recognizer
recognizer.LoadGrammar(grammar);
// Event handler for when speech is recognized
recognizer.SpeechRecognized += Recognizer_SpeechRecognized;
// Start asynchronous recognition
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
private static void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string recognizedText = e.Result.Text.ToLower(); // Convert speech to lowercase for easier matching
// Handle recognized commands
if (recognizedText.Contains("hello"))
{
Console.WriteLine("Virtual Assistant: Hello! How can I assist you?");
}
else if (recognizedText.Contains("open notepad"))
{
OpenApplication("notepad");
}
else if (recognizedText.Contains("exit"))
{
Console.WriteLine("Virtual Assistant: Goodbye!");
Environment.Exit(0);
}
else
{
Console.WriteLine($"Virtual Assistant: I didn't understand '{recognizedText}'. Try again!");
}
}
// Method to launch an application
private static void OpenApplication(string appName)
{
try
{
// Try to launch the application
System.Diagnostics.Process.Start(appName);
Console.WriteLine($"Virtual Assistant: Opening {appName}...");
}
catch (Exception ex)
{
Console.WriteLine($"Virtual Assistant: Sorry, I couldn't open {appName}. Error: {ex.Message}");
}
}
}
}