Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- SpeechSynthesizer s = new SpeechSynthesizer();
- Boolean wake = true;
- Choices list = new Choices();
- float temp;
- public Form1()
- {
- s.SelectVoiceByHints(VoiceGender.Female);
- SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
- list.Add(new String[] { "Hello", "how are you", "what time is it", "what is today", "open google", "sleep", "wake"
- , "play mameenoh antche", "play money money", "open steam", "close steam", "what's the weather", "what's the temperature"});
- Grammar gr = new Grammar(new GrammarBuilder(list));
- try {
- rec.RequestRecognizerUpdate();
- rec.LoadGrammar(gr);
- rec.SpeechRecognized += rec_SpeechRecognized;
- rec.SetInputToDefaultAudioDevice();
- rec.RecognizeAsync(RecognizeMode.Multiple);
- }
- catch { return; }
- InitializeComponent();
- }
- public void say(String h)
- {
- textBox2.AppendText(h + "\n");
- s.Speak(h);
- }
- private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
- {
- if(wake == true) textBox1.AppendText(e.Result.Text + "\n");
- String r = e.Result.Text;
- if (r == "wake") { wake = true; label3.Text = "State: Awake"; }
- if (r == "sleep") { wake = true; label3.Text = "State: Sleep"; }
- if (wake == true)
- {
- if (r == "Hello") say("Hi");
- if (r == "how are you") say("Great, and you");
- if (r == "what time is it") say(DateTime.Now.ToString("h:mm tt"));
- if (r == "what is today") say(DateTime.Now.ToString("M/d/yyyy"));
- if (r == "open google") Process.Start("http://google.com");
- if (r == "play money money") Process.Start("http://www.youtube.com/watch?v=EOeANoEsV1s");
- if (r == "play mameenoh antche") Process.Start("http://www.youtube.com/watch?v=3m5dH3T8BwU");
- if (r == "open steam") Process.Start(@"C:\Program Files (x86)\Steam\Steam.exe");
- if (r == "close steam") killProg("Steam");
- if (r == "what's the weather") say("The temperature is " + CheckWeather() + " degrees");
- if (r == "close steam") killProg("Steam");
- }
- }
- public static void killProg(String s)
- {
- System.Diagnostics.Process[] procs = null;
- try
- {
- procs = Process.GetProcessesByName(s);
- Process prog = procs[0];
- if (!prog.HasExited)
- {
- prog.Kill();
- }
- }
- finally
- {
- if (procs != null)
- {
- foreach (var p in procs)
- {
- p.Dispose();
- }
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- public float CheckWeather()
- {
- WeatherAPI DataAPI = new WeatherAPI("Ruse");
- temp = DataAPI.GetTemp();
- return temp;
- }
- }
- class WeatherAPI
- {
- public WeatherAPI(string city)
- {
- SetCurrentURL(city);
- xmlDocument = GetXML(CurrentURL);
- }
- public float GetTemp()
- {
- XmlNode temp_node = xmlDocument.SelectSingleNode("//temperature");
- XmlAttribute temp_value = temp_node.Attributes["value"];
- string temp_string = temp_value.Value;
- return float.Parse(temp_string);
- }
- private const string APIKEY = "e4f97b910e2968dfca0763ae84214a71";
- private string CurrentURL;
- private XmlDocument xmlDocument;
- private void SetCurrentURL(string location)
- {
- CurrentURL = "http://api.openweathermap.org/data/2.5/weather?q="
- + location + "&mode=xml&units=metric&APPID=" + APIKEY;
- }
- private XmlDocument GetXML(string CurrentURL)
- {
- using (WebClient client = new WebClient())
- {
- string xmlContent = client.DownloadString(CurrentURL);
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.LoadXml(xmlContent);
- return xmlDocument;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement