3

我一直试图让 Wolframalpha APIC#无法正常工作。我一直在尝试使用这两个资源:

  1. 堆栈问题
  2. Wolfram API 演示

帖子中的答案是半有用的,但我无法编译任何东西。我是 C# 的新手,所以有点不知所措。我真的很难让它接受输入然后输出结果。

如果有人可以帮助我使此代码正常工作,以便我可以使用有效的示例或知道我可以从中建模的示例项目,将不胜感激。

这是我剪切并粘贴到 C# (Visual Studio) 控制台项目中的代码:

    namespace WolframAlpha {
    using System;
    using System.Collections.Generic;
    using System.Data.Services.Client;
    using System.Net;
    using System.IO;


    public partial class DefaultPodEntity {

        private String _PlainText;

        private String _Img;

        private String _Title;

        private String _ParentTitle;

        private Int16 _ParentPosition;

        private String _ParentId;

        public String PlainText {
            get {
                return this._PlainText;
            }
            set {
                this._PlainText = value;
            }
        }

        public String Img {
            get {
                return this._Img;
            }
            set {
                this._Img = value;
            }
        }

        public String Title {
            get {
                return this._Title;
            }
            set {
                this._Title = value;
            }
        }

        public String ParentTitle {
            get {
                return this._ParentTitle;
            }
            set {
                this._ParentTitle = value;
            }
        }

        public Int16 ParentPosition {
            get {
                return this._ParentPosition;
            }
            set {
                this._ParentPosition = value;
            }
        }

        public String ParentId {
            get {
                return this._ParentId;
            }
            set {
                this._ParentId = value;
            }
        }
    }

    public partial class HtmlPodEntity {

        private String _Markup;

        private String _Title;

        private Int16 _Position;

        private String _Id;

        private String _Css;

        private String _Scripts;

        public String Markup {
            get {
                return this._Markup;
            }
            set {
                this._Markup = value;
            }
        }

        public String Title {
            get {
                return this._Title;
            }
            set {
                this._Title = value;
            }
        }

        public Int16 Position {
            get {
                return this._Position;
            }
            set {
                this._Position = value;
            }
        }

        public String Id {
            get {
                return this._Id;
            }
            set {
                this._Id = value;
            }
        }

        public String Css {
            get {
                return this._Css;
            }
            set {
                this._Css = value;
            }
        }

        public String Scripts {
            get {
                return this._Scripts;
            }
            set {
                this._Scripts = value;
            }
        }
    }

    public partial class PlainTextPodEntity {

        private String _PlainText;

        private String _Title;

        private String _ParentTitle;

        private Int16 _ParentPosition;

        private String _ParentId;

        public String PlainText {
            get {
                return this._PlainText;
            }
            set {
                this._PlainText = value;
            }
        }

        public String Title {
            get {
                return this._Title;
            }
            set {
                this._Title = value;
            }
        }

        public String ParentTitle {
            get {
                return this._ParentTitle;
            }
            set {
                this._ParentTitle = value;
            }
        }

        public Int16 ParentPosition {
            get {
                return this._ParentPosition;
            }
            set {
                this._ParentPosition = value;
            }
        }

        public String ParentId {
            get {
                return this._ParentId;
            }
            set {
                this._ParentId = value;
            }
        }
    }

    public partial class WolframAlphaFactsContainer : System.Data.Services.Client.DataServiceContext {

        public WolframAlphaFactsContainer(Uri serviceRoot) : 
                base(serviceRoot) {
        }

        /// <summary>
        /// </summary>
        /// <param name="Input">Query string Sample Values : weather|msft|derivative of x^4 sin x|SAT scores</param>
        /// <param name="Location">Location used for computation Sample Values : Madrid|Springfield, IL</param>
        /// <param name="LatitudeLongitude">Latitude/Longitude used for computation Sample Values : 40.42,-3.71|-22.54,-43.12</param>
        /// <param name="Width">Width in pixels for images returned Sample Values : 300|500</param>
        public DataServiceQuery<DefaultPodEntity> GetImageResults(String Input, String Location, String LatitudeLongitude, Int16? Width) {
            if ((Input == null)) {
                throw new System.ArgumentNullException("Input", "Input value cannot be null");
            }
            DataServiceQuery<DefaultPodEntity> query;
            query = base.CreateQuery<DefaultPodEntity>("GetImageResults");
            if ((Input != null)) {
                query = query.AddQueryOption("Input", string.Concat("\'", Input, "\'"));
            }
            if ((Location != null)) {
                query = query.AddQueryOption("Location", string.Concat("\'", Location, "\'"));
            }
            if ((LatitudeLongitude != null)) {
                query = query.AddQueryOption("LatitudeLongitude", string.Concat("\'", LatitudeLongitude, "\'"));
            }
            if (((Width != null) 
                        && (Width.HasValue == true))) {
                query = query.AddQueryOption("Width", Width.Value);
            }
            return query;
        }

        /// <summary>
        /// </summary>
        /// <param name="Input">Query string Sample Values : weather|msft|derivative of x^4 sin x|SAT scores</param>
        /// <param name="Location">Location used for computation Sample Values : Madrid|Springfield, IL</param>
        /// <param name="LatitudeLongitude">Latitude/Longitude used for computation Sample Values : 40.42,-3.71|-22.54,-43.12</param>
        /// <param name="Width">Width in pixels for images returned Sample Values : 300|500</param>
        public DataServiceQuery<HtmlPodEntity> GetHtmlResults(String Input, String Location, String LatitudeLongitude, Int16? Width) {
            if ((Input == null)) {
                throw new System.ArgumentNullException("Input", "Input value cannot be null");
            }
            DataServiceQuery<HtmlPodEntity> query;
            query = base.CreateQuery<HtmlPodEntity>("GetHtmlResults");
            if ((Input != null)) {
                query = query.AddQueryOption("Input", string.Concat("\'", Input, "\'"));
            }
            if ((Location != null)) {
                query = query.AddQueryOption("Location", string.Concat("\'", Location, "\'"));
            }
            if ((LatitudeLongitude != null)) {
                query = query.AddQueryOption("LatitudeLongitude", string.Concat("\'", LatitudeLongitude, "\'"));
            }
            if (((Width != null) 
                        && (Width.HasValue == true))) {
                query = query.AddQueryOption("Width", Width.Value);
            }
            return query;
        }

        /// <summary>
        /// </summary>
        /// <param name="Input">Query string Sample Values : weather|msft|derivative of x^4 sin x|SAT scores</param>
        /// <param name="Location">Location used for computation Sample Values : Madrid|Springfield, IL</param>
        /// <param name="LatitudeLongitude">Latitude/Longitude used for computation Sample Values : 40.42,-3.71|-22.54,-43.12</param>
        /// <param name="Width">Width in pixels for images returned Sample Values : 300|500</param>
        public DataServiceQuery<PlainTextPodEntity> GetPlainTextResults(String Input, String Location, String LatitudeLongitude, Int16? Width) {
            if ((Input == null)) {
                throw new System.ArgumentNullException("Input", "Input value cannot be null");
            }
            DataServiceQuery<PlainTextPodEntity> query;
            query = base.CreateQuery<PlainTextPodEntity>("GetPlainTextResults");
            if ((Input != null)) {
                query = query.AddQueryOption("Input", string.Concat("\'", Input, "\'"));
            }
            if ((Location != null)) {
                query = query.AddQueryOption("Location", string.Concat("\'", Location, "\'"));
            }
            if ((LatitudeLongitude != null)) {
                query = query.AddQueryOption("LatitudeLongitude", string.Concat("\'", LatitudeLongitude, "\'"));
            }
            if (((Width != null) 
                        && (Width.HasValue == true))) {
                query = query.AddQueryOption("Width", Width.Value);
            }
            return query;
        }
    }
}
4

4 回答 4

3

这个 codeplex 项目声称涵盖了最新的 Wolfram Alpha API 并包含一个示例: http ://wolframalphaapi20.codeplex.com/

控制台应用程序使用静态 Main 方法作为其入口点。此例程通常可以在为控制台应用程序创建新项目时自动创建的文件 program.cs 中找到。

如果编译器说它找不到 Main 那么它可能已被删除或从未创建。很难说没有任何代码看。当 Main 方法的问题得到解决时,可能会显示更多错误。

于 2012-05-30T18:40:23.130 回答
2

我目前正在玩一个 lib 调用 WolframAlpha.NET。代码源在github 上。有一个nuget 包(最后发布于 2019-06-24)。

示例(来自自述文件)

这是从 Wolfram|Alpha 获取数据的最简单形式:

static void Main(string[] args)
{
    //First create the main class:
    WolframAlpha wolfram = new WolframAlpha("APPID HERE");

    //Then you simply query Wolfram|Alpha like this
    //Note that the spelling error will be correct by Wolfram|Alpha
    QueryResult results = wolfram.Query("Who is Danald Duck?");

    //The QueryResult object contains the parsed XML from Wolfram|Alpha. Lets look at it.
    //The results from wolfram is split into "pods". We just print them.
    if (results != null)
    {
        foreach (Pod pod in results.Pods)
        {
            Console.WriteLine(pod.Title);
            if (pod.SubPods != null)
            {
                foreach (SubPod subPod in pod.SubPods)
                {
                    Console.WriteLine(subPod.Title);
                    Console.WriteLine(subPod.Plaintext);
                }
            }
        }
    }
}

有关更多示例,请查看 WolframAlphaNet.Examples 和 WolframAlphaNet.Tests 项目。

于 2015-01-19T10:33:41.960 回答
1

您有允许您与 Wolfram API 交互的复制粘贴的类定义(如DefaultPodEntityWolframAlphaFactsContainer),但您没有Main()定义您的程序应该对这些类做什么的函数的定义。您将需要添加方法定义

static void Main(string[] args)
{
  // TODO: call methods of WolframAlphaFactsContainer
} 

到其中一个类(例如,您的问题中未列出的WolframAlphaFactsContainer新类,如该类并使用适当的参数调用其方法)。ProgramTODOWolframAlphaFactsContainerGetImageResults()

注意:您需要学习基本的 C# 编程习惯,然后才能成功解决用 C# 编写一个工作的、正确的程序来执行想做的事情(而不是依赖其他人的代码)的问题。

注意:阅读文档以及Main()如何将命令行参数传递给您的程序(如果您想这样做)。

注意:该类WolframAlphaFactsContainer被标记partial,这意味着该类可能还有其他部分(请参阅文档)。如果有,您还需要在代码中包含这些内容。

于 2012-06-05T10:56:41.270 回答
0

我知道这篇文章很旧,但看看它是如何出现在谷歌顶部附近的: https ://wapiex.codeplex.com/

这是我刚刚完成的包装。它包含的内容比其他 codeplex 项目要多得多。随意使用它

于 2014-10-30T22:25:03.713 回答