/** * Chatbot with Voice * * This program implements a chatbot with voice using the OpenAI API. * The chatbot supports three languages: Farsi, Turkish, and English. * It takes user input in any of these languages and generates a response in the same language. * * Note: To use this program, you need to have an OpenAI API key and set it in the `apiKey` variable. */ import ai.openai.OpenAi fun main() { // Set the OpenAI API key val apiKey = "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" // Initialize the OpenAI client val openAi = OpenAi(apiKey) // Start the chatbot chatbot(openAi) } fun chatbot(openAi: OpenAi) { // Supported languages val languages = listOf("farsi", "turkish", "english") // Prompt for user input println("Welcome to the Chatbot with Voice!") println("Supported languages: Farsi, Turkish, English") println("Enter your message (type 'exit' to quit):") // Chat loop var input = readLine() while (input != "exit") { // Determine the language of the input val language = detectLanguage(input) // Check if the language is supported if (language in languages) { // Generate a response using the OpenAI API val response = openAi.generateResponse(input, language) // Speak the response speak(response) // Prompt for next input println("Enter your message (type 'exit' to quit):") input = readLine() } else { println("Unsupported language. Please enter a message in Farsi, Turkish, or English.") input = readLine() } } println("Goodbye!") } fun detectLanguage(input: String): String { // TODO: Implement language detection logic // You can use a language detection library or API to determine the language of the input // For simplicity, this function returns a fixed language based on the first character of the input return when (input.firstOrNull()?.toLowerCase()) { 'f' -> "farsi" 't' -> "turkish" else -> "english" } } fun speak(message: String) { // TODO: Implement text-to-speech logic // You can use a text-to-speech library or API to convert the message to speech // For simplicity, this function just prints the message println("Chatbot: $message") } class OpenAi(private val apiKey: String) { fun generateResponse(input: String, language: String): String { // TODO: Implement OpenAI API logic // You need to call the OpenAI API with the input and language parameters // and return the generated response // For simplicity, this function just returns a fixed response based on the language return when (language) { "farsi" -> "سلام! چطور میتونم به شما کمک کنم؟" "turkish" -> "Merhaba! Size nasıl yardımcı olabilirim?" else -> "Hello! How can I assist you?" } } }
Write, Run & Share Kotlin code online using OneCompiler’s Kotlin online compiler for free. It’s a modern and fast online playground for Kotlin, supporting the latest version and ideal for learning, experimenting, and sharing code instantly.
Kotlin is a statically typed, modern programming language developed by JetBrains. It runs on the JVM and is fully interoperable with Java. Kotlin is concise, expressive, and safe, and it’s officially supported by Google for Android app development.
The following is a simple Kotlin program that prints a greeting:
fun main() {
println("Hello, OneCompiler!")
}
OneCompiler’s Kotlin editor supports stdin. You can provide input using the I/O tab. Here's a sample program that reads a line of input and prints a greeting:
fun main() {
print("Enter your name: ")
val name = readLine()
println("Hello, $name")
}
val name: String = "OneCompiler" // Immutable
var age: Int = 25 // Mutable
Kotlin supports type inference, so explicit types are optional:
val city = "Hyderabad"
var count = 10
val score = 85
if (score >= 50) {
println("Pass")
} else {
println("Fail")
}
for (i in 1..5) {
println(i)
}
var i = 1
while (i <= 5) {
println(i)
i++
}
var j = 1
do {
println(j)
j++
} while (j <= 5)
fun add(a: Int, b: Int): Int {
return a + b
}
fun greet(name: String) = "Hello, $name"
val items = listOf("apple", "banana", "cherry")
for (item in items) {
println(item)
}
This guide provides a quick reference to Kotlin programming syntax and features. Start coding in Kotlin using OneCompiler’s Kotlin online compiler today!