1

我在我的项目中使用 Freebase Search Widget,使用它我可以从我的输入框中的建议列表中选择单词,但是如何在另一个文本框中获取类别呢?

示例:http ://suggest-examples.freebaseapps.com/

如果我输入:James Cameron,它将在文本框中被选中,但我还希望在另一个文本框中选择类别?

代码:

$(function() {
  $("#myinput").suggest({
    key: "YOUR-API-KEY-GOES-HERE",
    filter:'(all type:/film/director)'
  });
});
4

1 回答 1

1

Freebase Search 小部件使用Freebase Search API来建议实体。因此,当您在输入字段中键入“james cameron”时,您将发送以下搜索 API 请求:

https://www.googleapis.com/freebase/v1/search?query=james+cameron&exact=false&prefixed=true

您可以在该 API 调用的响应中看到,它传回了名称、ID、排名以及实体的显着特征:

{
  "mid": "/m/03_gd",
  "id": "/en/james_cameron",
  "name": "James Cameron",
  "notable": {
    "name": "Film Director",
    "id": "/m/02jknp"
  },
  "lang": "en",
  "score": 629.205872
},...

All of this data is accessible from the jQuery callback in the Search widget like this:

$(function() {
  $("#myinput").suggest({
    key: "YOUR-API-KEY-GOES-HERE",
    filter:'(all type:/film/director)'
  }).bind("fb-select", function(e, data) {
    alert(data.name + ", " + data.id + " (" + data.notable.name + ")");
  });
});

In this case, James Cameron (/m/03_gd) is notable for being a film director (/m/02jknp).

于 2013-03-30T22:50:10.973 回答