Skip to Content
Author's profile photo Frank Schuler

Make SAP Leonardo Machine Learning work for you

As mentioned in my blog Takeaways from 2017 SAPPHIRE NOW, the biggest topic there was SAP Leonardo: Intelligently connecting people, things, and businesses. In fact, SAP Leonardo is not strictly monolithic but comprises of the following components:

  • Internet of Things
  • Machine Learning
  • Analytics
  • Big Data
  • Blockchain
  • Data Intelligence
  • Design Thinking

In this blog, I focus on the Machine Learning aspect of SAP Leonardo. Technically speaking, SAP Leonardo Machine Learning is a set of 11 Functional Services of which 7 are still in Alpha state:

My scenario is a typical machine learning one: a chat bot. To keep it simple, I make my chat bot just language aware, nothing else. For this I chose the Language Detection API:

There are great tutorials how to Getting started with the SAP API Business Hub, so I will not repeat the details here.

My chat bot is a simple Java application, that accepts input from the user and if the used language is detected as German, then the bot greets with “Willkommen zu SAP Leonardo maschinelles Lernen”, otherwise with “Welcome to SAP Leonardo Machine Learning”:

import org.json.JSONObject;
public class Leonardo {
	public static void main(String[] args) {
		DataOutputStream dataOut = null;
		BufferedReader in = null;
		try {
			InputStreamReader r = new InputStreamReader(;  
			BufferedReader br = new BufferedReader(r);  
			System.out.print("? ");  
			String text = br.readLine();  
			String url = "";
			URL urlObj = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
			//setting request method
			//adding headers
			//sending POST request
			dataOut = new DataOutputStream(connection.getOutputStream());
			dataOut.writeBytes("{  \"message\": \"" + text + "\"}");
			int responseCode = connection.getResponseCode();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
			while ((inputLine = in.readLine()) != null) {
			//printing response
			JSONObject obj = new JSONObject(response.toString());
			String langCode = obj.getString("langCode");
			if (langCode.equals("de")) {
				System.out.println("Willkommen zu SAP Leonardo maschinelles Lernen!");
			} else {
				System.out.println("Welcome zu SAP Leonardo Machine Learning!");
		} catch (Exception e) {
			//do something with exception
		} finally {
			try {
				if(dataOut != null) {
				if(in != null) {
			} catch (IOException e) {
				//do something with exception

The interesting bit of the code that sends the input to the SAP API sandbox system and receives the response back is in fact auto generated from the API Hub including my API key. I only need to extract the langCode field for which I use this org.json implementation JSON In Java » 20170516.

With this, my chat bot detects my language and greets me respectively. If you like, please try this with completely arbitrary German and not German input:

I kept this example as simple as possible so that you can easily adopt it, but at the same time I hope it shows you the potential of SAP Leonardo Machine Learning.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo saurabh chikate
      saurabh chikate

      Hi Frank,

      Thanks for sharing.

      This is quite similar to Text Analysis found in HANA. ?

      I guess they are using the same thing under the hood.

      Author's profile photo Frank Schuler
      Frank Schuler
      Blog Post Author

      Thank you Saurabh,

      I believe you are right ?. What I like with SAP Leonardo Machine Learning is the ease of use through the API though.