我正在编写Phoenix编程书,我的Wolfram服务没有按预期工作。
当我在iex -S mix
中运行以下命令时:
Rumbl.InfoSys.compute("what is the meaning of life?")
它返回:
[]
期望值是这样的:
[%Rumbl.InfoSys.Result{backend: %Rumbl.User{...}, score: 95,
text: "42\n(according to the book The Hitchhiker", url: nil}]
以下是lib/rumbl/info_sys/wolfram.ex
defmodule Rumbl.InfoSys.Wolfram do
import SweetXml
alias Rumbl.InfoSys.Result
def start_link(query, query_ref, owner, limit) do
Task.start_link(__MODULE__, :fetch, [query, query_ref, owner, limit])
end
def fetch(query_str, query_ref, owner, _limit) do
query_str
|> fetch_xml()
|> xpath(~x"/queryresult/pod[contains(@title, 'Result') or
contains(@title, 'Definitions')]
/subpod/plaintext/text()")
|> send_results(query_ref, owner)
end
defp send_results(nil, query_ref, owner) do
send(owner, {:results, query_ref, []})
end
defp send_results(answer, query_ref, owner) do
results = [%Result{backend: "wolfram", score: 95, text: to_string(answer)}]
send(owner, {:results, query_ref, results})
end
defp fetch_xml(query_str) do
{:ok, {_, _, body}} = :httpc.request(
String.to_char_list("http://api.wolframalpha.com/v2/query" <>
"?appid=#{app_id()}" <>
"&input={URI.encode(query_str)}&format=plaintext"))
body
end
defp app_id, do: Application.get_env(:rumbl, :wolfram)[:app_id]
end
...和lib/rumbl/info_sys/supervisor.ex
defmodule Rumbl.InfoSys.Supervisor do
use Supervisor
def start_link() do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_opts) do
children = [
worker(Rumbl.InfoSys, [], restart: :temporary)
]
supervise children, strategy: :simple_one_for_one
end
end
为什么我在iex
中没有获得正确的返回值?任何帮助表示赞赏 - 如果您想查看任何其他文件,请告诉我。
答案 0 :(得分:0)
您确定没有使用WolframID /电子邮件地址而不是AppID吗?这是我的错误,没有Wolfram的经验。对于appid你必须作为wolfram。请参阅获取AppID http://products.wolframalpha.com/api/documentation.html#1
祝你好运,Stefan Houtzager。答案 1 :(得分:0)
{URI.encode(query_str)}
应为#{URI.encode(query_str)}