func answer()

in QuestionAnswering/QuestionAnswering/QuestionAnswering.swift [118:149]


    func answer(_ question: String, _ text: String) -> String {
        if text.isEmpty {
            return ""
        }
        
        if token2id.isEmpty {
            for (idx, word) in vocab.enumerated() {
                token2id[word] = idx
                id2token[idx] = word
            }
        }
        
        var result = ""
        do {
            let tokenIds = try tokenizer(question: question, text: text)
            if let tids = module.answer(tokenIds: tokenIds as [Any]) {
                for (n, tid) in tids.enumerated() {
                    result += id2token[tid.intValue]!
                    if n != tids.count - 1 {
                        result += " "
                    }
                }
                result = result.replacingOccurrences(of: " ##", with: "").replacingOccurrences(of: #" (?=\p{P})"#, with: "", options: .regularExpression
                )
            }
        }
        catch {
            return "Tokenization exception - Question_Too_Long"
        }
                        
        return result
    }