calculate_score.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. calculate_score
  3. """
  4. import os
  5. import re
  6. import json
  7. from Levenshtein import distance
  8. from lib import scoring
  9. from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
  10. from nltk.tokenize import word_tokenize
  11. import nltk
  12. nltk.download('punkt')
  13. class Scoring:
  14. """
  15. calculate_score
  16. """
  17. def __init__(self, result_path):
  18. """
  19. init
  20. """
  21. self.edit_distances = []
  22. self.bleu_scores = []
  23. self.sim_scores = []
  24. self.filenames = []
  25. self.score_dict = {}
  26. self.anntion_cnt = 0
  27. self.fw = open(result_path, "w+", encoding='utf-8')
  28. def simple_bleu_score(self, candidate, reference):
  29. """
  30. get bleu score
  31. """
  32. candidate_tokens = word_tokenize(candidate)
  33. reference_tokens = word_tokenize(reference)
  34. return sentence_bleu([reference_tokens], candidate_tokens, smoothing_function=SmoothingFunction().method1)
  35. def preprocess_string(self, s):
  36. """
  37. preprocess_string
  38. """
  39. sub_enter = re.sub(r'\n+', '\n', s)
  40. return re.sub(r' ', ' ', sub_enter)
  41. def calculate_similarity(self, annotion, actual, tool_type):
  42. """
  43. calculate_similarity
  44. """
  45. class_dict = {}
  46. edit_distances = []
  47. bleu_scores = []
  48. sim_scores = list()
  49. total_file = 0
  50. for filename in os.listdir(annotion):
  51. if filename.endswith('.md') and not filename.startswith('.'):
  52. total_file = total_file + 1
  53. with open(os.path.join(annotion, filename), 'r', encoding='utf-8') as file_a:
  54. content_a = file_a.read()
  55. self.anntion_cnt = self.anntion_cnt + 1
  56. filepath_b = os.path.join(actual, filename)
  57. if os.path.exists(filepath_b):
  58. with open(filepath_b, 'r', encoding='utf-8') as file_b:
  59. content_b = file_b.read()
  60. self.filenames.append(filename)
  61. edit_dist = distance(self.preprocess_string(content_b),self.preprocess_string(content_a)) / max(len(content_a), len(content_b))
  62. self.edit_distances.append(edit_dist)
  63. edit_distances.append(edit_dist)
  64. bleu_score = self.simple_bleu_score(content_b, content_a)
  65. bleu_scores.append(bleu_score)
  66. self.bleu_scores.append(bleu_score)
  67. score = scoring.score_text(content_b, content_a)
  68. sim_scores.append(score)
  69. self.sim_scores.append(score)
  70. class_dict[filename] = {"edit_dist": edit_dist, "bleu_score": bleu_score, "sim_score": score}
  71. self.score_dict[filename] = {"edit_dist": edit_dist, "bleu_score": bleu_score, "sim_score": score}
  72. else:
  73. print(f"File {filename} not found in actual directory.")
  74. class_average_edit_distance = sum(edit_distances) / len(edit_distances) if edit_distances else 0
  75. class_average_bleu_score = sum(bleu_scores) / len(bleu_scores) if bleu_scores else 0
  76. class_average_sim_score = sum(sim_scores) / len(sim_scores) if sim_scores else 0
  77. self.fw.write(json.dumps(class_dict, ensure_ascii=False) + "\n")
  78. ratio = len(class_dict)/total_file
  79. self.fw.write(f"{tool_type} extract ratio: {ratio}" + "\n")
  80. self.fw.write(f"{tool_type} Average Levenshtein Distance: {class_average_edit_distance}" + "\n")
  81. self.fw.write(f"{tool_type} Average BLEU Score: {class_average_bleu_score}" + "\n")
  82. self.fw.write(f"{tool_type} Average Sim Score: {class_average_sim_score}" + "\n")
  83. print (f"{tool_type} extract ratio: {ratio}")
  84. print (f"{tool_type} Average Levenshtein Distance: {class_average_edit_distance}")
  85. print (f"{tool_type} Average BLEU Score: {class_average_bleu_score}")
  86. print (f"{tool_type} Average Sim Score: {class_average_sim_score}")
  87. return self.score_dict
  88. def summary_scores(self):
  89. """
  90. calculate the average of edit distance, bleu score and sim score
  91. """
  92. over_all_dict = dict()
  93. average_edit_distance = sum(self.edit_distances) / len(self.edit_distances) if self.edit_distances else 0
  94. average_bleu_score = sum(self.bleu_scores) / len(self.bleu_scores) if self.bleu_scores else 0
  95. average_sim_score = sum(self.sim_scores) / len(self.sim_scores) if self.sim_scores else 0
  96. over_all_dict["average_edit_distance"] = average_edit_distance
  97. over_all_dict["average_bleu_score"] = average_bleu_score
  98. over_all_dict["average_sim_score"] = average_sim_score
  99. self.fw.write(json.dumps(over_all_dict, ensure_ascii=False) + "\n")
  100. return over_all_dict
  101. def calculate_similarity_total(self, tool_type, download_dir):
  102. """
  103. calculate the average of edit distance, bleu score and sim score
  104. """
  105. annotion = os.path.join(download_dir, "annotations", "cleaned")
  106. actual = os.path.join(download_dir, tool_type, "cleaned")
  107. score = self.calculate_similarity(annotion, actual, tool_type)
  108. return score