gpt_check.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # -*- coding:utf-8 -*-
  2. """
  3. 校验模块
  4. GPT回复的各个校验模块"""
  5. import json
  6. import re
  7. class CheckGptAnswer:
  8. @staticmethod
  9. def default_no_check(gpt_text: str):
  10. return True
  11. @staticmethod
  12. def score_value(gpt_text: str):
  13. if gpt_text.count("【取值0】") > 1:
  14. return False
  15. return True if re.findall("【取值.+?】", gpt_text) else False
  16. @staticmethod
  17. def original_modify(gpt_text: str):
  18. split_text = gpt_text.split("\n")
  19. for t in split_text:
  20. if "修改理由" in t and "错误" in t and len(t) <= 25:
  21. return False
  22. elif "没有严重的语法错误" in t:
  23. return False
  24. if "【原句】" in gpt_text and "【修改后】" in gpt_text:
  25. return True
  26. else:
  27. return False
  28. @staticmethod
  29. def count_chinese_characters_50(s: str):
  30. chinese_count = 0
  31. for char in s:
  32. if '\u4e00' <= char <= '\u9fff':
  33. chinese_count += 1
  34. return True if s and chinese_count / len(s) >= 0.5 else False
  35. @staticmethod
  36. def count_english_count_30(s: str, english_words_count=30):
  37. words_count = len(re.findall(r"[a-zA-Z\']+", s))
  38. return True if words_count >= english_words_count else False
  39. @staticmethod
  40. def count_letter_percentages(s: str, letter_percentages=0.8):
  41. count_letter = 0
  42. total_length = len(s)
  43. for char in s:
  44. if char.isalpha():
  45. count_letter += 1
  46. result = True if round(count_letter / total_length, 2) > letter_percentages else False
  47. return result
  48. class CheckArticleResult:
  49. @staticmethod
  50. def default_no_check(gpt_text: str):
  51. return True
  52. @staticmethod
  53. def get_article_1(gpt_text: str):
  54. try:
  55. json_object = json.loads(gpt_text)
  56. except json.decoder.JSONDecodeError:
  57. return False
  58. if not all(i in json_object for i in ["englishArticle", "chineseArticle", "difficultSentences", "usedMeanIds", "questions"]):
  59. return False
  60. try:
  61. for question in json_object['questions']:
  62. analysis = question['analysis']
  63. words_count_pct = len(re.findall(r"[a-zA-Z\']+", analysis)) / len(analysis)
  64. if words_count_pct > 0.5:
  65. return False
  66. except:
  67. return False
  68. return True
  69. if __name__ == '__main__':
  70. text = """{
  71. "difficultSentences": [
  72. {
  73. "english": "However, even on his sick days, James carries a spirit of courage and never lets himself rest for too long.",
  74. "chinese": "然而,即使在他生病的日子里,詹姆斯仍带着勇气,从未让自己放松太久。"
  75. }
  76. ],
  77. "usedMeanIds": [749, 1945, 1597, 1953, 2038, 2162, 1625],
  78. "englishArticle": "Overcoming Illness with Courage\n\nJames has always been an active person. Whether it is running in the park, playing basketball, or hiking in the mountains, he never likes to rest. However, last week, he suddenly felt a horrible headache. Alongside the headache, he experienced toothache and fever. The illness made him sick and he had to lie in bed. Not only was he unable to move around freely, but he also had to place an ice pack on his knee and neck to ease the pain.\n\nHis family was worried and thought it would be a serious matter. His mother sat beside him, asking if he needed a break or any help. James, showing his spirit of courage, assured her that he would recover soon. He understood that even when life presents challenges, they must be faced with bravery.\n\nHowever, even on his sick days, James carries a spirit of courage and never lets himself rest for too long. With each passing day, he begins to feel a bit better. The fever decreases, and his headaches become less frequent. Soon, he looks forward to the day he can return to his active lifestyle, encouraged by the fact that he fought his illness with bravery and determination.",
  79. "chineseArticle": "勇敢地战胜疾病\n\n詹姆斯一直是个活跃的人。无论是在公园跑步,打篮球,还是爬山,他从不喜欢放松。然而,上周他突然感到头痛欲裂。除了头痛外,他还感到牙痛和发烧。这场病让他生病了,他不得不躺在床上。他不仅无法自由活动,还得在膝盖和颈部放冰袋来缓解疼痛。\n\n他的家人很担心,觉得这会是个严重的问题。他的母亲坐在他旁边,询问他是否需要间歇或者帮忙。詹姆斯展现了他的勇气,向她保证他会很快康复。他明白,即使生活给出挑战,也必须勇敢面对。\n\n然而,即使在他生病的日子里,詹姆斯仍带着勇气,从未让自己放松太久。随着每一天的过去,他开始感觉好一点了。发烧开始下降,头痛也减少了。他期待着能回到他积极的生活方式,鼓舞于他凭借勇气和决心战胜了疾病。",
  80. "questions": [
  81. {
  82. "trunk": "What activities does James enjoy?",
  83. "analysis": "根据文章,詹姆斯喜欢活跃的生活方式,比如跑步、打篮球和登山。",
  84. "candidates": [
  85. {
  86. "label": "A",
  87. "text": "Swimming",
  88. "isRight": 0
  89. },
  90. {
  91. "label": "B",
  92. "text": "Running, basketball, and hiking",
  93. "isRight": 1
  94. },
  95. {
  96. "label": "C",
  97. "text": "Reading books",
  98. "isRight": 0
  99. },
  100. {
  101. "label": "D",
  102. "text": "Painting",
  103. "isRight": 0
  104. }
  105. ]
  106. },
  107. {
  108. "trunk": "How did James's family react to his illness?",
  109. "analysis": "文章中提到,詹姆斯的家人担心他的健康情况。",
  110. "candidates": [
  111. {
  112. "label": "A",
  113. "text": "They were indifferent",
  114. "isRight": 0
  115. },
  116. {
  117. "label": "B",
  118. "text": "They were worried",
  119. "isRight": 1
  120. },
  121. {
  122. "label": "C",
  123. "text": "They laughed",
  124. "isRight": 0
  125. },
  126. {
  127. "label": "D",
  128. "text": "They scolded him",
  129. "isRight": 0
  130. }
  131. ]
  132. },
  133. {
  134. "trunk": "What symptoms did James experience?",
  135. "analysis": "根据文章,詹姆斯有头痛、牙痛和发烧等症状。",
  136. "candidates": [
  137. {
  138. "label": "A",
  139. "text": "Coughing and sneezing",
  140. "isRight": 0
  141. },
  142. {
  143. "label": "B",
  144. "text": "Headache, toothache, and fever",
  145. "isRight": 1
  146. },
  147. {
  148. "label": "C",
  149. "text": "Stomach ache",
  150. "isRight": 0
  151. },
  152. {
  153. "label": "D",
  154. "text": "Sore throat",
  155. "isRight": 0
  156. }
  157. ]
  158. },
  159. {
  160. "trunk": "How did James approach his recovery?",
  161. "analysis": "詹姆斯通过保持勇气和决心来对待他的康复,并没有让自己放松太久。",
  162. "candidates": [
  163. {
  164. "label": "A",
  165. "text": "With defeat",
  166. "isRight": 0
  167. },
  168. {
  169. "label": "B",
  170. "text": "With carelessness",
  171. "isRight": 0
  172. },
  173. {
  174. "label": "C",
  175. "text": "With courage and determination",
  176. "isRight": 1
  177. },
  178. {
  179. "label": "D",
  180. "text": "With anger",
  181. "isRight": 0
  182. }
  183. ]
  184. }
  185. ]
  186. }"""
  187. text2 = r"""{
  188. "difficultSentences": [
  189. {
  190. "english": "Even when she was feeling sick, she showed great spirit and continued her project with the same dedication.",
  191. "chinese": "即使她感觉生病了,她仍然展示出极大的勇气,继续以同样的奉献精神进行她的项目。"
  192. }
  193. ],
  194. "usedMeanIds": [
  195. 1945,
  196. 1953,
  197. 1816,
  198. 544,
  199. 1453,
  200. 2038,
  201. 1597,
  202. 1625
  203. ],
  204. "englishArticle": "A Day with Unexpected Challenges\n\nIt was a regular Friday morning when Emily woke up with a headache. The thought of going to school was discomforting, but she decided to push through. Emily dressed slowly, feeling each movement in her knee as she descended the stairs for breakfast. Her mother noticed her pale face and asked if she was alright. 'I have a bad headache and a sore neck,' Emily replied. Her mother suggested she might be coming down with a fever and checked her temperature.\n\nDespite feeling under the weather, Emily showed great spirit and chose to attend school due to an important math test. When she reached the classroom, her friends greeted her with smiles, but Emily felt too sick to smile back. During the test, every question seemed to be a major problem. Her vision blurred as the headache intensified, and she wondered if she could finish the test. She reminded herself to stay focused, even as she felt increasingly feverish.\n\nAfter the test, Emily took a break and went to the nurse's office to rest. The nurse noticed her flushed skin and lethargic demeanor. 'You need to rest and possibly see a doctor,' the nurse advised, noting that a toothache could also be contributing to her discomfort. As Emily lay on the cot, she felt thankful for the brief respite from the classroom chaos. She knew that her health mattered more than anything else. The day, though challenging, taught Emily the importance of listening to her body and asking for help if necessary.",
  205. "chineseArticle": "一个意想不到挑战的一天\n\n这是一个普通的星期五早晨,艾米莉醒来时头痛。想到要去学校就让人难受,但她决定继续坚持。艾米莉慢慢穿好衣服,感受到在她下楼吃早餐时每一步膝盖的酸痛。她的母亲注意到她脸色苍白,问她是不是不舒服。“我头很痛,脖子也痛,”艾米莉回答。她的母亲建议她可能是发烧了,并检查了她的体温。\n\n尽管身体不适,艾米莉展示了极大的勇气,选择去上学,因为那天有一场重要的数学考试。当她到达教室时,她的朋友们以微笑迎接她,但艾米莉觉得太不舒服了,无法回应微笑。在考试期间,每一个问题似乎都成了一个大问题。她的视线模糊,因为头痛加剧,她怀疑自己是否能完成考试。即使感到愈发发烧,她也提醒自己要保持专注。\n\n考试结束后,艾米莉休息片刻,去了校医室休息。校医注意到她泛红的皮肤和疲惫的样子。“你需要休息,可能需要看医生,”校医建议,并指出牙痛也可能是她不适的原因。当艾米莉躺在床上时,她感激不已,感谢能从课堂的混乱中得到片刻的喘息。她知道健康比什么都重要。这一天虽然充满挑战,却教会了艾米莉聆听自己身体的重要性,并在必要时寻求帮助。",
  206. "questions": [
  207. {
  208. "trunk": "What was Emily's main challenge during the day?",
  209. "analysis": "根据文章,Emily主要面对的挑战是身体不适。",
  210. "candidates": [
  211. {
  212. "label": "A",
  213. "text": "Completing her math test",
  214. "isRight": 0
  215. },
  216. {
  217. "label": "B",
  218. "text": "Feeling unwell with a headache and fever",
  219. "isRight": 1
  220. },
  221. {
  222. "label": "C",
  223. "text": "Greeting her friends",
  224. "isRight": 0
  225. },
  226. {
  227. "label": "D",
  228. "text": "Attending a birthday party",
  229. "isRight": 0
  230. }
  231. ]
  232. },
  233. {
  234. "trunk": "Why did Emily decide to go to school?",
  235. "analysis": "她去了学校是因为有重要的数学考试。",
  236. "candidates": [
  237. {
  238. "label": "A",
  239. "text": "To see her friends",
  240. "isRight": 0
  241. },
  242. {
  243. "label": "B",
  244. "text": "Because of an important math test",
  245. "isRight": 1
  246. },
  247. {
  248. "label": "C",
  249. "text": "To visit the nurse",
  250. "isRight": 0
  251. },
  252. {
  253. "label": "D",
  254. "text": "To join a sports event",
  255. "isRight": 0
  256. }
  257. ]
  258. },
  259. {
  260. "trunk": "What did the nurse advise Emily to do?",
  261. "analysis": "校医建议Emily应该休息并可能看医生。",
  262. "candidates": [
  263. {
  264. "label": "A",
  265. "text": "Return to class",
  266. "isRight": 0
  267. },
  268. {
  269. "label": "B",
  270. "text": "Rest and possibly see a doctor",
  271. "isRight": 1
  272. },
  273. {
  274. "label": "C",
  275. "text": "Take more classes",
  276. "isRight": 0
  277. },
  278. {
  279. "label": "D",
  280. "text": "Continue the test",
  281. "isRight": 0
  282. }
  283. ]
  284. },
  285. {
  286. "trunk": "What lesson did Emily learn from her experience?",
  287. "analysis": "文章最后提到Emily意识到健康的重要性。",
  288. "candidates": [
  289. {
  290. "label": "A",
  291. "text": "To finish tests quickly",
  292. "isRight": 0
  293. },
  294. {
  295. "label": "B",
  296. "text": "The importance of health and asking for help",
  297. "isRight": 1
  298. },
  299. {
  300. "label": "C",
  301. "text": "To study more often",
  302. "isRight": 0
  303. },
  304. {
  305. "label": "D",
  306. "text": "How to greet friends properly",
  307. "isRight": 0
  308. }
  309. ]
  310. }
  311. ]
  312. }"""
  313. json_text = json.loads(text2)
  314. print(json_text)