Created
November 17, 2019 13:57
-
-
Save anupamchugh/110b96615fd8441fcd73c970aad904b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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