Wolfram Alpha API Example help

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,

i am working on using the wolfram alpha api to return information that my application searches on the internet and i have looked at the example visual basic binding example found here http://products.wolframalpha.com/api/libraries.html


however oddly enough for the .net example there are 2 errors in the code and i cant figure out what the issue is. for the sake of simplicity i will only include the relevent part of the code
There are 2 modules Main.vb and the WolframAlphaEngine.vb
the errors are:
Error 1Argument not specified for parameter APIKey of Public Sub New(APIKey As String).D:........NET_Binding_1_0 (1)NET_Binding_1_0WA_WrapperMain.vb3 9WA_Wrapper

and

Error 2Overload resolution failed because no accessible LoadResponse accepts this number of arguments.D:.........NET_Binding_1_0 (1)NET_Binding_1_0WA_WrapperMain.vb37 9WA_Wrapper

any help would be greatly appreciated!!

Main.vb
Public Module WolframAlphaWrapperExample

Dim Engine As New WolframAlphaEngine

Public Sub Output(ByVal Data As String, ByVal Indenting As Integer, ByVal Color As System.ConsoleColor)
Data = New String(" ", Indenting * 4) & Data

Console.ForegroundColor = Color
Console.WriteLine(Data)
Console.ForegroundColor = ConsoleColor.White

Dim Writer As New IO.StreamWriter(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "Wolfram Alpha wrapper log.log", True)
Writer.WriteLine(Data)
Writer.Close()
Writer.Dispose()

End Sub

Public Sub Main()

Try to delete the log file if it already exists.
Try
IO.File.Delete(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "Wolfram Alpha wrapper log.log")
Catch
End Try

Define what our application ID is.
Dim WolframAlphaApplicationID As String = "beta824g1"

Define what we want to search for.
Dim WolframAlphaSearchTerms As String = "england"

Print out what were about to do in the console.
Output("Getting response for the search terms """ & WolframAlphaSearchTerms & """ and the application ID string """ & WolframAlphaApplicationID & """ ...", 0, ConsoleColor.White)

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

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)

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)

Dim PodNumber As Integer = 1

For Each Item As WolframAlphaPod 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(" & Item.Position, 2, ConsoleColor.White)

Dim SubPodNumber As Integer = 1

For Each SubItem As WolframAlphaSubPod 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)
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

Next

PodNumber += 1


Next

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()

End Sub
End Module

and WolframAlphaEngine.vb

Public Class WolframAlphaEngine

Private WA_APIKey As String

Private WA_QueryResult As WolframAlphaQueryResult
Private WA_ValidationResult As WolframAlphaValidationResult

Public Sub New(ByVal APIKey As String)
WA_APIKey = APIKey
End Sub

Public Property APIKey As String
Get
Return WA_APIKey
End Get
Set(ByVal value As String)
WA_APIKey = value
End Set
End Property

Public ReadOnly Property QueryResult As WolframAlphaQueryResult
Get
Return WA_QueryResult
End Get
End Property

Public ReadOnly Property ValidationResult As WolframAlphaValidationResult
Get
Return WA_ValidationResult
End Get
End Property

#Region "Overloads of ValidateQuery"

Public Function ValidateQuery(ByVal Query As WolframAlphaQuery) As WolframAlphaValidationResult

If Query.APIKey = "" Then
If Me.APIKey = "" Then
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.")
End If
Query.APIKey = Me.APIKey
End If

If Query.Asynchronous = True AndAlso Query.Format = WolframAlphaQuery.WolframAlphaQueryFormat.HTML Then
Throw New Exception("Wolfram Alpha does not allow asynchronous operations while the format for the query is not set to ""HTML"".")
End If

Dim WebRequest As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create("http://preview.wolframalpha.com/api/v1/validatequery.jsp" & Query.FullQueryString), System.Net.HttpWebRequest)
WebRequest.KeepAlive = True
Dim Response As String = New IO.StreamReader(WebRequest.GetResponse().GetResponseStream()).ReadToEnd()

Return ValidateQuery(Response)

End Function

Public Function ValidateQuery(ByVal Response As String) As WolframAlphaValidationResult

Dim Document As New Xml.XmlDocument
Dim Result As WolframAlphaValidationResult = Nothing
Try
Document.LoadXml(Response)
Result = ValidateQuery(Document)
Catch
End Try
Document = Nothing

Return Result

End Function

Public Function ValidateQuery(ByVal Response As Xml.XmlDocument) As WolframAlphaValidationResult

System.Threading.Thread.Sleep(1)

Dim MainNode As Xml.XmlNode = Response.SelectNodes("/validatequeryresult")(0)

WA_ValidationResult = New WolframAlphaValidationResult
WA_ValidationResult.Success = MainNode.Attributes("success").Value
WA_ValidationResult.ErrorOccured = MainNode.Attributes("error").Value
WA_ValidationResult.Timing = MainNode.Attributes("timing").Value
WA_ValidationResult.ParseData = MainNode.SelectNodes("parsedata")(0).InnerText
WA_ValidationResult.Assumptions = New List(Of WolframAlphaAssumption)

For Each Node As Xml.XmlNode In MainNode.SelectNodes("assumptions")

System.Threading.Thread.Sleep(1)

Dim Assumption As New WolframAlphaAssumption

Assumption.Word = Node.SelectNodes("word")(0).InnerText

Dim SubNode As Xml.XmlNode = Node.SelectNodes("categories")(0)

For Each ContentNode As Xml.XmlNode In SubNode.SelectNodes("category")

System.Threading.Thread.Sleep(1)

Assumption.Categories.Add(ContentNode.InnerText)

Next

WA_ValidationResult.Assumptions.Add(Assumption)

Next

Return WA_ValidationResult

End Function

#End Region

#Region "Overloads of LoadResponse"

Public Function LoadResponse(ByVal Query As WolframAlphaQuery) As WolframAlphaQueryResult

If Query.APIKey = "" Then
If Me.APIKey = "" Then
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.")
End If
Query.APIKey = Me.APIKey
End If

If Query.Asynchronous = True AndAlso Query.Format = WolframAlphaQuery.WolframAlphaQueryFormat.HTML Then
Throw New Exception("Wolfram Alpha does not allow asynchronous operations while the format for the query is not set to ""HTML"".")
End If

Dim WebRequest As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create("http://preview.wolframalpha.com/api/v1/query.jsp" & Query.FullQueryString), System.Net.HttpWebRequest)
WebRequest.KeepAlive = True
Dim Response As String = New IO.StreamReader(WebRequest.GetResponse().GetResponseStream()).ReadToEnd()

Return LoadResponse(Response)

End Function
Public Function LoadResponse(ByVal Response As String) As WolframAlphaQueryResult

Dim Document As New Xml.XmlDocument
Dim Result As WolframAlphaQueryResult = Nothing
Try
Document.LoadXml(Response)
Result = LoadResponse(Document)
Catch
End Try
Document = Nothing

Return Result

End Function
Public Function LoadResponse(ByVal Response As Xml.XmlDocument) As WolframAlphaQueryResult

System.Threading.Thread.Sleep(1)

Dim MainNode As Xml.XmlNode = Response.SelectNodes("/queryresult")(0)
WA_QueryResult = New WolframAlphaQueryResult
WA_QueryResult.Success = MainNode.Attributes("success").Value
WA_QueryResult.ErrorOccured = MainNode.Attributes("error").Value
WA_QueryResult.NumberOfPods = MainNode.Attributes("numpods").Value
WA_QueryResult.Timing = MainNode.Attributes("timing").Value
WA_QueryResult.TimedOut = MainNode.Attributes("timedout").Value
WA_QueryResult.DataTypes = MainNode.Attributes("datatypes").Value
WA_QueryResult.Pods = New List(Of WolframAlphaPod)

For Each Node As Xml.XmlNode In MainNode.SelectNodes("pod")

System.Threading.Thread.Sleep(1)

Dim Pod As New WolframAlphaPod

Pod.Title = Node.Attributes("title").Value
Pod.Scanner = Node.Attributes("scanner").Value
Pod.Position = Node.Attributes("position").Value
Pod.ErrorOccured = Node.Attributes("error").Value
Pod.NumberOfSubPods = Node.Attributes("numsubpods").Value
Pod.SubPods = New List(Of WolframAlphaSubPod)

For Each SubNode As Xml.XmlNode In Node.SelectNodes("subpod")

System.Threading.Thread.Sleep(1)

Dim SubPod As New WolframAlphaSubPod
SubPod.Title = SubNode.Attributes("title").Value

For Each ContentNode As Xml.XmlNode In SubNode.SelectNodes("plaintext")

System.Threading.Thread.Sleep(1)

SubPod.PodText = ContentNode.InnerText

Next

For Each ContentNode As Xml.XmlNode In SubNode.SelectNodes("img")

System.Threading.Thread.Sleep(1)

Dim Image As New WolframAlphaImage
Image.Location = New Uri(ContentNode.Attributes("src").Value)
Image.HoverText = ContentNode.Attributes("alt").Value
Image.Title = ContentNode.Attributes("title").Value
Image.Width = ContentNode.Attributes("width").Value
Image.Height = ContentNode.Attributes("height").Value
SubPod.PodImage = Image

Next

Pod.SubPods.Add(SubPod)

Next

WA_QueryResult.Pods.Add(Pod)

Next

Return WA_QueryResult

End Function

#End Region

End Class

Regards
- James

View the full article
 
Back
Top