3

C#私は無駄に働くためにWolframalphaAPIを取得しようとしてきました。私はこれらの2つのリソースを使おうとしています。

  1. スタックの質問
  2. WolframAPIデモ

投稿の答えは半ば役に立ちましたが、コンパイルするものが何もありません。私は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

私は現在、WolframAlpha.NETというlib呼び出しで遊んでいます。コードソースはgithubにあります。nugetパッケージがあります(最終公開2019-06-24)。

例(readmeから)

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と対話することを可能にするコピーペーストされたクラス定義(DefaultPodEntityおよびなど)がありますが、プログラムがそれらのクラスで何をすべきかを定義する関数の定義がありません。メソッド定義を追加する必要がありますWolframAlphaFactsContainerMain()

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

クラスの1つ(たとえば、質問にリストされていないWolframAlphaFactsContainer新しいクラスなどProgram。これがコンパイルされたら、TODOコメントを、WolframAlphaFactsContainerクラスとの対話方法を指定するC#ステートメントに置き換える必要があります(たとえば、のインスタンスを作成します)。そのクラスを作成GetImageResults()し、適切なパラメータを使用してそのメソッドを呼び出します)。

:C#で(他の人のコードに依存するのではなく)やりたいことを実行する正しいプログラムを作成する問題にうまく取り組むには、基本的なC#プログラミングイディオムを学ぶ必要があります。

:コマンドラインパラメーターをプログラムに渡す方法とその方法に関するドキュメントをお読みくださいMain()必要に応じて)。

:クラスWolframAlphaFactsContainerにはマークが付けられています。partialこれは、このクラスの他の部分が存在する可能性があることを意味します(ドキュメントを参照)。ある場合は、それらもコードに含める必要があります。

于 2012-06-05T10:56:41.270 に答える
0

私はこの投稿が古いことを知っていますが、上部近くのグーグルでどのように表示されるかを見てください: https ://wapiex.codeplex.com/

これは私が完成したばかりのラッパーです。これには、他のcodeplexプロジェクトよりもはるかに多くのものが含まれています。お気軽にご利用ください

于 2014-10-30T22:25:03.713 に答える