A couple of weeks ago I’ve heard about the Google Knowledge Graph API for the first time. All of you who followed my work in the last years probably know that I’m interested in all kind of public APIs and Graph Databases. Obviously I’ve started to play around with this API immediatelly.


As described on https://developers.google.com/knowledge-graph/ I’ve registered my user and got an API key.


My first call: https://kgsearch.googleapis.com/v1/entities:search?query=chuck%20norris&key=my_API_key&limit=1

{
  “@context”: {
    “@vocab”: “http://schema.org/“,
    “goog”: “http://schema.googleapis.com/“,
    “EntitySearchResult”: “goog:EntitySearchResult”,
    “detailedDescription”: “goog:detailedDescription”,
    “resultScore”: “goog:resultScore”,
    “kg”: “http://g.co/kg
  },
  “@type”: “ItemList”,
  “itemListElement”: [
    {
      “@type”: “EntitySearchResult”,
      “result”: {
        “@id”: “kg:/m/015lhm”,
        “name”: “Chuck Norris”,
        “@type”: [
          “Person”,
          “Thing”
        ],
        “description”: “Martial Artist”,
        “image”: {
          “contentUrl”: “http://t3.gstatic.com/images?q=tbn:ANd9GcTOLvRnq7ANkycMmFAT0UoC1kzQXFAzBUkBTOEMMJvLMMXu-QWt“,
          “url”: “https://en.wikipedia.org/wiki/Chuck_Norris
        },
        “detailedDescription”: {
          “articleBody”: “Carlos Ray \”Chuck\” Norris is an American martial artist, actor, film producer and screenwriter. After serving in the United States Air Force, he began his rise to fame as a martial artist, and has since founded his own school, Chun Kuk Do.\n”,
          “url”: “http://en.wikipedia.org/wiki/Chuck_Norris“,
          “license”: “https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License
        },
        “url”: “http://www.chucknorris.com/
      },
      “resultScore”: 729.463074
    }
  ]
}

Not really “Knowledge Graph” style. Where is the linked data like movies etc.?


Back to the homepage. Et voilà, better read before you judge:

“Note: The Knowledge Graph Search API returns only individual matching entities, rather than graphs of interconnected entities. If you need the latter, we recommend using data dumps from Wikidata instead.”

But why Google calls it “Knowledge Graph Search” then? Hopefully they’ll change their minds in the near future…

Nevertheless, I’ve created a new ABAP Open Source project to use the Google Knowledge Graph Search: zGKGS


Google Knowledge Graph Search API for ABAP

With just a few lines of code you get nice results:

DATA(google_knowledge_graph_search) = NEW zcl_gkgs_api( i_api_key = ‘Your_API_Key’ ).

google_knowledge_graph_search->search(
  EXPORTING
    i_query     = ‘sap’
    i_limit     = 5
  IMPORTING
    e_results   = DATA(results)
).

LOOP AT results ASSIGNING FIELD-SYMBOL(<result>).

  cl_demo_output=>begin_section( title = <result>->get_result( )->get_description( ) ).

  IF <result>->get_result( )->get_image( ) IS BOUND.
    cl_demo_output=>write_html( html = |<image src=”{ <result>->get_result( )->get_image( )->get_content_url( ) }“/>| ).
  ENDIF.

  cl_demo_output=>write_text( <result>->get_result( )->get_detailed_description( )->get_article_body( ) ).
  cl_demo_output=>end_section( ).

ENDLOOP.

cl_demo_output=>display( ).

/wp-content/uploads/2016/01/search_864537.png

How does it work? Well, the most interesting part of zGKGS is my next open source project: SchemA


SchemA: The schema.org ABAP framework


What is schema.org?

“Schema.org is a collaborative, community activity with a mission to create, maintain, and promote schemas for structured data on the Internet, on web pages, in email messages, and beyond.”


It’s a project sponsored by Google, Microsoft, Yahoo and Yandex and runs under the hood of W3C. More information you can find on the homepage of schema.org.


Based on the schema.org objects, I’ve created an OO framework in ABAP. Let’s look at an example:

DATA(organization) = NEW zcl_schema_organization( ).

organization->set_legal_name( ‘SAP SE’ ).
organization->set_founding_date( ‘19720401’ ).
organization->set_address( NEW #( ) ).
organization->get_address( )->set_address_country( ‘DE’ ).
organization->get_address( )->set_address_locality( ‘Walldorf’ ).

DATA(founder) = organization->get_founder( ).
INSERT NEW zcl_schema_person( ) INTO TABLE founder ASSIGNING FIELD-SYMBOL(<founder>).
<founder>->set_name( ‘Claus Wellenreuther’ ).
INSERT NEW zcl_schema_person( ) INTO TABLE founder ASSIGNING <founder>.
<founder>->set_name( ‘Hans-Werner Hector’ ).
INSERT NEW zcl_schema_person( ) INTO TABLE founder ASSIGNING <founder>.
<founder>->set_name( ‘Klaus Tschira’ ).
INSERT NEW zcl_schema_person( ) INTO TABLE founder ASSIGNING <founder>.
<founder>->set_name( ‘Dietmar Hopp’ ).
INSERT NEW zcl_schema_person( ) INTO TABLE founder ASSIGNING <founder>.
<founder>->set_name( ‘Hasso Plattner’ ).
organization->set_founder( founder ).

cl_demo_output=>display_json( organization->get_json( ) ).

If (hopefully) everything works fine you’ll get a response in JSON-LD (linked data) format:

{
“@context”:
{
  “@vocab”:”http://schema.org/
},
“@type”:”Organization”,
“address”:
{
  “@type”:”PostalAddress”,
  “addressCountry”:”DE”,
  “addressLocality”:”Walldorf”
},
“founder”:
[
  {
   “@type”:”Person”,
   “name”:”Claus Wellenreuther”
  },
  {
   “@type”:”Person”,
   “name”:”Hans-Werner Hector”
  },
  {
   “@type”:”Person”,
   “name”:”Klaus Tschira”
  },
  {
   “@type”:”Person”,
   “name”:”Dietmar Hopp”
  },
  {
   “@type”:”Person”,
   “name”:”Hasso Plattner”
  }
],
“foundingDate”:”1972-04-01″,
“legalName”:”SAP SE”
}

To validate the output, you can copy the string and paste it into the JSON-LD playground page on http://json-ld.org/playground/ .


Need help


If you look at the page http://schema.org/docs/full.html you’ll see, that there are hundreds of objects to be created in ABAP. If you want to support the SchemA project, please go to the project wiki under https://github.com/se38/SchemA/wiki/How-to-contribute-to-the-project and read the instructions (will be created soon). Thank you!


These projects are created under NW7.50 SP1 and tested under NW740 SP10 but the target version should be NW7.40 SP8, so please let me know if there are some syntax errors.


Conclusion

The next time your supplier of choice asks you in which format they can expect your purchase order, just response with: http://schema.org/Order 😉

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply