comprehensive_score.py 4.6 KB

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