Last active
November 17, 2019 13:55
-
-
Save anupamchugh/f45475ea55109b23fb241ab72585bb64 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 FirebaseMLNaturalLanguage | |
| struct ContentView: View { | |
| @State var name: String = "" | |
| @State var languageIdentified: String = "" | |
| var body: some View { | |
| VStack(alignment: .center, spacing: 20){ | |
| TextField("Enter some text", text: $name) | |
| .font(.system(size: 20)) | |
| .multilineTextAlignment(.center) | |
| Text(languageIdentified) | |
| Button(action: identifyLanguage, label: { | |
| Text("Identify Language").foregroundColor(.blue) | |
| }) | |
| } | |
| } | |
| func identifyLanguage(){ | |
| let languageId = NaturalLanguage.naturalLanguage().languageIdentification() | |
| languageId.identifyLanguage(for: name) { (languageCode, error) in | |
| if let error = error { | |
| print("Failed with error: \(error)") | |
| return | |
| } | |
| if let languageCode = languageCode, languageCode != "und" { | |
| self.languageIdentified = languageCode | |
| } else { | |
| self.languageIdentified = "No language was identified" | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment