How to get Wikipedia article data from Freebase suggest response - javascript

I'm looking for a simple way to get data about a university (name, native_name, city etc) from the Wikipedia infobox after a user selects a university from Freebase suggest. The dataset returned from freebase, however, is very small and doesn't include the wikipedia link unfortunately.
Currently I am using the "name" property and making an ajax request to http://live.dbpedia.org/data/"+name+".json. This often works but while doing some tests it turned out the name doesn't always map directly to the correct page. Let me split my question in a few to make myself clear:
Is it possible to configure the Freebase suggest plugin so that the
response includes the wikipedia link?
OR is there a similar plugin that queries DBpedia directly and is as
simple and user-friendly as Freebase's?
OR, as a plan B, is there a way to send a request to
"live.dbpedia.org" so that it only returns me the json after
redirects? On the Wikipedia API I can send a "redirects" variable that does this. But then I'd have to parse the data myself…
The problem with the plan B is that nothing guarantees that the freebase object's name will ever lead me to the correct Wikipedia page. Even after the redirects…
I swear I've read a lot of API documentation but everything is extremely confusing and I chose not to read long tutorials about RDF, SPARQL and MQL because I really don't think the solution should be so complicated. I'm asking here because I really hope I'm missing a simple solution…
UPDATE
{
id: "/en/babes-bolyai_university",
lang: "en",
mid: "/m/08c4bf",
name: "Babeş-Bolyai University",
notable: {
id: "/education/university",
name: "College/University"
},
score: 37.733559
}
This is the result I get after selecting "Babeş-Bolyai University" in the suggest widget.
SOLUTION
I assumed I can't configure the Suggest widget to return more data, so after getting the Freebase ID of the object I just send another request, this time with a query specifically asking for the Wikipedia ID. I didn't know any MQL and couldn't find the name of the Freebase field with the Wikipedia ID. Maybe I'm stupid but the Freebase documentation really confuses me. In any case Tom Morris' answer and this question helped me build the query that returned what I wanted:
https://www.googleapis.com/freebase/v1/mqlread?query={"id":"/en/babes-bolyai_university","key":{"namespace":"/wikipedia/en_title","value":null}}
The strings in the result come with numeric codes for special unicode characters though (in my case Babe$0219-Bolyai_University). I've been able to convert the code with String.fromCharCode(parseInt(219, 16)) but if someone knows of a way to convert the whole string that would be helpful. Otherwise I can just make my own function replacing the "$dddd" pattern with the corresponding character.
Thanks for the help!

There isn't a DBpedia autosuggest comparable to Freebase Suggest as far as I'm aware.
Anything that's in Freebase can be retrieved with Suggest by using an MQL expressions in the output parameter. For simple things, e.g. names, aliases, the MQL is basically just a JSON snippet containing the relevant property name.
EDIT: The output parameter doesn't actually appear to be documented in the context of Suggest, but anything that isn't a Suggest parameter gets passed through transparently to the Freebase Search API, so you can use all of the stuff described here: https://developers.google.com/freebase/v1/search-output You can get as much or as little information as you require returned with each suggestion.
If you do need to query DBpedia, you should be using the Wikipedia/DBpedia key, which is not necessarily the same as the name. For English Wikipedia, the key is in the namespace /wikipedia/en or if you want the numeric Wikipedia ID in the namespace `/wikipedia/en_id'. Replace the 'en' with the appropriate language code if you want to query other language Wikipedias. These keys have some non-ASCII characters escaped, so if you need to unescape them, you can use the documentation here: http://wiki.freebase.com/wiki/MQL_key_escaping

You can update the "flyout_service_path" parameter. Here there is a description in the freebase suggest documentation (https://developers.google.com/freebase/v1/suggest). I'm using this configuration for to get all the keys of a entity.
$(inputClass).suggest(
{
"key" : _FREEBASE_API_KEY_BROWSER_APP,
"flyout_service_path":"/search?filter=(all mid:${id})&output=(notable:/client/summary description type /type/object/key)&key=${key}"
}
).bind("fb-select", this.fbSelectedHandler);
In the freebase response I can see now in the "output" parameter the "/type/object/key" with all the keys of the entity (wikipedia,etc..).
My question now is how I can acquire this data from output ?. In the "fb-select" event the "data" variable, don't carry this fields.
Some help, please..

Related

How do I query information from the Trefle API

I didn't want to ask such a basic question but I seem to not be able to find the answer on my own.
How can I query a specific plant without knowing its id?
Such as using the common name or binomial name.
I see that according to the documentation the path is /api/species/{id}, it might be my inexperience on using APIs but I'm left clueless on how to query a specific plant without prior knowledge of their id.
Would anyone be able to give me an explanation of how it works or even better link me to an article to fill the gaps of my API knowledge?
My current knowledge stems from the freecodecamp JSON APIs and AJAX short course which doesn't help when faced with this sort of documentation.
I got the response from another forum and then complemented it by learning more about REST APIs - APIs for Beginners - How to use an API (Full Course / Tutorial)
At the basic level in the form of a URL:
You start with the provided URL - https://trefle.io/api
Select which type of data you want such as Kingdom, subKingdom,
division, etc All the way down to plants or species. -
https://trefle.io/api/plants. As it is mentioned, "Plants
are all main species, without all the varieties, cultivars,
subspecies and forms", and species will give you all that belongs
to that species meaning several plants.
You provide the parameters that you want, with some being required and others being optional.
To know which parameters to use you can read underneath here, such as token, page_size, page, etc.
The first parameters
would go in front of the former link like so with ? -
https://trefle.io/api/plants?q=strawberry and the following parameters
are separated with a & like so -
https://trefle.io/api/plants?q=strawberry&token=yourAPIkey and
you can add any parameters to that such as showing only items with complete
data, minimum ph, or others that might interest you.
But do keep in mind that the database still seems to be particularly incomplete.
Also, this is in the form of a URL, in practice, this would be done differently, I recommend watching the video I linked above where he goes through a lot of this information and showcases helpful tools such as Postman.

First time using an API: How do I construct a request for the proper data from a quote API? Links included

I am using this API from Stands4 to get quotes. I was able to build my random quote generator for free code camp using their RANDOM tag in the request. Now I want to know how to write a request to get quote from a tv show for a personal project I am working on. Does anyone know how this is possible?
Here is the API doc: Link
In particular I am looking at the second and third lines of their Request parameters table. I want to use 'SEARCH' but don't know how to produce the correct 'query'. Maybe I am going about this wrong.
Here is the show I would like to get quotes from: Link
Here is what I tried. Goal is to get a random quote from the show.
http://www.stands4.com/services/v2/quotes.php?uid=MYUID&tokenid=MYTOKENID&searchtype=RANDOM&SEARCH&query=Its+Always+Sunny+In+Philadelphia
Unfortunately, these aren't the greatest docs so you'll have to play around with the "query=" part of your search. You're correct in thinking that's where to start.
Here's the approach I would take to make this work:
1. Searchtype=Search: I would start subbing in all sorts of different things from It's always Sunny and seeing what gets returned, including: character names, the show name, writer/actor names, and if all else fails show objects ("Paddy's pub" and "Day man") come to mind. Note: for things with spaces make sure you're using url-encoding.
2. Searchtype=Author: Take writer names and maybe character names to see if putting those in with an author search type returns anything.
3. Use their Scripts API: I'd hope something would come from that, but if it doesn't work, you can always try and find some scripts from their Scripts API and parse them.
If all else fails you can use a scraping tool like Import.io and grab quotes from a site like IMDB. Import.io is free and very easy to use.

Return formatted value in MongoDB db.collection.find()

I have a MongoDB JavaScript function saved in db.system.js, and I want to use it to both query and produce output data.
I'm able to query the results of the function using the $where clause like so:
db.records.find(
{$where: "formatEmail(this.email.toString(), 'format type') == 'xxx'"},
{email:1})
but I'm unable to use this to return a formatted value for the projected "email" field, like so:
db.records.find({}, {"formatEmail(this.email.toString(), 'format type')": 1})
Is there any way to do this while preserving the ability to simply use a pre-built function?
UPDATE:
Thank you all for your prompt participation.
Let me explain why I need to do this in MongoDB and it's not a matter of client logic at the wrong layer.. What I am really trying to do is use the function for a shard bucketing value. Email was just one example, but in reality, what I have is a hash function that returns a mod value.
I'm aware of Mongo having the ability to shard based on a hashed value, but from what I gather, it produces a highly random value that can burden the re-balancing of shards with unnecessary load. So I want to control it like so func(_id, mod), which would return a value from 0 to say 1000 (depending on the mod value).
Also, I guess I would also like to use the output of the function in some sort of grouping scenario, and I guess Map Reduce does come to mind.. I was just hoping to avoid writing overly complex M/R for something so simple.. also, I don't really know how to do Map Reduce .. lol.
So, I gather that from your answers, there is no way to return any formatted value back from mongo (without map/reduce), is that right?
I think you are mixing your "layers" of functionality here -- the database stores and retrieves data, thats all. What you need to do is:
* get that data and store the cursor in a variable
* loop through your cursor, and for every record you go through
* format and output your record as you see fit.
This is somewhat similar to what you have described in your question, but its not part of MongoDB and you have to provide the "formatEmail" function in your "application layer"
Hope it helps
As #alernerdev has already mentioned, this is generally not done at a database layer. However, sometimes storing a pre-formatted version in your database is the way to go. Here's some instances where you may wish to store extra data:
If you need to lookup data in a particular format. For example, I have a "username" and a "usernameLowercase" fields for my primary user collection. The lowercase'd one is indexed, and is the one I use for username lookup. The mixed-case one is used for displaying the username.
If you need to report a large amount of data in a particular format. 100,000 email addresses all formatted in a particular way? Probably best to just store them in that format in the db.
If your translation from one format to another is computationally expensive. Doubly so if you're processing lots of records.
In this case, if all you're doing is looking up or retrieving an email in a specific format, I'd recommend adding a field for it and then indexing it. That way you won't need to do actual document retrieval for the lookup or the display. Super fast. Disk storage space for something the size of an email address is super cheap!

Reddit Search API URL?

I'm trying to implement a search using reddit's api, but am not having much luck:
http://www.reddit.com/search.json?q=ferrari?jsonp=?
It's returning some json formatted text but no results.. If I search "cars", it returns 2 results, and the rest of my code won't recognize the objects. (Plus, I know there's more than 2 results for cars). Any idea on how to modify the URL?
Your URL looks wrong. I think you used a question mark instead of an amp to separate request variables.
I tried using http://www.reddit.com/search.json?q=ferrari and I got a lot of results in JSON format.
This will allow you to do the most basic search for reddit posts.
You can add other parameters, described on the reddit API page : http://www.reddit.com/dev/api#GET_search. You can also limit your search to one subreddit.
For example, to get the same results, sort by newest : http://www.reddit.com/search.json?q=ferrari&sort=new
It does work for me like so :
http://www.reddit.com/search.json?q=rest&jsonp=callback
and there is limit parameter which by default is 25 and the maximum is 100

OData:Wildcard (startswith) filtering for number (ID) fields in the url request

I've been researching a way to perform fuzzy searches for my numerical ID fields of my entity via OData and JavaScript. So far, I have not found the answer I am looking for. I can filter other edm.string columns perfectly using the "Startswith" filter option, however when i attempt to pass in any other non-string type, I get a type error response from the server back.
In applications that I control the database, I was successfully able to get around this, by creating views I need and converting the numerical type of the view to a string. However, this seems a bit overkill to go out of my way and create a view for an entire set of data just so I can allow a user to wildcard search the ID, when otherwise the OData query works perfectly well.
Has anyone found a good solution to this? Thank you!
Unfortunately I think you've already discovered one of the best solutions (creating a view). You could also create a service operation that allows you to do fuzzy search.
What are you using for your backend? This isn't supported on LINQ-to-Entities, but you might be able to create a service operation that looks something like this (to prove that it can work, you can stuff a ToList() call in there after Products, just be sure not to deploy something like that to production :)):
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ScratchService : DataService<ScratchContext>
{
[WebGet]
public IQueryable<Product> FuzzySearch(string idStartsWith)
{
var context = new ScratchContext();
return context.Products.Where(p => p.ID.ToString().StartsWith(idStartsWith));
}
// ...
}
It's not an ask we've heard a lot, but I can definitely bring it up on the team and as we start the OASIS standardization process it's something we can be thinking about.

Categories