Exemple #1
0
        public static BigInteger RandomPrime(int bitLength)
        {
            BigInteger prime            = BigInteger.Zero;
            BigInteger randomBigInteger = BigInteger.Abs(new BigInteger(RandomByteArray(bitLength)));
            string     query            = String.Format(MathHelper.WolframAlphaPrimeQuery, randomBigInteger);

            WolframAlphaService service = new WolframAlphaService(ConfigurationManager.AppSettings.Get(MathHelper.WolframAlphaAppKey));
            WolframAlphaRequest request = new WolframAlphaRequest(query);
            WolframAlphaResult  result;

            request.Formats = new List <string>()
            {
                "plaintext"
            };

            result = service.Compute(request).GetAwaiter().GetResult();

            for (int i = 0; result.QueryResult.Success && i < result.QueryResult.Pods.Count; i++)
            {
                Pod pod = result.QueryResult.Pods[i];

                if (pod.SubPods != null && pod.Title == MathHelper.WolframAlphaResultTitle)
                {
                    for (int j = 0; j < pod.SubPods.Count; j++)
                    {
                        SubPod subPod = pod.SubPods[j];

                        prime = BigInteger.Parse(subPod.Plaintext);
                    }
                }
            }

            return(prime);
        }
        public static string AskWolfram(WolframAlphaService wolfram, string input)
        {
            string alphaMessage = "";

            try
            {
                WolframAlphaRequest request = new WolframAlphaRequest(input);
                request.Formats = new List <string> {
                    Format.Plaintext
                };
                request.IncludePodIds = new List <string> {
                    "Result"
                };
                request.IgnoreCase  = true;
                request.Translation = false;
                request.Scanners    = new List <string> {
                    "Numeric", "Data", "Word"
                };
                request.GeoCoordinate = new GeoCoordinate(53.211004, 5.729472);
                request.FormatTimeout = 5;
                request.PodTimeout    = 5;
                request.Reinterpret   = true;
                request.Unit          = Unit.Metric;
                request.Async         = false;
                request.Width         = 1000;

                WolframAlphaResult result = wolfram.Compute(request).Result;

                if (result != null)
                {
                    if (result.QueryResult.Pods != null)
                    {
                        foreach (Pod pod in result.QueryResult.Pods)
                        {
                            if (pod.SubPods != null && pod.SubPods.Count > 0)
                            {
                                foreach (SubPod sub in pod.SubPods)
                                {
                                    if (!string.IsNullOrWhiteSpace(sub.Plaintext))
                                    {
                                        alphaMessage += sub.Plaintext + ", ";
                                    }
                                }
                                alphaMessage = alphaMessage.Remove(alphaMessage.Length - 2, 2);
                            }
                        }
                    }
                }
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
                alphaMessage = "";
            }

            return(alphaMessage);
        }
        public string Integrate(string function, string from, string to)
        {
            string res = "";
            string req = "integrate " + function + "dx " + "from " + from + " to " + to;

            request = new WolframAlphaRequest(req)
            {
                Formats = new List <string>
                {
                    Format.Plaintext,
                    Format.MathematicaOutput
                }
            };
            result = wolfram.Compute(request).GetAwaiter().GetResult();
            Console.WriteLine("Вычисляю: " + req);
            if (result.QueryResult.Error != null)
            {
                Console.WriteLine(result.QueryResult.Error.Message);
            }

            if (result != null)
            {
                if (result.QueryResult.Pods != null)
                {
                    foreach (var pod in result.QueryResult.Pods)
                    {
                        if (pod.SubPods != null)
                        {
                            foreach (var subpod in pod.SubPods)
                            {
                                if (pod.Title.Contains("Definite"))
                                {
                                    res = subpod.Plaintext.Split('=')[1];//.Replace(" ", "*");
                                }
                            }
                        }
                        else
                        {
                            res = "SUBPODS ERROR";
                        }
                    }
                }
                else
                {
                    Console.WriteLine("NULL PODS");
                }
            }
            else
            {
                res = "UNKNOWN ERROR";
            }

            Console.WriteLine("Результат: " + res);
            return(res);
        }
        /// <summary>
        /// Makes a Full Results API request
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <WolframAlphaResult> Compute(WolframAlphaRequest request)
        {
            string url = BuildUrl(ApiBaseUrl + FullResultsApiUrl, request);

            using (var client = new HttpClient())
            {
                var httpRequest = await client.GetAsync(url);

                var response = await httpRequest.Content.ReadAsStringAsync();

                WolframAlphaResult result = JsonConvert.DeserializeObject <WolframAlphaResult>(response);
                return(result);
            }
        }
        private static async System.Threading.Tasks.Task WolframTestAsync()
        {
            WolframAlphaService wolfram = new WolframAlphaService(WolframAlphaHelper.API_KEY);
            WolframAlphaRequest request = new WolframAlphaRequest("integrate(2x + x ^ 2 / 12 + (x ^ 2)) dx from 0 to x")
            {
                Formats = new List <string>
                {
                    Format.Plaintext,
                    Format.MathematicaOutput,
                    Format.MathML,
                    Format.MathematicaCell
                }
            };
            WolframAlphaResult result = await wolfram.Compute(request);



            string res = "";

            if (result != null)
            {
                foreach (var pod in result.QueryResult.Pods)
                {
                    if (pod.SubPods != null)
                    {
                        Console.WriteLine(pod.Title + "----");
                        foreach (var subpod in pod.SubPods)
                        {
                            Console.WriteLine(subpod.Plaintext);
                            Console.WriteLine(subpod.Minput);
                            Console.WriteLine(subpod.Title);
                        }
                    }
                    else
                    {
                        res = "SUBPODS ERROR";
                    }
                }
            }
            else
            {
                res = "UNKNOWN ERROR";
            }

            Console.WriteLine(res);
            Console.ReadLine();
        }
Exemple #6
0
        private static void FullResults()
        {
            Console.WriteLine("What would you like to search for?");
            string input   = Console.ReadLine();
            var    request = new WolframAlphaRequest(input)
            {
                Formats = new List <string>
                {
                    Format.Plaintext,
                    Format.Image,
                    Format.Sound
                }
            };

            var result = service.Compute(request).GetAwaiter().GetResult();

            if (result.QueryResult.Error != null)
            {
                Console.WriteLine(result.QueryResult.Error.Message);
            }

            if (result.QueryResult.Pods != null)
            {
                foreach (var pod in result.QueryResult.Pods)
                {
                    if (pod.SubPods != null)
                    {
                        Console.WriteLine(pod.Title);
                        foreach (var subpod in pod.SubPods)
                        {
                            Console.WriteLine($"\t{subpod.Plaintext}");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No results were found!");
            }
            Console.ReadKey();
        }
Exemple #7
0
        public async void Ask()
        {
            WolframAlphaService service = new WolframAlphaService("5YAGW3-LXEQ4H8YQJ");
            WolframAlphaRequest request = new WolframAlphaRequest("Integrate e^(-x^2)2|x^3| from 2 to 5");
            WolframAlphaResult  result  = await service.Compute(request);

            StringBuilder strb = new StringBuilder();

            if (result.QueryResult.Pods != null)
            {
                var WolframImage = result.QueryResult.Pods
                                   .Where(x => x.Title == "Visual representation of the integral").First()
                                   .SubPods.First()
                                   .Image;
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(WolframImage.Src, UriKind.Absolute);
                bitmap.EndInit();

                VisualRepresentation.Width  = WolframImage.Width;
                VisualRepresentation.Source = bitmap;
            }
        }
Exemple #8
0
        public async Task AnswerQuery([Remainder] string query)
        {
            WolframAlphaService service = new WolframAlphaService("");
            WolframAlphaRequest request = new WolframAlphaRequest(query);
            WolframAlphaResult  result  = await service.Compute(request);

            string final = "";

            if (result.QueryResult.Pods != null)
            {
                final = result.QueryResult.Pods[1].SubPods[0].Plaintext.Replace("Wolfram|Alpha", "wang");
            }
            else
            {
                final = "idk";
            }

            if (final != null || final != "")
            {
                await ReplyAsync(final);
            }
            else
            {
                await ReplyAsync("idk");
            }
            //foreach (var pod in result.QueryResult.Pods)
            //{
            //    if (pod.SubPods != null)
            //    {
            //        await ReplyAsync(pod.Title);
            //        foreach (var subpod in pod.SubPods)
            //        {
            //            await ReplyAsync("    " + subpod.Plaintext);
            //        }
            //    }
            //}
        }
Exemple #9
0
        public async Task wolframAlphaSimple([Remainder] string query)
        {
            WolframAlphaRequest request = new WolframAlphaRequest(query);

            request.Reinterpret = true;
            QueryResult    result       = (await WolframAlphaService.Compute(request)).QueryResult;
            Queue <string> messageQueue = new Queue <string>();

            if (result.Success)
            {
                string outputBuffer = "";
                foreach (var pod in result.Pods)
                {
                    string title     = (pod.Title.Length > 0) ? ("**" + pod.Title + "**\n") : "";
                    string newBuffer = outputBuffer + title;
                    if (newBuffer.Length < 2000)
                    {
                        outputBuffer = newBuffer;
                    }
                    else
                    {
                        messageQueue.Enqueue(outputBuffer);
                        outputBuffer = title;
                    }
                    foreach (var subPod in pod.SubPods)
                    {
                        title = (subPod.Title.Length > 0) ? ("**" + subPod.Title + "**\n") : "";

                        if (subPod.Plaintext.Length > 0)
                        {
                            newBuffer = outputBuffer + title + subPod.Plaintext + "\n";

                            if (newBuffer.Length < 2000)
                            {
                                outputBuffer = newBuffer;
                            }
                            else
                            {
                                messageQueue.Enqueue(outputBuffer);
                                outputBuffer = title + subPod.Plaintext + "\n";
                            }
                        }
                        else
                        {
                            newBuffer = outputBuffer + title;

                            if (newBuffer.Length < 2000)
                            {
                                messageQueue.Enqueue(newBuffer);
                                outputBuffer = "";
                            }
                            else
                            {
                                messageQueue.Enqueue(outputBuffer);
                                outputBuffer = title + subPod.Plaintext + "\n";
                                messageQueue.Enqueue(outputBuffer);
                                outputBuffer = "";
                            }
                            messageQueue.Enqueue(subPod.Image.Src);
                        }
                    }
                }
                if (outputBuffer.Length > 0)
                {
                    messageQueue.Enqueue(outputBuffer);
                }
                if (messageQueue.Count > 0)
                {
                    while (messageQueue.Count > 0)
                    {
                        string message = messageQueue.Dequeue();
                        if (message.Length > 0)
                        {
                            await Context.Channel.SendMessageAsync(message);
                        }
                        Thread.Sleep(1000);
                    }
                }
            }
            else
            {
                Context.Channel.SendMessageAsync("Sorry there was an issue talking to Wolfram Alpha.");
            }
        }