【发布时间】:2020-12-15 23:40:51
【问题描述】:
如何比较两个人的反应并计算相似度百分比?例如,在我下面的数据库中,Kevin 用“yes”回答 questionId,用“no”回答 questionId1。约翰回答“是”和“是”。我希望输出显示 50%,因为他们对 1 有相同的答案,而对另一个有不同的答案。
我正在尝试,但不确定如何比较其他用户:
func calculatePercentage(completion: @escaping ([String])->()) {
let postRef = self.databaseRef.child("responses").child("Kevin")
postRef.observeSingleEvent(of: .value, with: { (snapshot) in
var userIdArray = [String]()
for topic in snapshot.children.allObjects as! [DataSnapshot] {
let question1 = topic.childSnapshot(forPath: "questionId").value
let question2 = topic.childSnapshot(forPath: "questionId1").value
userIdArray.append(topic as! String)
}
completion(userIdArray)
})
}
【问题讨论】:
-
看看 Levenstein 算法计算相似度的百分比
-
我不了解 Levenstein 但不会进行以下工作,(questionsWithSameAnswer / totalNumberOfQuestions) * 100
-
谢谢!有没有一个简单的公式来计算相同答案的问题?我有一个数组 ["question1: yes", "question2: no"] - 如何将相同的答案与另一个数组 ["question1: yes", "question2: yes"] 进行比较和计数?
标签: swift firebase firebase-realtime-database swift5