# -*- coding:utf-8 -*-
"""
校验模块
GPT回复的各个校验模块"""
import json
import re


class CheckGptAnswer:
    @staticmethod
    def default_no_check(gpt_text: str):
       
        return True

   
    @staticmethod
    def score_value(gpt_text: str):
       
        if gpt_text.count("【取值0】") > 1:
            return False
        return True if re.findall("【取值.+?】", gpt_text) else False

   
    @staticmethod
    def original_modify(gpt_text: str):
        split_text = gpt_text.split("\n")
        for t in split_text:
           
            if "修改理由" in t and "错误" in t and len(t)<=25:
                return False
            elif "没有严重的语法错误" in t:
                return False

        if "【原句】" in gpt_text and "【修改后】" in gpt_text:
            return True
        else:
            return False

   
    @staticmethod
    def count_chinese_characters_50(s: str):
        chinese_count = 0
        for char in s:
           
            if '\u4e00' <= char <= '\u9fff':
                chinese_count += 1
        return True if s and chinese_count/len(s) >= 0.5 else False

   
    @staticmethod
    def count_english_count_30(s: str,english_words_count=30):
        words_count = len(re.findall(r"[a-zA-Z\']+",s))
        return True if words_count >= english_words_count else False

   
    @staticmethod
    def count_letter_percentages(s:str,letter_percentages=0.8):
        count_letter=0
       
        total_length = len(s)

       
        for char in s:
           
            if char.isalpha():
               
                count_letter += 1
        result = True if round(count_letter/total_length,2)>letter_percentages else False
        return result


class CheckArticleResult:
    @staticmethod
    def default_no_check(gpt_text: str):
       
        return True

    @staticmethod
    def get_article_1(gpt_text: str):
       
        try:
            json_object = json.loads(gpt_text)
        except json.decoder.JSONDecodeError:
            return False
       
        if not all(i in json_object for i in ["englishArticle","chineseArticle","difficultSentences","usedMeanIds","questions"]):
            return False
       
        try:
            for question in json_object['questions']:
                analysis = question['analysis'] 
                words_count_pct = len(re.findall(r"[a-zA-Z\']+", analysis))/len(analysis)
                if words_count_pct>0.5:
                    return False
        except:
            return False

        return True

if __name__ == '__main__':
   
   
   
   
   

    text = """{
  "difficultSentences": [
    {
      "english": "However, even on his sick days, James carries a spirit of courage and never lets himself rest for too long.",
      "chinese": "然而，即使在他生病的日子里，詹姆斯仍带着勇气，从未让自己放松太久。"
    }
  ],
  "usedMeanIds": [749, 1945, 1597, 1953, 2038, 2162, 1625],
  "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.",
  "chineseArticle": "勇敢地战胜疾病\n\n詹姆斯一直是个活跃的人。无论是在公园跑步，打篮球，还是爬山，他从不喜欢放松。然而，上周他突然感到头痛欲裂。除了头痛外，他还感到牙痛和发烧。这场病让他生病了，他不得不躺在床上。他不仅无法自由活动，还得在膝盖和颈部放冰袋来缓解疼痛。\n\n他的家人很担心，觉得这会是个严重的问题。他的母亲坐在他旁边，询问他是否需要间歇或者帮忙。詹姆斯展现了他的勇气，向她保证他会很快康复。他明白，即使生活给出挑战，也必须勇敢面对。\n\n然而，即使在他生病的日子里，詹姆斯仍带着勇气，从未让自己放松太久。随着每一天的过去，他开始感觉好一点了。发烧开始下降，头痛也减少了。他期待着能回到他积极的生活方式，鼓舞于他凭借勇气和决心战胜了疾病。",
  "questions": [
    {
      "trunk": "What activities does James enjoy?",
      "analysis": "根据文章，詹姆斯喜欢活跃的生活方式，比如跑步、打篮球和登山。",
      "candidates": [
        {
          "label": "A",
          "text": "Swimming",
          "isRight": 0
        },
        {
          "label": "B",
          "text": "Running, basketball, and hiking",
          "isRight": 1
        },
        {
          "label": "C",
          "text": "Reading books",
          "isRight": 0
        },
        {
          "label": "D",
          "text": "Painting",
          "isRight": 0
        }
      ]
    },
    {
      "trunk": "How did James's family react to his illness?",
      "analysis": "文章中提到，詹姆斯的家人担心他的健康情况。",
      "candidates": [
        {
          "label": "A",
          "text": "They were indifferent",
          "isRight": 0
        },
        {
          "label": "B",
          "text": "They were worried",
          "isRight": 1
        },
        {
          "label": "C",
          "text": "They laughed",
          "isRight": 0
        },
        {
          "label": "D",
          "text": "They scolded him",
          "isRight": 0
        }
      ]
    },
    {
      "trunk": "What symptoms did James experience?",
      "analysis": "根据文章，詹姆斯有头痛、牙痛和发烧等症状。",
      "candidates": [
        {
          "label": "A",
          "text": "Coughing and sneezing",
          "isRight": 0
        },
        {
          "label": "B",
          "text": "Headache, toothache, and fever",
          "isRight": 1
        },
        {
          "label": "C",
          "text": "Stomach ache",
          "isRight": 0
        },
        {
          "label": "D",
          "text": "Sore throat",
          "isRight": 0
        }
      ]
    },
    {
      "trunk": "How did James approach his recovery?",
      "analysis": "詹姆斯通过保持勇气和决心来对待他的康复，并没有让自己放松太久。",
      "candidates": [
        {
          "label": "A",
          "text": "With defeat",
          "isRight": 0
        },
        {
          "label": "B",
          "text": "With carelessness",
          "isRight": 0
        },
        {
          "label": "C",
          "text": "With courage and determination",
          "isRight": 1
        },
        {
          "label": "D",
          "text": "With anger",
          "isRight": 0
        }
      ]
    }
  ]
}"""
    text2 = r"""{
    "difficultSentences": [
        {
            "english": "Even when she was feeling sick, she showed great spirit and continued her project with the same dedication.",
            "chinese": "即使她感觉生病了，她仍然展示出极大的勇气，继续以同样的奉献精神进行她的项目。"
        }
    ],
    "usedMeanIds": [
        1945,
        1953,
        1816,
        544,
        1453,
        2038,
        1597,
        1625
    ],
    "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.",
    "chineseArticle": "一个意想不到挑战的一天\n\n这是一个普通的星期五早晨，艾米莉醒来时头痛。想到要去学校就让人难受，但她决定继续坚持。艾米莉慢慢穿好衣服，感受到在她下楼吃早餐时每一步膝盖的酸痛。她的母亲注意到她脸色苍白，问她是不是不舒服。“我头很痛，脖子也痛，”艾米莉回答。她的母亲建议她可能是发烧了，并检查了她的体温。\n\n尽管身体不适，艾米莉展示了极大的勇气，选择去上学，因为那天有一场重要的数学考试。当她到达教室时，她的朋友们以微笑迎接她，但艾米莉觉得太不舒服了，无法回应微笑。在考试期间，每一个问题似乎都成了一个大问题。她的视线模糊，因为头痛加剧，她怀疑自己是否能完成考试。即使感到愈发发烧，她也提醒自己要保持专注。\n\n考试结束后，艾米莉休息片刻，去了校医室休息。校医注意到她泛红的皮肤和疲惫的样子。“你需要休息，可能需要看医生，”校医建议，并指出牙痛也可能是她不适的原因。当艾米莉躺在床上时，她感激不已，感谢能从课堂的混乱中得到片刻的喘息。她知道健康比什么都重要。这一天虽然充满挑战，却教会了艾米莉聆听自己身体的重要性，并在必要时寻求帮助。",
    "questions": [
        {
            "trunk": "What was Emily's main challenge during the day?",
            "analysis": "根据文章，Emily主要面对的挑战是身体不适。",
            "candidates": [
                {
                    "label": "A",
                    "text": "Completing her math test",
                    "isRight": 0
                },
                {
                    "label": "B",
                    "text": "Feeling unwell with a headache and fever",
                    "isRight": 1
                },
                {
                    "label": "C",
                    "text": "Greeting her friends",
                    "isRight": 0
                },
                {
                    "label": "D",
                    "text": "Attending a birthday party",
                    "isRight": 0
                }
            ]
        },
        {
            "trunk": "Why did Emily decide to go to school?",
            "analysis": "她去了学校是因为有重要的数学考试。",
            "candidates": [
                {
                    "label": "A",
                    "text": "To see her friends",
                    "isRight": 0
                },
                {
                    "label": "B",
                    "text": "Because of an important math test",
                    "isRight": 1
                },
                {
                    "label": "C",
                    "text": "To visit the nurse",
                    "isRight": 0
                },
                {
                    "label": "D",
                    "text": "To join a sports event",
                    "isRight": 0
                }
            ]
        },
        {
            "trunk": "What did the nurse advise Emily to do?",
            "analysis": "校医建议Emily应该休息并可能看医生。",
            "candidates": [
                {
                    "label": "A",
                    "text": "Return to class",
                    "isRight": 0
                },
                {
                    "label": "B",
                    "text": "Rest and possibly see a doctor",
                    "isRight": 1
                },
                {
                    "label": "C",
                    "text": "Take more classes",
                    "isRight": 0
                },
                {
                    "label": "D",
                    "text": "Continue the test",
                    "isRight": 0
                }
            ]
        },
        {
            "trunk": "What lesson did Emily learn from her experience?",
            "analysis": "文章最后提到Emily意识到健康的重要性。",
            "candidates": [
                {
                    "label": "A",
                    "text": "To finish tests quickly",
                    "isRight": 0
                },
                {
                    "label": "B",
                    "text": "The importance of health and asking for help",
                    "isRight": 1
                },
                {
                    "label": "C",
                    "text": "To study more often",
                    "isRight": 0
                },
                {
                    "label": "D",
                    "text": "How to greet friends properly",
                    "isRight": 0
                }
            ]
        }
    ]
}"""
    json_text = json.loads(text2)
    print(json_text)
   
   
