how to execute voice google search - c#

I can't search in google using my voice. I disabled search by default and can only be enabled by saying the word "search". I separated the words and phrases to be searched for to another file so that it wont get mixed with my recognition file and the response file. I placed the path file under the if statement of "search" so that it can only be access when the I speak "search"
public partial class Form1 : Form
{
//user and jarvis texts
string[] grammarFile = (File.ReadAllLines(#"C:\Friday AI\user.txt.txt"));
string[] responseFile = (File.ReadAllLines(#"C:\Friday AI\jarvis.txt.txt"));
//speech synthesis
SpeechSynthesizer speechSynth = new SpeechSynthesizer();
//speech recognition
Choices grammarList = new Choices();
SpeechRecognitionEngine speechRecognition = new SpeechRecognitionEngine();
public Boolean search = false;
public Form1()
{
//initialize grammarfile
grammarList.Add(grammarFile);
Grammar grammar = new Grammar(new GrammarBuilder(grammarList));
try
{
speechRecognition.RequestRecognizerUpdate();
speechRecognition.LoadGrammar(grammar);
speechRecognition.SpeechRecognized += rec_SpeechRecognized;
speechRecognition.SetInputToDefaultAudioDevice();
speechRecognition.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return;
}
private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string result = e.Result.Text;
int resp = Array.IndexOf(grammarFile, result);
if (search)
{
Process.Start(#"chrome.exe", "--incognito https://www.google.com/search?q=" + result);
label5.Text = "Enabled";
}
if (wake == true)
{
if (result.Contains("search"))
{
search = true;
string[] globalsearch = (File.ReadAllLines(#"C:\Friday AI\global search words.txt"));
grammarList.Add(globalsearch);
label5.Text = "Enabled";
}

Related

Hotkey with MouseKeyHook not catching event in C#

I'm building a word lookup application that runs in system tray and when you scan a block of a few words and then press a hotkey combination like Shift + C + V for example.
However, the problem is that when I test it on my personal machine, it works fine. But when through Tester's machine, it is not possible to catch the hotkey press event.
I'm using MouseKeyHook's library.
May everyone help you
Here is my code
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4
}
public Main()
{
System.Diagnostics.Process myProcess = System.Diagnostics.Process.GetCurrentProcess();
myProcess.PriorityClass = System.Diagnostics.ProcessPriorityClass.High;
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
notify.Visible = true;
this.FormBorderStyle = FormBorderStyle.None;
this.Padding = new Padding(borderSize);
this.panel1.BackColor = borderColor;
this.BackColor = borderColor;
if (File.Exists("hotkey.txt") == false)
{
string noidung = "Shift+F";
File.WriteAllText("hotkey.txt", noidung);
}
manage = new ManageDictionary();
conn = ConnectDB.Connect();
ctrl = new ControlData(m_Events, data, txtSearch);
ctrl.SubscribeGlobal();
resetHotKey();
}
public void resetConnection()
{
conn = ConnectDB.Connect();
}
public void resetHotKey()
{
string[] hot;
string key;
this.hotkey_Events = Hook.GlobalEvents();
if (File.Exists("hotkey.txt"))
{
hot = File.ReadAllLines("hotkey.txt");
key = hot[0];
}
else
{
key = "Shift+F";
}
var subhotkey = Combination.FromString(key);
Action action = () => {
string keyword = Clipboard.GetText().Trim();
if (keyword.Equals(""))
return;
try
{
conn.Open();
ArrayList re;
re = manage.findWord(conn, keyword);
if (re.Count == 0)
{
Dictionary a = new Dictionary(0, "Không tìm thấy kết quả", "", "", "", "");
re.Add(a);
Notification noti = new Notification(re, keyword);
Thread.Sleep(1000);
noti.Show();
}
else
{
Notification noti = new Notification(re, keyword);
Thread.Sleep(1000);
noti.Show();
}
}
catch (Exception)
{
MessageBox.Show("Kết nối thất bại");
}
conn.Close();
};
var assignment = new Dictionary<Combination, Action>
{
{subhotkey, action}
};
this.hotkey_Events.OnCombination(assignment);
}

SAPI does not implement phonetic alphabet selection. Speech Command App

In my speech command app, I am loading files from an external source, processing that data and loading them into a list of possible commands for execution
When I run the app, I get the message in the console
Main.exe Information: 0: SAPI does not implement phonetic alphabet selection.
I tried solutions such as adding
gram.Culture = New System.Globalization.CultureInfo("en-GB")
I think this is either outdated or does not work on this type of WPF application
Any tips?
Code:
{
InitializeComponent();
}
SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder builder = new PromptBuilder();
SpeechRecognitionEngine recog = new SpeechRecognitionEngine();
private System.Windows.Input.Key k;
private int vkey;
private Choices cmd;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
b1.IsEnabled = false;
Choices list = new Choices();
cmd = Singleton.getInstance().getChoices();
list.Add(new string[] { "hello", "test", "it works", "sup", "windows", "grenade" });
Grammar gr = new Grammar(new GrammarBuilder(cmd));
try
{
recog.RequestRecognizerUpdate();
recog.LoadGrammar(gr);
recog.SpeechRecognized += Recog_SpeechRecognized;
recog.SetInputToDefaultAudioDevice();
recog.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return;
}
}

No Book Results from ISBNDB with Valid ISBN

I've got a weird issue here. I'll start by explaining my program:
I have a C# application. The main goal of the program is to get information about a book based on its ISBN. The ISBN is passed to the program via a TCP/IP scanner on an Android device. The ISBN is then put into a valid URL which is used to grab the XML data from ISBNDB.com.
The issue that I am having is this:
When I query an ISBN typed into a TextBox, the program works fine. When I query an ISBN scanned from the reader, it returns 'No Results'
I have implemented various ways to try and get to the bottom of this case. Right before the XML is read, I have a message box show me the XML that it received:
As you can see, it shows no results. However, when I visit the URL (Also gotten from within the program):
I get this in Microsoft Edge:
Which, is exactly what I would think the application would get as well.
Does anyone know what is going on? If so, what can I do to fix it and how can my code be improved to eliminate this error?
For those interested, here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Threading;
using System.Diagnostics;
namespace LibraryBookLister
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string XML = "";
private void btnQuery_Click(object sender, EventArgs e)
{
GetXMLBarcodeData();
}
private void GetXMLBarcodeData()
{
string Barcode4 = textBarcode.Text;
MessageBox.Show(Barcode4);
string barcode = Barcode4;
StringBuilder output = new StringBuilder();
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Set the reader settings object to use the resolver.
if(barcode.Length > 13)
{
barcode = barcode.Remove(14);
MessageBox.Show(barcode);
}
string xmlString = #"?access_key=IDC057UX&results=details&index1=isbn&value1=" + barcode;
MessageBox.Show("GEttting book info for : " + barcode);
Uri baseUri = new Uri("https://isbndb.com/api/books.xml");
Uri fulluri = resolver.ResolveUri(baseUri, xmlString);
MessageBox.Show("Now Getting The URL: " + fulluri.ToString());
Process.Start(fulluri.ToString());
StringBuilder sb = new StringBuilder();
XmlReader readesr = XmlReader.Create(fulluri.ToString());
MessageBox.Show("REading data from " + fulluri.ToString());
while (readesr.Read())
{
sb.AppendLine(readesr.ReadOuterXml());
}
string XMLs = sb.ToString();
XML = XMLs;
MessageBox.Show("XML : " + XML);
GetXMLStuff();
}
public void GetXMLStuff()
{
tcplistener.Stop();
XmlDocument doc = new XmlDocument();
doc.LoadXml(XML);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/ISBNdb/BookList");
List<Book> books = new List<Book>();
foreach (XmlNode node in nodes)
{
Book book = new Book();
try
{
if (node.SelectSingleNode("BookData/AuthorsText").InnerText == null)
{
MessageBox.Show("Could not find this book. Please enter data by hand.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBarcode.Clear();
return;
}
}
catch
{
MessageBox.Show("Could not find this book. Please enter data by hand.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// textBarcode.Clear();
return;
}
book.author = node.SelectSingleNode("BookData/AuthorsText").InnerText;
book.title = node.SelectSingleNode("BookData/Title").InnerText;
book.ISBN = node.SelectSingleNode("BookData").Attributes["isbn"].Value;
books.Add(book);
MessageBox.Show(book.author);
addInfo(book.author, book.title, book.ISBN);
textBarcode.Clear();
}
// MessageBox.Show("Total books: " + books.Count);
}
private void addInfo(string Author, string Title, string ISBN)
{
textAuthor.Text = Author;
textTitle.Text = Title;
textISBN.Text = ISBN;
}
class Book
{
public string ISBN;
public string title;
public string author;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
int time = 10;
bool cancel = false;
private void timer1_Tick(object sender, EventArgs e)
{
if(time > 0)
{
labelTime.Text = time.ToString();
button1.Text = "Change Data";
cancel = true;
labelTime.Visible = true;
time--;
// MessageBox.Show(time.ToString());
}
if(time <= 0)
{
cancel = false;
button1.Text = "Add to List";
timer1.Stop();
time = 10;
labelTime.Visible = false;
MessageBox.Show("Submitting");
}
}
private void button1_Click(object sender, EventArgs e)
{
if(cancel)
{
timer1.Stop();
labelTime.Visible = false;
time = 10;
cancel = false;
button1.Text = "Add to List";
}
else
{
timer1.Start();
}
}
private void button2_Click(object sender, EventArgs e)
{
Thread tcpServer = new Thread(new ParameterizedThreadStart(TCPServerRun));
//TCPServerRun();
tcpServer.Start();
}
bool on = true;
TcpListener tcplistener = new TcpListener(IPAddress.Any, 5004);
private void TCPServerRun(object test)
{
try
{
MessageBox.Show("Starting Listener");
tcplistener.Start();
}
catch { MessageBox.Show("COULDNT START TPCSERVER"); return; }
while (on == true)
{
try
{
TcpClient client = tcplistener.AcceptTcpClient();
Thread tcpHandlerThread = new Thread(new ParameterizedThreadStart(tcpHandler));
// tcpHandlerThread.Start(client);
tcpHandler(client);
}
catch
{
tcplistener.Stop();
// MessageBox.Show("Stopping Listener");
}
}
}
string bCode = "";
private void tcpHandler(object client)
{
TcpClient mClient = (TcpClient)client;
NetworkStream stream = mClient.GetStream();
byte[] message = new byte[1024];
stream.Read(message, 0, message.Length);
bCode = Encoding.ASCII.GetString(message);
stream.Close();
mClient.Close();
MessageBox.Show(bCode);
this.textBarcode.Text = bCode;
GetXMLBarcodeData();
}
}
}
Possible Hint: Could it have something to do with how I have threads working?
*Edit: * **I have updated the code to have the barcode be put in a textBox and then used to fetch the data. This does not seem to work either because it 'Cannot access the control on a thread other than on which it was created'
If manual user input succeed while automated input fail, the simplest hack is just replacing the automated input to a call to manual control BeginInvoke. For your code this would be :
textBarcode.BeginInvoke(new Action(() => {
textBarcode.Text = bCode;
GetXMLBarcodeData();
}));

How could i save listbox items even if i restart the app?

How could i save listbox items even if i restart the app in the windows phone app. I want them to be sort of saved somehow, in a file, and then read them on the next start of the app. Please help..
Ok i am updating with a code:
public partial class MainPage : PhoneApplicationPage
{
#region VariableDeclaration
DispatcherTimer timer = new DispatcherTimer();
WebClient client = new WebClient();
WebBrowserTask Facebook = new WebBrowserTask();
WebBrowserTask YouTube = new WebBrowserTask();
WebBrowserTask Odnoklassniki = new WebBrowserTask();
WebBrowserTask Vkontakte = new WebBrowserTask();
List<ItemFormat> Items = new List<ItemFormat>();
DispatcherTimer PopulateIsoFile = new DispatcherTimer();
string SongBuffer;
int c = 1;
string Time;
#endregion
#region AppInit
// Constructor
public MainPage()
{
InitializeComponent();
if (Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable == false)
{
MessageBox.Show("No internet connection", "Error", MessageBoxButton.OKCancel);
}
else
{
if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
{
PauseBtn.Visibility = Visibility.Visible;
PlayBtn.Visibility = Visibility.Collapsed;
}
else
{
BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri("http://air-online2.hitfm.md/hitfm.mp3"), "HITFM", "Включи себя", null, null);
PlayBtn.Visibility = Visibility.Visible;
PauseBtn.Visibility = Visibility.Collapsed;
}
BackgroundAudioPlayer.Instance.PlayStateChanged += Instance_PlayStateChanged;
SlideView.Begin();
SlideView.Completed += SlideView_Completed;
SlideView.AutoReverse = true;
}
timer.Interval = TimeSpan.FromSeconds(30);
timer.Tick += timer_Tick;
timer.Start();
Loaded += timer_Tick;
}
#region DownloadTrackInfo
void timer_Tick(object sender, EventArgs e)
{
try
{
client.Encoding = System.Text.Encoding.UTF8;
client.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.Now.ToString();
client.DownloadStringAsync(new Uri("http://air-online2.hitfm.md/status_hitfm.xsl"));
client.DownloadStringCompleted += client_DownloadStringCompleted;
}
catch (System.Net.WebException)
{
}
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
string[] raw = e.Result.Substring(166).Split('-');
if (raw[0].Contains(":"))
{
Artist.Text = raw[0].Replace(":", string.Empty).Substring(0, raw[0].Length - 1);
Title.Text = raw[1].Substring(1);
}
else
{
Artist.Text = raw[0];
Title.Text = raw[1].Substring(1);
}
TitleProgress.Visibility = Visibility.Collapsed;
Title.Foreground = new SolidColorBrush(Colors.White);
Artist.Foreground = new SolidColorBrush(Colors.White);
if (DateTime.Now.Minute < 10)
{
Time = "0" + DateTime.Now.Minute.ToString();
}
else
{
Time = DateTime.Now.Minute.ToString();
}
ItemFormat Item = new ItemFormat(e.Result.Substring(166).Replace(":", string.Empty), c, DateTime.Now.Hour.ToString() + ":" + Time);
if ((!(Item.Song == SongBuffer)) || (Recent.Items.Count == 0))
{
Recent.Items.Add(Item);
SongBuffer = Item.Song;
c += 1;
}
}
catch (System.SystemException)
{
}
}
}
public class ItemFormat
{
public string Song { get; set; }
public int Count { get; set; }
public string Time { get; set; }
public ItemFormat(string Song, int count, string time)
{
this.Song = Song;
this.Count = count;
this.Time = time;
}
}
}
I use this list box for a sort of a playlist for a radio. But i need my items to be saved even when the user clicks back button or is under lock screen. Please help me save my dear items.
There are several ways to store data between "sessions":
Using files in the IsolatedStorage to serialize to/deserialize from.
Using IsolatedStorageSettings for the same purpose, but with smaller amount of data.
Using database, either SQL CE or sqlite
I suggest that you use the first method because it is the easiest one and you will get the least errors with it. Simply serialize the data to the file whenever you need to, either on application closing or when it changes. You then load the data on startup from the file (if it exists) and fill the initial list.

Searching the web through C# Speech

Okay. I have been working on a speech recognition program. I want to make it when you say something like "search the web for images of cats" it will open the browser and search for images of cats. I made a textbox to receive the inputs of the user's speech, but I have been stumped to find out how to retrieve the text from the textbox and send it to the web.
Where textbox1.text = speech; is where the speech is converted to text.
Heres my code so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
using System.Xml;
using System.Threading;
using System.Net;
namespace Jarvis
{
public partial class Form1 : Form
{
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
SpeechSynthesizer JARVIS = new SpeechSynthesizer();
string QEvent;
string ProcWindow;
string BrowseDirectory;
string Temperature;
string Condition;
string Humidity;
string WindSpeed;
string Town;
string TFCond;
string TFHigh;
string TFLow;
double timer = 10;
int count = 1;
Random rnd = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammar(new DictationGrammar());
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\commands.txt")))));
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\greetings.txt")))));
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\programs.txt")))));
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\social.txt")))));
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
var game = 0;
int ranNum = rnd.Next(1, 10);
int ranNumm = rnd.Next(1, 15);
bool wh = false;
bool sleep = false;
string speech = e.Result.Text;
textBox1.Text = speech;
if (textBox1.Text == url)
{
}
switch (speech)
{
//GREETINGS
case "hello":
case "hey":
case "hey jarvis":
if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
else if (ranNum > 5) { JARVIS.Speak("Hi"); }
break;
}
}
private void ShutdownTimer_Tick(object sender, EventArgs e)
{
if (timer == 0)
{
lblTimer.Visible = false;
ComputerTermination();
ShutdownTimer.Enabled = false;
}
else if (QEvent == "abort")
{
timer = 10;
lblTimer.Visible = false;
ShutdownTimer.Enabled = false;
}
else
{
timer = timer - .01;
lblTimer.Text = timer.ToString();
}
}
private void ComputerTermination()
{
switch (QEvent)
{
case "shutdown":
System.Diagnostics.Process.Start("shutdown", "-s");
break;
case "logoff":
System.Diagnostics.Process.Start("shutdown", "-l");
break;
case "restart":
System.Diagnostics.Process.Start("shutdown", "-r");
break;
}
}
private void StopWindow()
{
System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(ProcWindow);
foreach (System.Diagnostics.Process proc in procs)
{
proc.CloseMainWindow();
}
}
private void LoadDirectory()
{
lstCommands.Items.Clear();
switch (QEvent)
{
case "music directory":
string[] files = Directory.GetFiles(BrowseDirectory, ".mp3", SearchOption.AllDirectories);
foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory, ""));}
break;
case "video directory":
files = Directory.GetFiles(BrowseDirectory, "*", SearchOption.AllDirectories);
foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory, ""));}
break;
case "picture directory":
files = Directory.GetFiles(BrowseDirectory, "*", SearchOption.AllDirectories);
foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory, ""));}
break;
case "load directory":
files = Directory.GetFiles(BrowseDirectory, "*", SearchOption.AllDirectories);
foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory + "\\", ""));}
break;
}
}
private void lstCommands_SelectedIndexChanged(object sender, EventArgs e)
{
Object open = BrowseDirectory + lstCommands.SelectedItem;
try
{ System.Diagnostics.Process.Start(open.ToString()); }
catch { open = BrowseDirectory + "\\" + lstCommands.SelectedItem; System.Diagnostics.Process.Start(open.ToString()); }
}
private void GetWeather()
{
string query = String.Format("http://weather.yahooapis.com/forecastrss?w=2434216");
XmlDocument wData = new XmlDocument();
wData.Load(query);
XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);
Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;
WindSpeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;
TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;
TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;
TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value;
}
}
}
Why do you need to retrieve the text from the textbox, when you have the text
string speech = e.Result.Text;
already? All you need to do is URLEncode the string (sample function here) and open a browser window with the URL.
Use Google image search API developer, get image url, declare xml document, make variables.
https://developers.google.com/image-search/v1/devguide

Categories

Resources