comprehensive_score.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import json
  2. from fuzzywuzzy import fuzz
  3. class Comprehensive_Score():
  4. def __init__(self, dev_mode):
  5. with open("model_data/keyword_mapping.json", 'r', encoding="utf8") as f:
  6. keyword_mapping = json.load(f)
  7. # 根据ksy和fxb判断值的次方
  8. self.power = 2 if dev_mode == "ksy" else 1
  9. self.knowledge2id = keyword_mapping["knowledge2id"]
  10. self.quantity2id = keyword_mapping["quantity2id"]
  11. self.init_id2max_id = keyword_mapping["init_id2max_id"]
  12. def __call__(self, query, refer, scale):
  13. score_dict = dict()
  14. quesType = self.compute_quesType(query["quesType"], refer["quesType"]["quesType"])
  15. knowledge = self.compute_knowledge(query["knowledge"], refer["knowledge"])
  16. solving_type = self.compute_solving_type(query["solving_type"], refer["solving_type"])
  17. difficulty = self.compute_difficulty(query["difficulty"], refer["difficulty"])
  18. physical_quantity = self.compute_physical_quantity(query["physical_quantity"], refer["physical_quantity"])
  19. sum_score = quesType * scale["quesType"] + knowledge * scale["knowledge"] + \
  20. solving_type * scale["solving_type"] + difficulty * scale["difficulty"] + \
  21. physical_quantity * scale["physical_quantity"]
  22. sum_score = int(sum_score * 100) / 100
  23. sum_score = min(sum_score, 1.0)
  24. score_dict["quesType"] = quesType
  25. score_dict["knowledge"] = knowledge
  26. score_dict["solving_type"] = solving_type
  27. score_dict["difficulty"] = difficulty
  28. score_dict["physical_quantity"] = physical_quantity
  29. return sum_score, score_dict
  30. # 知识点/物理场景/物理量相互关联得分计算
  31. def compute_relate_score(self, query_list, refer_list, keyword2id, mode):
  32. query_set, refer_set = set(query_list), set(refer_list)
  33. if query_set == refer_set:
  34. return 1.0
  35. if len(query_set) > len(refer_set):
  36. query_set, refer_set = refer_set, query_set
  37. accumulate_score = 0
  38. max_length = len(refer_set)
  39. # 双层循环计算知识点之间关联得分
  40. for query in query_set:
  41. query_score = 0
  42. query_id = keyword2id.get(query, 0)
  43. for refer in refer_set:
  44. refer_id = keyword2id.get(refer, 0)
  45. if query_id == refer_id:
  46. query_score += 1
  47. continue
  48. # 知识点
  49. if mode == 1:
  50. if abs(query_id - refer_id) < 10 ** self.power: query_score += 0.3
  51. elif abs(query_id - refer_id) < 100 ** self.power: query_score += 0.2
  52. else: continue
  53. elif mode == 2:
  54. if abs(query_id - refer_id) < 100: query_score += 0.2
  55. else: continue
  56. fuzz_score = fuzz.ratio(query, refer)
  57. if mode == 1 and fuzz_score >= 0.4:
  58. query_score += 0.1
  59. # refer长度为1特殊处理
  60. if max_length == 1:
  61. return query_score
  62. # 限定关联得分上限
  63. max_score = 1 / max_length + 1 if query in refer_set else (max_length - 1) / max_length
  64. if query_score > max_score:
  65. accumulate_score += max_score
  66. else:
  67. accumulate_score += query_score
  68. return min(accumulate_score / max_length, 0.85)
  69. # 题型相似度评分
  70. def compute_quesType(self, query, refer):
  71. score = 0.0
  72. if query == refer:
  73. score = 1.0
  74. return score
  75. # 知识点相似度评分
  76. def compute_knowledge(self, query_list, refer_list):
  77. score = self.compute_relate_score(query_list, refer_list, self.knowledge2id, mode=1)
  78. return int(score * 100) / 100
  79. # 试题求解类型相似度评分
  80. def compute_solving_type(self, query_list, refer_list):
  81. query_set, refer_set = set(query_list), set(refer_list)
  82. if len(query_set) > len(refer_set):
  83. query_set, refer_set = refer_set, query_set
  84. same_count = sum([1 for ele in query_set if ele in refer_set])
  85. score = same_count / len(refer_set)
  86. return int(score * 100) / 100
  87. # 难度相似度评分
  88. def compute_difficulty(self, query, refer):
  89. score = 1 - abs(float(query) - float(refer))
  90. return int(score * 100) / 100
  91. # 物理量相似度评分
  92. def compute_physical_quantity(self, query_list, refer_list):
  93. score = self.compute_relate_score(query_list, refer_list, self.quantity2id, mode=2)
  94. return int(score * 100) / 100