# -*- coding:utf-8 -*-
"""
校验模块
GPT回复的各个校验模块"""
import json
import re
from typing import List
from enum import Enum
from pydantic import BaseModel,ValidationError

from tools.loglog import log_err_e


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:
           
            english_article = json_object['englishArticle']
            words_count_pct = len(re.findall(r"[^\u4e00-\u9fff]", english_article)) / len(english_article)
            if words_count_pct < 0.15: 
                return False
           
            chinese_article = json_object['chineseArticle']
            words_count_pct = len(re.findall(r"[^\u4e00-\u9fff]", chinese_article)) / len(chinese_article)
            if words_count_pct > 0.85: 
                return False

            analysis = json_object['questions'][0]['analysis'] 
            words_count_pct = len(re.findall(r"[a-zA-Z\']+", analysis))/len(analysis)
            if words_count_pct>0.5:
                return False
        except Exception as e:
            log_err_e(e,"get_article_1函数校验")
            return False

        return True



class IsRight(Enum):
    RIGHT = 1
    WRONG = 0


class Options(Enum):
    A = "A"
    B = "B"
    C = "C"
    D = "D"


class DifficultSentence(BaseModel):
    english: str
    chinese: str


class Candidate(BaseModel):
    label: Options
    text: str
    isRight: IsRight


class Question(BaseModel):
    trunk: str
    analysis: str
    candidates: List[Candidate]


class Article(BaseModel):
    difficultSentences: List[DifficultSentence]
    usedMeanIds: List[int]
    questions: List[Question]
    englishArticle: str
    chineseArticle: str

class Annotation(BaseModel):
    annotation_text:str

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 = """{\n  "englishArticle": "Simple Agreement in the Office\nSarah is a bright girl. She works as a clerk in a busy office. One day, her boss asked her to prepare a forecast about migration for the company. They had made an agreement to share their ideas every Friday.\nSarah worked hard and talked with other clerks. She used numbers and information to make her forecast. On Friday, Sarah and her friends joined a meeting room. They shared their agreements and ideas with the boss. The boss was happy because Sarah’s forecast was simple and easy to understand. All the clerks learned something new that day.",\n  "chineseArticle": "办公室里的简单协议\n莎拉是个聪明的女孩。她在一家忙碌的办公室做职员。有一天，她的老板让她为公司准备一个有关迁移的预报。他们已经达成协议，每周五分享彼此的想法。\n莎拉很努力地工作，还与其他店员交流。她用数据和信息来做预测。周五，莎拉和她的朋友们一起进入会议室。他们和老板分享了自己的协议和想法。老板很开心，因为莎拉的预报简单易懂。那一天，所有的职员都学到了新东西。",\n  "difficultSentences": [\n    {\n      "english": "They had made an agreement to share their ideas every Friday.",\n      "chinese": "他们已经达成协议，每周五分享彼此的想法。"\n    }\n  ],\n  "usedMeanIds": [1743, 2495, 5069, 1826, 1451],\n  "questions": [\n    {\n      "trunk": "Who is the bright girl in the office?",\n      "analysis": "由文章一开始介绍可知，莎拉是那个聪明的女孩，注意这考察对文章角色的理解。",\n      "candidates": [\n        {"label":"A", "text":"Sarah", "isRight":1},\n        {"label":"B", "text":"The boss", "isRight":0},\n        {"label":"C", "text":"The cook", "isRight":0},\n        {"label":"D", "text":"John", "isRight":0}\n      ]\n    },\n    {\n      "trunk": "What did Sarah's boss ask her to prepare?",\n      "analysis": "文章第二句明确提到老板让莎拉准备关于迁移的预报，考查文中信息定位。",\n      "candidates": [\n        {"label":"A", "text":"A forecast about migration", "isRight":1},\n        {"label":"B", "text":"A birthday party", "isRight":0},\n        {"label":"C", "text":"A sports game", "isRight":0},\n        {"label":"D", "text":"A song", "isRight":0}\n      ]\n    },\n    {\n      "trunk": "When do Sarah and her friends share their ideas?",\n      "analysis": "文章有一句话提到，他们每周五分享想法，此题考察细节记忆。",\n      "candidates": [\n        {"label":"A", "text":"Every Friday", "isRight":1},\n        {"label":"B", "text":"Every Monday", "isRight":0},\n        {"label":"C", "text":"Every Sunday", "isRight":0},\n        {"label":"D", "text":"Every morning", "isRight":0}\n      ]\n    },\n    {\n      "trunk": "Why was the boss happy with Sarah's forecast?",\n      "analysis": "文章最后一句说明老板因为莎拉的预报简单易懂而开心，此题考查对原因的理解。",\n      "candidates": [\n        {"label":"A", "text":"It was simple and easy to understand", "isRight":1},\n        {"label":"B", "text":"It was very long", "isRight":0},\n        {"label":"C", "text":"It was colorful", "isRight":0},\n        {"label":"D", "text":"It had pictures", "isRight":0}\n      ]\n    }\n  ]\n}"""

   
    json_text = json.loads(repr(text2))
   
   
   
