/// <summary> /// Truncates the string with response to JSON with results and parses it to save as JsonBooks.SearchResult. /// </summary> /// <param name="searchResponse">String with the response from Google Books.</param> /// <returns>Returns a JsonBooks.SearchResult object with the search results.</returns> private static JsonBooks.SearchResult ParseBooksSearchResults(String searchResponse) { //Create a JsonBooks.SearchResult object for results JsonBooks.SearchResult parsingResult = new JsonBooks.SearchResult(); //Find a match to result pattern in the response from Google Books Match rgxResult = rgx.Match(searchResponse); //Get a string that matches result pattern String processedResponse = rgxResult.ToString(); //Try to parse it try { //Check if the end of this string looks as expected or not if (processedResponse.Substring(processedResponse.Length - responseTail.Length, responseTail.Length) == responseTail) { //Truncate that string to JSON format string processedResponse = processedResponse.Substring(0, processedResponse.Length - responseTail.Length); //Parse it parsingResult = JsonBooks.Parse(processedResponse); } else { parsingResult.search_error = "Error: Unexpected response format. Server response: " + searchResponse; } } catch (Exception e) { parsingResult.search_error = "Error: " + e.Message + ". Server response: " + searchResponse; } return(parsingResult); }
public MainForm() { InitializeComponent(); /* * //Test of the audio recording and speech recognition * String wavFileName = @"tmp.wav"; * int recordingLength = 5000; * * AudioRecording.RecordAudio(wavFileName, recordingLength); * * String speechRecognitionResult = ""; * speechRecognitionResult = SpeechRecognition.GetRecognizedText(wavFileName); * MessageBox.Show(speechRecognitionResult, "Recognition result", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); */ //Test of the text search for books String bookIdentifier = "6CgNP0U7AeAC"; //Google Books Search format //String requestedText = "job"; String requestedText = "job afte"; //requestedText = speechRecognitionResult; JsonBooks.SearchResult searchResult = GoogleBooksSearch.GetResult(bookIdentifier, requestedText); requestedText += ""; //Breakpoint }
/// <summary> /// Gives a result of the search of requested text in requested book. /// </summary> /// <param name="bookIdentifier">String with book ID in a Google Books format.</param> /// <param name="requestedText">String with the text to search fo.r</param> /// <returns>Returns a JsonBooks.SearchResult object with the search results.</returns> public static JsonBooks.SearchResult GetResult(String bookIdentifier, String requestedText) { //Create a JsonBooks.SearchResult object for result JsonBooks.SearchResult result = new JsonBooks.SearchResult(); try { //Form a Uri object with the to Google Books Uri requestUri = FormRequestUri(bookIdentifier, requestedText); //Get a string with the response from server String searchResponse = RequestGoogleBooks(requestUri); //Parse this string and get a JsonBooks.SearchResult object with the search results result = ParseBooksSearchResults(searchResponse); } catch (Exception e) { result.search_error = "Error: " + e.Message; } return(result); }