I’m an absolute beginner in MQL trying to access Freebase by ken-rb in this way:
[1] pry(main)> res = Ken.get("/en/allium_neapolitanum")
=> #<Resource id="/en/allium_neapolitanum" name="Allium neapolitanum">
I noticed the query submitted via freebase API:
{"guid":null,"name":null,"ken:type":[
{"id":null,"name":null,"properties":[
{"id":null,
"name":null,
"expected_type":null,
"unique":null,
"reverse_property":null,
"master_property":null}]}],
"/type/reflect/any_master":[
{"id":null,
"link":null,
"name":null,
"optional":true,
"limit":999999}],
"/type/reflect/any_reverse":[
{"id":null,
"link":null,
"name":null,
"optional":true,
"limit":999999}],
"/type/reflect/any_value":[
{"link":null,
"value":null,
"optional":true,
"limit":999999}],
"id":"/en/allium_neapolitanum"}
fiddling with this MQL in the query editor I found a way to get simply a list of common names for my resource:
[2]pry(#<Ken::Resource>):2> data["/type/reflect/any_value"].map { |h| h }
=> [{"link"=>"/type/object/name", "value"=>"Allium neapolitanum"},
{"link"=>"/type/object/name", "value"=>"שום משולש"},
{"link"=>"/type/object/name", "value"=>"Ail blanc"},
{"link"=>"/biology/organism_classification/scientific_name",
"value"=>"Allium neapolitanum"},
{"link"=>"/type/object/name", "value"=>"Neapolitanischer Lauch"}]
[3] pry(#<Ken::Resource>):2> names = data["/type/reflect/any_value"].select { |h| h["link"]=="/type/object/name"}
=> [{"link"=>"/type/object/name", "value"=>"Allium neapolitanum"},
{"link"=>"/type/object/name", "value"=>"שום משולש"},
{"link"=>"/type/object/name", "value"=>"Ail blanc"},
{"link"=>"/type/object/name", "value"=>"Neapolitanischer Lauch"}]
[4] pry(#<Ken::Resource>):2> names.map { |name| name["value"] }
=> ["Allium neapolitanum", "שום משולש", "Ail blanc", "Neapolitanischer Lauch"]
The problem is: I need to know which language (I means an attribute such as: “lang: ‘/lang/fr'”) is related to each common name.
There is a chance, using Ken or by changing the MQL query / submit others queries / etc. , to also have what is the language of each “/type/object/name”?
[EDIT]
I found a possible approach to the solution, however, my goal would be to get the data directly modifying the original query originated by ken-rb ( .. copied to the top of the question )
The MQL could be:
[{
"name": [{
"lang": null,
"value": null
}]
"id": "/en/allium_neapolitanum"
}]
give this result
"result": [{
"id": "/en/allium_neapolitanum",
"name": [
{
"lang": "/lang/en",
"value": "Allium neapolitanum"
},
{
"lang": "/lang/he",
"value": "שום משולש"
},
{
"lang": "/lang/fr",
"value": "Ail blanc"
},
{
"lang": "/lang/de",
"value": "Neapolitanischer Lauch"
}
]
}]
According to the documentation Ken supports Ken.mqlread() in addition to Ken.get(), so you could use that with the MQL query that you worked out.
Having said that, I would argue that a) it’s a bug that they query multiple languages without returning the language info and b) you might not want to use Ken anyway since it appears to still be using the old freebase.com API which is deprecated and about to be turned off. The new googleapis.com/freebase APIs are documented here. The MQL syntax is unchanged, but you’ll need an API key and need to use the new endpoint.
If you want to fix Ken to return the language with the text value, you can modify the any_value portion of the query to look like this:
I think you should also be able to use just
but that gives you the language and value, but no link, so you can’t tell what property the value is associated with (not very useful).
Note that you can drop the top level
nameproperty from your query since it’s technically redundant. If you want to keep it for simpler access to the name, you could consider changing it from"name":nullto"name":[{}]which will give you the name in all languages (all this information is also available in the /reflect/any_value) portion of the query.Finally, I feel compelled to point out that querying all values of all properties is going to be more expensive and slower than necessary in almost all cases since one normally is only interested in a few key bits of info. It’s find for a general purpose browser/explorer/debugger, but huge overkill for almost everything else.