Extract Teneo ML Dataset

The following code extracts all classes and their training examples in the Class Manager from a solution and generates a tsv file which can be used to import classes to another solution. This is very useful when you need to move all the contents in the Class Manager from an old solution to a new one.

import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

static String login(serverURL,username,password) {

	def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/auth/login"
	HttpRequest request = HttpRequest.newBuilder()
			.uri(URI.create(apiUrl))
			.header("content-type", "application/json")
			.method("POST", HttpRequest.BodyPublishers.ofString(new JsonBuilder(["username":username,"password":password]).toString()))
			.build()

	HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
	if (response.statusCode() == 200) {
		def accessToken = new JsonSlurper().parseText(response.body())
		return accessToken
	} else {
		throw new Exception("Login Error. Error code: "+response.statusCode())    
	}	
		
}	

static void logout(serverURL,accessToken) {

		def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/auth/logout"
		HttpRequest request = HttpRequest.newBuilder()
			.uri(URI.create(apiUrl))
			.header("content-type", "application/json")
			.header("Authorization", "Bearer "+accessToken)
			.method("POST", HttpRequest.BodyPublishers.noBody())
			.build()

		HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()) 
		
	}

static List<HashMap> getIntents(serverURL,solutionId,accessToken) {

	def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/classes/" + solutionId
	HttpRequest request = HttpRequest.newBuilder()
			.uri(URI.create(apiUrl))
			.header("content-type", "application/json")
			.header("Authorization", "Bearer "+accessToken)
			.method("GET", HttpRequest.BodyPublishers.noBody())
			.build()

	HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
	if (response.statusCode() == 200) {
		def classes = new JsonSlurper().parseText(response.body())
		return classes
	} else {
		throw new Exception("Error code: "+response.statusCode())
	}
		
}
	
static void writeImportFile(intents,fileName){
	
	BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))
	List temp_intents = []
	for (intent in intents){
	    for (example in intent.trainingData){
	        temp_intents << intent.name+"\t"+example+"\n"
	    }
	}
	Collections.sort(temp_intents)
	for (line in temp_intents) writer.write(line)
	writer.close()
		
}

public static void main(def args) {

	String serverURL = "your_server_url" // replace with your server url
	String username = "your_username" // replace with your username 
	String password = "your_password" // replace with your password
	String solutionId = "your_solution_id" // replace with your solution id
	String outputFile = "your_classes.tsv" // default file name

	def accessToken = login(serverURL,username,password)
	def intents = getIntents(serverURL,solutionId,accessToken)
	writeImportFile(intents,outputFile)
	logout(serverURL,accessToken)
	
}

To use the code, you need to fill in the following information in the main function:

  • serverURL: the server URL. You can find it by opening the Studio and clicking on the About button on the left, and copy the URL from the Connected to:

  • username: username you use to login the Teneo Studio.

  • password: password you use to login the Teneo Studio.

  • solutionId: the ID of the solution from which you export the classes. You can find the solution by clicking on the Solutions button on the left and selecting the solution you need (without opening it). The solution ID will be shown within the Properties panel on the right-hand side:

  • outputFile: the path and name of the output file which should end with .tsv such as “C:/Documents/ClassesImport.tsv”. This is an optional argument. If you don’t assign a name to the output file, the code will generate a file named your_classes.tsv in the current folder.

After filling in the information, please save it as a Groovy file such as ExportDataset.groovy. Then run the following command in the Command Prompt of your system (install groovy first if you don’t have it):

groovy ExportDataset.groovy 
2 Likes