Last active
September 17, 2019 07:31
-
-
Save thechaudharysab/f9cc74e5e3247f1355a45e0a1aa54b1e to your computer and use it in GitHub Desktop.
Class created for Smart Reply tutorial: https://medium.com/@ibjects/smart-reply-nlp-firebase-mlkit-for-ios-tutorial-4034f3deb243
This file contains 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
// | |
// QuestionViewController.swift | |
// WorseThanSiri | |
// | |
// Created by Chaudhry Talha on 9/16/19. | |
// Copyright © 2019 ibjects.com. All rights reserved. | |
// | |
import UIKit | |
import Firebase | |
class QuestionViewController: UIViewController { | |
@IBOutlet weak var questionTextField: UITextField! | |
@IBOutlet weak var answerLabel: UILabel! | |
var replies: [String] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
@IBAction func submitButtonPressed(_ sender: Any) { | |
view.endEditing(true) | |
self.answerLabel.text = "..." | |
guard let text = questionTextField.text, !text.isEmpty else { | |
return | |
} | |
//Natural Language accept object of type TextMessage | |
let message = TextMessage(text: questionTextField.text!, timestamp: Date().timeIntervalSince1970, userID: "nothing", isLocalUser: false) | |
let nl = NaturalLanguage.naturalLanguage() | |
nl.smartReply().suggestReplies(for: [message]) { (result, error) in | |
guard error == nil, let result = result else { | |
return | |
} | |
if result.status == .notSupportedLanguage { | |
//The conversation language is not english OR the result doesn't contain any suggestions | |
self.answerLabel.text = "I don't understand." | |
} else if result.status == .success { | |
//Successful | |
for suggestion in result.suggestions { | |
self.replies.append(suggestion.text) | |
} | |
self.answerLabel.text = self.replies.randomElement() | |
} else { | |
self.answerLabel.text = "I don't know the answer to that." | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment