public WolframAlphaQueryResult LoadResponse(WolframAlphaQuery Query)
    {
        if (Query.APIKey == "")
        {
            if (this.APIKey == "")
            {
                throw new Exception("To use the Wolfram Alpha API, you must specify an API key either through the parsed WolframAlphaQuery, or on the WolframAlphaEngine itself.");
            }
            Query.APIKey = this.APIKey;
        }

        if (Query.Asynchronous == true && Query.Format == WolframAlphaQuery.WolframAlphaQueryFormat.HTML)
        {
            throw new Exception("Wolfram Alpha does not allow asynchronous operations while the format for the query is not set to \"HTML\".");
        }

        var serverAPIv2 = "http://api.wolframalpha.com/v2/query";
        var WebRequest  = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(serverAPIv2 + Query.FullQueryString);

        //System.Net.HttpWebRequest WebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://preview.wolframalpha.com/api/v1/query.jsp" + Query.FullQueryString);
        WebRequest.KeepAlive = true;
        string Response = new StreamReader(WebRequest.GetResponse().GetResponseStream()).ReadToEnd();

        return(LoadResponse(Response));
    }
        /// <summary>
        /// Run a query.
        /// </summary>
        /// <param name="query">The query to run</param>
        /// <returns>A query result</returns>
        public WolframAlphaQueryResult LoadResponse(WolframAlphaQuery query)
        {
            if (String.IsNullOrEmpty(query.APIKey))
            {
                if (String.IsNullOrEmpty(this.APIKey))
                    throw new Exception("To use the Wolfram Alpha API v2, you must specify an API key either through the WolframAlphaQuery, or on the WolframAlphaEngine itself.");
                query.APIKey = this.APIKey;
            }

            if (query.Asynchronous && query.Format == WolframAlphaQueryFormat.html)
                throw new Exception("Wolfram Alpha does not allow asynchronous operations while the format for the query is not set to \"HTML\".");
            // Please see ValidateQuery(WolframAlphaQuery query) above

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(String.Format("{0}{1}{2}", WolframAlphaEngine.baseUrl, WolframAlphaEngine.baseUrlQuery, query.FullQueryString));
            webRequest.KeepAlive = true;
            string response = new StreamReader(webRequest.GetResponse().GetResponseStream()).ReadToEnd();

            return this.LoadResponse(response);
        }
        // or something like this
        public static void Main(string[] args)
        {
            WolframAlphaEngine engine = new WolframAlphaEngine("your-key-here");
            WolframAlphaQuery query = new WolframAlphaQuery()
            {
                Query = "1500kWh in Joules",
                Format = WolframAlphaQueryFormat.plaintext
            };

            WolframAlphaValidationResult validation = engine.ValidateQuery(query);
            if (validation.ErrorOccured)
            {
                Console.WriteLine("Couldn't comprehend: {0}", validation.ParseData);
                return;
            }
            WolframAlphaQueryResult result = engine.LoadResponse(query);
            WolframAlphaSubPod subPod = result.Pods.Where(x => x.Title == "Result").FirstOrDefault().SubPods.FirstOrDefault();
            if (subPod != null)
                Console.WriteLine(subPod.PodText);
        }
示例#4
0
        public void ShowAndCalculate(string query, string appkey)
        {
            this.Show();
            Properties.Settings.Default.WA_RespSend++;
            Properties.Settings.Default.Save();
            Engine = new WolframAlphaEngine(appkey);//KLQJ9W-5UU8EGQUG3
            WolframAlphaQuery WAQ = new WolframAlphaQuery();

            WAQ.APIKey       = appkey;
            WAQ.MoreOutput   = false;
            WAQ.Asynchronous = false;
            WAQ.AllowCaching = false;
            WAQ.Query        = query;
            WAQ.TimeLimit    = 30000;
            WAQ.Format       = WolframAlphaQueryFormat.PlainText + "," + WolframAlphaQueryFormat.Image;

            pictureLoadAnim.Image = Properties.Resources.anim_0;
            loadingslide          = 0;
            timerAnim.Start();
            del = new Delegate(Engine.LoadResponse);
            del.BeginInvoke(WAQ, new AsyncCallback(CallBackFunc), null);
        }
	public WolframAlphaQueryResult LoadResponse(WolframAlphaQuery Query)
	{
		if (Query.APIKey == "") {
			if (this.APIKey == "") {
				throw new Exception("To use the Wolfram Alpha API, you must specify an API key either through the parsed WolframAlphaQuery, or on the WolframAlphaEngine itself.");
			}
			Query.APIKey = this.APIKey;
		}

		if (Query.Asynchronous == true && Query.Format == WolframAlphaQuery.WolframAlphaQueryFormat.HTML) {
			throw new Exception("Wolfram Alpha does not allow asynchronous operations while the format for the query is not set to \"HTML\".");
		}
		
		var serverAPIv2 = "http://api.wolframalpha.com/v2/query";
		var WebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(serverAPIv2 + Query.FullQueryString);
		//System.Net.HttpWebRequest WebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://preview.wolframalpha.com/api/v1/query.jsp" + Query.FullQueryString);
		WebRequest.KeepAlive = true;
		string Response = new StreamReader(WebRequest.GetResponse().GetResponseStream()).ReadToEnd();

		return LoadResponse(Response);

	}
示例#6
0
	public void Main(string apiKey, string WolframAlphaSearchTerms)
	{
		Engine = new WolframAlphaEngine();
		
		//Try to delete the log file if it already exists.
		/*try {
			File.Delete(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Wolfram Alpha wrapper log.log");
		} catch {
		}*/

		//Define what our application ID is.
		string WolframAlphaApplicationID = "beta824g1";

		//Define what we want to search for.
		//string WolframAlphaSearchTerms = "england";

		//Print out what we're about to do in the console.
		Output("Getting response for the search terms \"" + WolframAlphaSearchTerms , 0, ConsoleColor.White);

		//Use the engine to get a response, from the application ID specified, and the search terms.
		//Engine.LoadResponse(WolframAlphaSearchTerms, WolframAlphaApplicationID);
		
		var query = new WolframAlphaQuery();
		query.APIKey = apiKey;
		query.Format = "plaintext";
		query.Query = WolframAlphaSearchTerms; //"england"; //"what day is today?"; 
		//Engine.LoadResponse(WolframAlphaSearchTerms);
		Engine.LoadResponse(query);
		
		//Print out a message saying that the last task was successful.
		Output("Response injected.", 0, ConsoleColor.White);

		//Make 2 empty spaces in the console.
		Output("", 0, ConsoleColor.White);
		
		Output("Response details", 1, ConsoleColor.Blue);
		
		"Showing details for Engine: {0}".info(Engine);	
		
		//Print out how many different pods that were found.
		Output("Pods found: " + Engine.QueryResult.NumberOfPods, 1, ConsoleColor.White);
		Output("Query pasing time: " + Engine.QueryResult.ParseTiming + " seconds", 1, ConsoleColor.White);
		Output("Query execution time: " + Engine.QueryResult.Timing + " seconds", 1, ConsoleColor.White);

		int PodNumber = 1;


		foreach (WolframAlphaPod Item in Engine.QueryResult.Pods) {
			//Make an empty space in the console.
			Output("", 0, ConsoleColor.White);

			Output("Pod " + PodNumber, 2, ConsoleColor.Red);

			Output("Sub pods found: " + Item.NumberOfSubPods, 2, ConsoleColor.White);
			Output("Title: \"" + Item.Title + "\"", 2, ConsoleColor.White);
			Output("Position: " + Item.Position, 2, ConsoleColor.White);

			int SubPodNumber = 1;


			foreach (WolframAlphaSubPod SubItem in Item.SubPods) {
				Output("", 0, ConsoleColor.White);

				Output("Sub pod " + SubPodNumber, 3, ConsoleColor.Magenta);
				Output("Title: \"" + SubItem.Title + "\"", 3, ConsoleColor.White);
				Output("Pod text: \"" + SubItem.PodText + "\"", 3, ConsoleColor.White);
				if (SubItem.PodImage.notNull())
				{
					Output("Pod image title: \"" + SubItem.PodImage.Title + "\"", 3, ConsoleColor.White);
					Output("Pod image width: " + SubItem.PodImage.Width, 3, ConsoleColor.White);
					Output("Pod image height: " + SubItem.PodImage.Height, 3, ConsoleColor.White);
					Output("Pod image location: \"" + SubItem.PodImage.Location.ToString() + "\"", 3, ConsoleColor.White);
					Output("Pod image description text: \"" + SubItem.PodImage.HoverText + "\"", 3, ConsoleColor.White);
				}
				SubPodNumber += 1;

			}

			PodNumber += 1;


		}

		//Make an empty space in the console.
		Output("", 0, ConsoleColor.White);

		//Make the application stay open until there is user interaction.
		//Output("All content has been saved to " + System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Wolfram Alpha wrapper log.log. Press a key to close the example.", 0, ConsoleColor.Green);
		//Console.ReadLine();

	}
示例#7
0
    public void Main(string apiKey, string WolframAlphaSearchTerms)
    {
        Engine = new WolframAlphaEngine();

        //Try to delete the log file if it already exists.

        /*try {
         *      File.Delete(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Wolfram Alpha wrapper log.log");
         * } catch {
         * }*/

        //Define what our application ID is.
        string WolframAlphaApplicationID = "beta824g1";

        //Define what we want to search for.
        //string WolframAlphaSearchTerms = "england";

        //Print out what we're about to do in the console.
        Output("Getting response for the search terms \"" + WolframAlphaSearchTerms, 0, ConsoleColor.White);

        //Use the engine to get a response, from the application ID specified, and the search terms.
        //Engine.LoadResponse(WolframAlphaSearchTerms, WolframAlphaApplicationID);

        var query = new WolframAlphaQuery();

        query.APIKey = apiKey;
        query.Format = "plaintext";
        query.Query  = WolframAlphaSearchTerms;        //"england"; //"what day is today?";
        //Engine.LoadResponse(WolframAlphaSearchTerms);
        Engine.LoadResponse(query);

        //Print out a message saying that the last task was successful.
        Output("Response injected.", 0, ConsoleColor.White);

        //Make 2 empty spaces in the console.
        Output("", 0, ConsoleColor.White);

        Output("Response details", 1, ConsoleColor.Blue);

        "Showing details for Engine: {0}".info(Engine);

        //Print out how many different pods that were found.
        Output("Pods found: " + Engine.QueryResult.NumberOfPods, 1, ConsoleColor.White);
        Output("Query pasing time: " + Engine.QueryResult.ParseTiming + " seconds", 1, ConsoleColor.White);
        Output("Query execution time: " + Engine.QueryResult.Timing + " seconds", 1, ConsoleColor.White);

        int PodNumber = 1;


        foreach (WolframAlphaPod Item in Engine.QueryResult.Pods)
        {
            //Make an empty space in the console.
            Output("", 0, ConsoleColor.White);

            Output("Pod " + PodNumber, 2, ConsoleColor.Red);

            Output("Sub pods found: " + Item.NumberOfSubPods, 2, ConsoleColor.White);
            Output("Title: \"" + Item.Title + "\"", 2, ConsoleColor.White);
            Output("Position: " + Item.Position, 2, ConsoleColor.White);

            int SubPodNumber = 1;


            foreach (WolframAlphaSubPod SubItem in Item.SubPods)
            {
                Output("", 0, ConsoleColor.White);

                Output("Sub pod " + SubPodNumber, 3, ConsoleColor.Magenta);
                Output("Title: \"" + SubItem.Title + "\"", 3, ConsoleColor.White);
                Output("Pod text: \"" + SubItem.PodText + "\"", 3, ConsoleColor.White);
                if (SubItem.PodImage.notNull())
                {
                    Output("Pod image title: \"" + SubItem.PodImage.Title + "\"", 3, ConsoleColor.White);
                    Output("Pod image width: " + SubItem.PodImage.Width, 3, ConsoleColor.White);
                    Output("Pod image height: " + SubItem.PodImage.Height, 3, ConsoleColor.White);
                    Output("Pod image location: \"" + SubItem.PodImage.Location.ToString() + "\"", 3, ConsoleColor.White);
                    Output("Pod image description text: \"" + SubItem.PodImage.HoverText + "\"", 3, ConsoleColor.White);
                }
                SubPodNumber += 1;
            }

            PodNumber += 1;
        }

        //Make an empty space in the console.
        Output("", 0, ConsoleColor.White);

        //Make the application stay open until there is user interaction.
        //Output("All content has been saved to " + System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Wolfram Alpha wrapper log.log. Press a key to close the example.", 0, ConsoleColor.Green);
        //Console.ReadLine();
    }
        /// <summary>
        /// Validate a query to check for issues before execution.
        /// </summary>
        /// <param name="query">The query to check for errors</param>
        /// <returns>A validation result</returns>
        public WolframAlphaValidationResult ValidateQuery(WolframAlphaQuery query)
        {
            if (String.IsNullOrEmpty(query.APIKey))
            {
                if (String.IsNullOrEmpty(this.APIKey))
                    throw new Exception("To use the Wolfram Alpha API v2, you must specify an API key either through the WolframAlphaQuery, or on the WolframAlphaEngine itself.");
                query.APIKey = this.APIKey;
            }

            if (query.Asynchronous && query.Format == WolframAlphaQueryFormat.html)
                throw new Exception("Wolfram Alpha does not allow asynchronous operations while the format for the query is not set to \"HTML\".");
            // Sorry, I took most of this code from the previous .NET interface written in VB and there are SO MANY ERRORS.
            // I tried to fix most of them but this one is a special one.
            // The exceptions says async is not allowed while format is not set to html, that means only html can do async,
            // however the code in the if parameter shows exactly the opposite.
            // I cba to figure out what was meant so I'll implement this 1:1. If you ever figure it out, fix it and submit it. Thank you.

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(String.Format("{0}{1}{2}", WolframAlphaEngine.baseUrl, WolframAlphaEngine.baseUrlValidation, query.FullQueryString));
            webRequest.KeepAlive = true;
            string response = new StreamReader(webRequest.GetResponse().GetResponseStream()).ReadToEnd();

            return this.ValidateQuery(response);
        }