Skip to Content

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 java.net.URL;
import java.net.HttpURLConnection;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import org.json.JSONObject;
public class Leonardo {
	public static void main(String[] args) {
		DataOutputStream dataOut = null;
		BufferedReader in = null;
		try {
			InputStreamReader r = new InputStreamReader(System.in);  
			BufferedReader br = new BufferedReader(r);  
			System.out.print("? ");  
			String text = br.readLine();  
			String url = "https://sandbox.api.sap.com/ml/languagedetection/language";
			URL urlObj = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
			//setting request method
			connection.setRequestMethod("POST");
			//adding headers
			connection.setRequestProperty("Content-Type","application/json");
			connection.setRequestProperty("Accept","application/json");
			connection.setRequestProperty("APIKey","/*yourAPIKey*/");
			connection.setDoInput(true);
			//sending POST request
			connection.setDoOutput(true);
			dataOut = new DataOutputStream(connection.getOutputStream());
			dataOut.writeBytes("{  \"message\": \"" + text + "\"}");
			dataOut.flush();
			int responseCode = connection.getResponseCode();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			//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
			e.printStackTrace();
		} finally {
			try {
				if(dataOut != null) {
					dataOut.close();
				}
				if(in != null) {
					in.close();
				}
			} catch (IOException e) {
				//do something with exception
				e.printStackTrace();
			}
		}
	}
}

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.

To report this post you need to login first.

2 Comments

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

    1. Frank Schuler 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.

      Cheers

      Frank

      (0) 

Leave a Reply