Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created November 17, 2019 13:57
Show Gist options
  • Save anupamchugh/110b96615fd8441fcd73c970aad904b9 to your computer and use it in GitHub Desktop.
Save anupamchugh/110b96615fd8441fcd73c970aad904b9 to your computer and use it in GitHub Desktop.
import SwiftUI
import FirebaseMLNLSmartReply
struct ContentView: View {
@State var name: String = ""
@State var smartReply: String = ""
var body: some View {
VStack(alignment: .center, spacing: 20){
TextField("Enter some text", text: $name)
.font(.system(size: 20))
.multilineTextAlignment(.center)
Text(smartReply)
Button(action: composeSmartReply, label: {
Text("Smart Reply").foregroundColor(.blue)
})
}
}
func composeSmartReply(){
var conversation: [TextMessage] = []
let message = TextMessage(
text: name,
timestamp: Date().timeIntervalSince1970,
userID: "userId",
isLocalUser: false)
conversation.append(message)
let naturalLanguage = NaturalLanguage.naturalLanguage()
naturalLanguage.smartReply().suggestReplies(for: conversation) { result, error in
guard error == nil, let result = result else {
print("error \(error?.localizedDescription ?? "")")
return
}
if (result.status == .notSupportedLanguage) {
self.smartReply = "Language Not Supported."
} else if (result.status == .success) {
self.smartReply = result.suggestions[0].text
}
else if result.status == .noReply{
self.smartReply = "Offensive text. No Reply"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment