wolfram

package module
v0.0.0-...-c99b0fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2018 License: MIT Imports: 7 Imported by: 3

README

go-wolfram

Single file library for the Wolfram Alpha API written in Golang without extra dependencies

Installing

go get github.com/JoshuaDoes/go-wolfram

Example

package main

import "github.com/JoshuaDoes/go-wolfram"

func main() {
	//Initialize a new client
	c := &wolfram.Client{AppID:"your app id here"}

	//Get a result without additional parameters
	res, err := c.GetQueryResult("1+1", nil)

	if err != nil {
		panic(err)
	}
	
	resObj := res.QueryResult

	//Iterate through the pods and subpods
	//and print out their title attributes
	for i := range resObj.Pods {
		println(resObj.Pods[i].Title)

		for j := range resObj.Pods[i].SubPods {
			println(resObj.Pods[i].SubPods[j].Title)
		}
	}
}
Output
> go run main.go

< Input
< Result
< Number name
< Visual representation
< Number line
< Illustration

Adding extra parameters

package main

import "github.com/JoshuaDoes/go-wolfram"

func main() {
	//Initialize a new client
	c := &wolfram.Client{AppID:"your app id here"}
  
  	params := url.Values{}
	params.Set("assumption", "DateOrder_**Day.Month.Year--")
  
	//Get a result with additional parameters
	res, err := c.GetQueryResult("26-9-2016", params)

	if err != nil {
		panic(err)
	}
	
	resObj := res.QueryResult

	//Iterate through the pods and subpods
	//and print out their title attributes
	for i := range resObj.Pods {
		println(resObj.Pods[i].Title)

		for j := range resObj.Pods[i].SubPods {
			println(resObj.Pods[i].SubPods[j].Title)
		}
	}
}
Output
> go run main.go

< Input interpretation
< Date formats
< Time difference from today (Friday, September 23, 2016)
< Time in 2016
< Observances for September 26 (Sweden)
< Anniversaries for September 26, 2016
< Daylight information for September 26 in Stockholm, Sweden
< Phase of the Moon

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alternative

type Alternative struct {
	InnerText string `json:",innerxml"`
}

type Assumption

type Assumption struct {
	Values   []Value `json:"value"`
	Type     string  `json:"type"`
	Word     string  `json:"word"`
	Template string  `json:"template"`
	Count    int     `json:"count"`
}

type Assumptions

type Assumptions struct {
	Assumption []Assumption `json:"assumption"`
	Count      int          `json:"count"`
}

type Client

type Client struct {
	AppID string
}

The client, requires an App ID, which you can sign up for at https://developer.wolframalpha.com/

func (*Client) GetConversationalQuery

func (c *Client) GetConversationalQuery(query string, units Unit, lastResult *Conversation) (*Conversation, error)

func (*Client) GetFastQueryRecognizer

func (c *Client) GetFastQueryRecognizer(query string, mode Mode) (*FastQueryResult, error)

func (*Client) GetQueryResult

func (c *Client) GetQueryResult(query string, params url.Values) (*QueryResult, error)

GetQueryResult gets the query result from the API and returns it. Example extra parameter: "format=image", for a url.Value it'd be: u := url.Values{} u.Add("format", "image") Additional information about parameters can be found at http://products.wolframalpha.com/docs/WolframAlpha-API-Reference.pdf, page 42

func (*Client) GetShortAnswerQuery

func (c *Client) GetShortAnswerQuery(query string, units Unit, timeout int) (string, error)

func (*Client) GetSimpleQuery

func (c *Client) GetSimpleQuery(query string, params url.Values) (io.ReadCloser, string, error)

GetSimpleQuery gets an image from the `simple` endpoint.

Returns the image as a response body, the query url, and an error

Can take some extra parameters, e.g `background=F5F5F5` sets the background color to #F5F5F5

The rest of the parameters can be found here https://products.wolframalpha.com/simple-api/documentation/

func (*Client) GetSpokenAnswerQuery

func (c *Client) GetSpokenAnswerQuery(query string, units Unit, timeout int) (string, error)

type Conversation

type Conversation struct {
	Result         string `json:"result"`
	ConversationID string `json:"conversationID"`
	Host           string `json:"host"`
	S              string `json:"s"`
	ErrorMessage   string `json:"error"`
}

type Delimiters

type Delimiters struct {
	Text string `json:"text"`
}

type FastQueryResult

type FastQueryResult struct {
	Version            string `json:"version"`
	SpellingCorrection string `json:"spellingCorretion"`
	BuildNumber        string `json:"buildnumber"`
	Query              []*struct {
		I                       string `json:"i"`
		Accepted                string `json:"accepted"`
		Timing                  string `json:"timing"`
		Domain                  string `json:"domain"`
		ResultSignificanceScore string `json:"resultsignificancescore"`
		SummaryBox              *struct {
			Path string `json:"path"`
		} `json:"summarybox"`
	} `json:"query"`
}

type Generalization

type Generalization struct {
	Topic       string `json:"topic"`
	Description string `json:"desc"`
	URL         string `json:"url"`
}

type Img

type Img struct {
	Src    string `json:"src"`
	Alt    string `json:"alt"`
	Title  string `json:"title"`
	Width  int    `json:"width"`
	Height int    `json:"height"`
}

type Info

type Info struct {
	Text string `json:"text"`
	Img  []Img  `json:"img"`
	Link []Link `json:"link"`
}

type Infos

type Infos struct {
	Count int    `json:"count"`
	Info  []Info `json:"info"`
}

If there's extra information for the pod, the pod will have a <infos> element which contains <info> elements with text, and/or images/links to that information

type Link struct {
	URL   string `json:"url"`
	Text  string `json:"text"`
	Title string `json:"title"`
}

type Mode

type Mode int
const (
	Default Mode = iota
	Voice
)

type Pod

type Pod struct {
	//The subpod elements of the pod
	SubPods []SubPod `json:"subpods"`

	//sub elements of the pod
	Infos  []Infos  `json:"infos"`
	States []States `json:"states"`

	//The pod title, used to identify the pod.
	Title string `json:"title"`

	//The name of the scanner that produced this pod. A guide to the type of
	//data it holds.
	Scanner string `json:"scanner"`

	//Not documented currently
	ID         string `json:"id"`
	Position   int    `json:"position"`
	Error      bool   `json:"error"`
	NumSubPods int    `json:"numsubpods"`
	Sounds     Sounds `json:"sounds"`
}

<pod> elements are subelements of <queryresult>. Each contains the results for a single pod

type QueryResult

type QueryResult struct {
	Query string

	QueryResult struct {
		//The pods are what hold the majority of the information
		Pods []Pod `json:"pods"`

		//Warnings hold information about for example spelling errors
		Warnings Warnings `json:"warnings"`

		//Assumptions show info if some assumption was made while parsing the query
		Assumptions Assumptions `json:"assumptions"`

		//Generalizes the query to display more information
		Generalizations []Generalization `json:"generalization"`

		//true or false depending on whether the input could be successfully
		//understood. If false there will be no <pod> subelements
		Success bool `json:"success"`

		//true or false depending on whether a serious processing error occurred,
		//such as a missing required parameter. If true there will be no pod
		//content, just an <error> sub-element.
		Error bool `json:"error"`

		//The number of pod elements
		NumPods int `json:"numpods"`

		//Categories and types of data represented in the results
		DataTypes string `json:"datatypes"`

		//The number of pods that are missing because they timed out (see the
		//scantimeout query parameter).
		TimedOut string `json:"timedout"`

		//The wall-clock time in seconds required to generate the output.
		Timing float64 `json:"timing"`

		//The time in seconds required by the parsing phase.
		ParseTiming float64 `json:"parsetiming"`

		//Whether the parsing stage timed out (try a longer parsetimeout parameter
		//if true)
		ParseTimedOut bool `json:"parsetimedout"`

		//A URL to use to recalculate the query and get more pods.
		ReCalculate string `json:"recalculate"`

		//These elements are not documented currently
		ID      string `json:"id"`
		Host    string `json:"host"`
		Server  string `json:"server"`
		Related string `json:"related"`

		//The version specification of the API on the server that produced this result.
		Version string `json:"version"`
	}
}

The QueryResult is what you get back after a request

type ReInterpretation

type ReInterpretation struct {
	Alternatives []Alternative `json:"alternative"`
	Text         string        `json:"text"`
	New          string        `json:"new"`
}

type Sound

type Sound struct {
	URL  string `json:"url"`
	Type string `json:"type"`
}

type Sounds

type Sounds struct {
	Count int     `json:"count"`
	Sound []Sound `json:"sound"`
}

If there was a sound related to the query, if you for example query a musical note You will get a <sound> element which contains a link to the sound

type Source

type Source struct {
	URL  string `json:"url"`
	Text string `json:"text"`
}

type Sources

type Sources struct {
	URL    string   `json:"url"`
	Text   string   `json:"text"`
	Source []Source `json:"source"`
}

Each Source contains a link to a web page with the source information

type Spellcheck

type Spellcheck struct {
	Word       string `json:"word"`
	Suggestion string `json:"suggestion"`
	Text       string `json:"text"`
}

type State

type State struct {
	Name  string `json:"name"`
	Input string `json:"input"`
}

type States

type States struct {
	Count int     `json:"count"`
	State []State `json:"state"`
}

"Many pods on the Wolfram|Alpha website have text buttons in their upper-right corners that substitute the contents of that pod with a modified version. In Figure 1, the Result pod has buttons titled "More days", "Sun and Moon", CDT", "GMT", and "Show metric". Clicking any of these buttons will recompute just that one pod to display different information."

type SubPod

type SubPod struct {
	//HTML <img> element
	Image Img `json:"img"`

	//Textual representation of the subpod
	Plaintext string `json:"plaintext"`

	//Usually an empty string because most subpod elements don't have a title
	Title string `json:"title"`
}

type Translation

type Translation struct {
	Phrase      string `json:"phrase"`
	Translation string `json:"trans"`
	Language    string `json:"lang"`
	Text        string `json:"text"`
}

type Unit

type Unit int
const (
	Imperial Unit = iota
	Metric
)

type Value

type Value struct {
	Name        string `json:"name"`
	Word        string `json:"word"`
	Description string `json:"desc"`
	Input       string `json:"input"`
}

Usually contains info about an assumption

type Warnings

type Warnings struct {
	//How many warnings were issued
	Count int `json:"count"`

	//Suggestions for spelling corrections
	Spellchecks []Spellcheck `json:"spellcheck"`

	//"If you enter a query with mismatched delimiters like "sin(x", Wolfram|Alpha attempts to fix the problem and reports
	//this as a warning."
	Delimiters []Delimiters `json:"delimiters"`

	//"[The API] will translate some queries from non-English languages into English. In some cases when it does
	//this, you will get a <translation> element in the API result."
	Translations []Translation `json:"translation"`

	//"[The API] can automatically try to reinterpret a query that it does not understand but that seems close to one
	//that it can."
	ReInterpretations []ReInterpretation `json:"reinterpret"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL