Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. SpeechSynthesizer s = new SpeechSynthesizer();
  4. Boolean wake = true;
  5. Choices list = new Choices();
  6.  
  7. float temp;
  8. public Form1()
  9. {
  10. s.SelectVoiceByHints(VoiceGender.Female);
  11. SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
  12.  
  13. list.Add(new String[] { "Hello", "how are you", "what time is it", "what is today", "open google", "sleep", "wake"
  14. , "play mameenoh antche", "play money money", "open steam", "close steam", "what's the weather", "what's the temperature"});
  15. Grammar gr = new Grammar(new GrammarBuilder(list));
  16.  
  17. try {
  18. rec.RequestRecognizerUpdate();
  19. rec.LoadGrammar(gr);
  20. rec.SpeechRecognized += rec_SpeechRecognized;
  21. rec.SetInputToDefaultAudioDevice();
  22. rec.RecognizeAsync(RecognizeMode.Multiple);
  23. }
  24. catch { return; }
  25.  
  26. InitializeComponent();
  27. }
  28.  
  29. public void say(String h)
  30. {
  31. textBox2.AppendText(h + "\n");
  32. s.Speak(h);
  33. }
  34.  
  35. private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  36. {
  37. if(wake == true) textBox1.AppendText(e.Result.Text + "\n");
  38. String r = e.Result.Text;
  39.  
  40. if (r == "wake") { wake = true; label3.Text = "State: Awake"; }
  41. if (r == "sleep") { wake = true; label3.Text = "State: Sleep"; }
  42.  
  43. if (wake == true)
  44. {
  45. if (r == "Hello") say("Hi");
  46.  
  47. if (r == "how are you") say("Great, and you");
  48.  
  49. if (r == "what time is it") say(DateTime.Now.ToString("h:mm tt"));
  50.  
  51. if (r == "what is today") say(DateTime.Now.ToString("M/d/yyyy"));
  52. if (r == "open google") Process.Start("http://google.com");
  53. if (r == "play money money") Process.Start("http://www.youtube.com/watch?v=EOeANoEsV1s");
  54. if (r == "play mameenoh antche") Process.Start("http://www.youtube.com/watch?v=3m5dH3T8BwU");
  55. if (r == "open steam") Process.Start(@"C:\Program Files (x86)\Steam\Steam.exe");
  56. if (r == "close steam") killProg("Steam");
  57. if (r == "what's the weather") say("The temperature is " + CheckWeather() + " degrees");
  58. if (r == "close steam") killProg("Steam");
  59. }
  60. }
  61.  
  62. public static void killProg(String s)
  63. {
  64. System.Diagnostics.Process[] procs = null;
  65. try
  66. {
  67. procs = Process.GetProcessesByName(s);
  68. Process prog = procs[0];
  69. if (!prog.HasExited)
  70. {
  71. prog.Kill();
  72. }
  73. }
  74. finally
  75. {
  76. if (procs != null)
  77. {
  78. foreach (var p in procs)
  79. {
  80. p.Dispose();
  81. }
  82. }
  83. }
  84. }
  85.  
  86. private void Form1_Load(object sender, EventArgs e)
  87. {
  88.  
  89. }
  90.  
  91. public float CheckWeather()
  92. {
  93. WeatherAPI DataAPI = new WeatherAPI("Ruse");
  94. temp = DataAPI.GetTemp();
  95. return temp;
  96. }
  97. }
  98.  
  99. class WeatherAPI
  100. {
  101. public WeatherAPI(string city)
  102. {
  103. SetCurrentURL(city);
  104. xmlDocument = GetXML(CurrentURL);
  105. }
  106.  
  107. public float GetTemp()
  108. {
  109. XmlNode temp_node = xmlDocument.SelectSingleNode("//temperature");
  110. XmlAttribute temp_value = temp_node.Attributes["value"];
  111. string temp_string = temp_value.Value;
  112. return float.Parse(temp_string);
  113. }
  114.  
  115. private const string APIKEY = "e4f97b910e2968dfca0763ae84214a71";
  116. private string CurrentURL;
  117. private XmlDocument xmlDocument;
  118.  
  119. private void SetCurrentURL(string location)
  120. {
  121. CurrentURL = "http://api.openweathermap.org/data/2.5/weather?q="
  122. + location + "&mode=xml&units=metric&APPID=" + APIKEY;
  123. }
  124.  
  125. private XmlDocument GetXML(string CurrentURL)
  126. {
  127. using (WebClient client = new WebClient())
  128. {
  129. string xmlContent = client.DownloadString(CurrentURL);
  130. XmlDocument xmlDocument = new XmlDocument();
  131. xmlDocument.LoadXml(xmlContent);
  132. return xmlDocument;
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement