|
|
@@ -112,40 +112,77 @@ class Question:
|
|
|
"""
|
|
|
根据题型生成题目
|
|
|
"""
|
|
|
- if self.type == "A":
|
|
|
- data = self._generate_type_a()
|
|
|
- data['type'] = "A"
|
|
|
- elif self.type == "B":
|
|
|
- data = self._generate_type_b()
|
|
|
- data['type'] = "B"
|
|
|
- elif self.type == "C":
|
|
|
- data = self._generate_type_c()
|
|
|
- data['type'] = "C"
|
|
|
- elif self.type == "D":
|
|
|
- data = self._generate_type_d()
|
|
|
- data['type'] = "D"
|
|
|
- elif self.type == "E":
|
|
|
- data = self._generate_type_e()
|
|
|
- data['type'] = "E"
|
|
|
- else:
|
|
|
- self.type= random.choice(["A", "B", "C", "D", "E"])
|
|
|
- data = self.generate()
|
|
|
+ valid_types = ["A", "B", "C", "D", "E"]
|
|
|
+
|
|
|
+ # 如果类型无效,随机选择一个有效类型
|
|
|
+ if self.type not in valid_types:
|
|
|
+ self.type = random.choice(valid_types)
|
|
|
+ return self.generate()
|
|
|
+
|
|
|
+ # 调用对应类型的生成方法
|
|
|
+ generator_method = getattr(self, f"_generate_type_{self.type.lower()}")
|
|
|
+ data = generator_method()
|
|
|
+ data['type'] = self.type
|
|
|
+
|
|
|
return data
|
|
|
|
|
|
@classmethod
|
|
|
- def _generate_type_a(cls) -> Dict[str, Any]:
|
|
|
+ def _ensure_cache_loaded(cls, cache_type: str) -> None:
|
|
|
"""
|
|
|
- 推理未知数:给出四个数字(1-13)算24,其中一个是x,并且x等于另外三个数(这三个数不能相同)中的一个。
|
|
|
- 要求找出x的可能值,并写出相应的算式
|
|
|
+ 确保缓存已加载,并验证指定类型的缓存是否有可用数据
|
|
|
"""
|
|
|
if not cls._is_cache_loaded:
|
|
|
cls._load_cache()
|
|
|
|
|
|
- if not cls._cache['A']:
|
|
|
- raise ValueError("缓存中没有可用的A类型题目数据")
|
|
|
+ if not cls._cache[cache_type]:
|
|
|
+ raise ValueError(f"缓存中没有可用的{cache_type}类型题目数据")
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def _get_random_item(cls, cache_type: str) -> Dict[str, Any]:
|
|
|
+ """
|
|
|
+ 从指定类型的缓存中随机获取一个题目
|
|
|
+ """
|
|
|
+ cls._ensure_cache_loaded(cache_type)
|
|
|
+ return random.choice(cls._cache[cache_type])
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def _format_solutions(cls, solutions: List[Dict], flag_value: Optional[int] = None) -> List[Dict]:
|
|
|
+ """
|
|
|
+ 统一格式化解法数据
|
|
|
+ 如果指定了flag_value,则只保留该flag值的解法
|
|
|
+ """
|
|
|
+ answers = []
|
|
|
+ for solution in solutions:
|
|
|
+ # 如果指定了flag值且当前解法的flag不匹配,则跳过
|
|
|
+ if flag_value is not None and solution.get('flag', 0) != flag_value:
|
|
|
+ continue
|
|
|
+
|
|
|
+ answers.append({
|
|
|
+ "expression": solution.get('expression', ''),
|
|
|
+ "flag": flag_value if flag_value is not None else solution.get('flag', 0)
|
|
|
+ })
|
|
|
+ return answers
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def _create_question_result(cls, question_text: str, numbers: List, answers: List) -> Dict[str, Any]:
|
|
|
+ """
|
|
|
+ 创建标准格式的问题结果对象
|
|
|
+ """
|
|
|
+ return {
|
|
|
+ "question": question_text,
|
|
|
+ "numbers": numbers,
|
|
|
+ "answers": answers
|
|
|
+ }
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def _generate_type_a(cls) -> Dict[str, Any]:
|
|
|
+ """
|
|
|
+ 推理未知数:给出四个数字(1-13)算24,其中一个是x,并且x等于另外三个数(这三个数不能相同)中的一个。
|
|
|
+ 要求找出x的可能值,并写出相应的算式
|
|
|
+ """
|
|
|
+ # 获取随机题目
|
|
|
+ item = cls._get_random_item('A')
|
|
|
|
|
|
- # 从缓存中随机选择一个题目
|
|
|
- item = random.choice(cls._cache['A'])
|
|
|
original_numbers = item.get('numbers', [])
|
|
|
solutions = item.get('solutions', [])
|
|
|
|
|
|
@@ -180,8 +217,6 @@ class Question:
|
|
|
numbers = distinct_nums.copy()
|
|
|
numbers.insert(unknown_pos, None) # None 表示未知数 x
|
|
|
|
|
|
-
|
|
|
-
|
|
|
# 构造答案
|
|
|
answers = []
|
|
|
for possible_x in distinct_nums:
|
|
|
@@ -201,138 +236,72 @@ class Question:
|
|
|
if len(answers)>1:
|
|
|
question_text += f"(共有{len(answers)}个答案,请写全)"
|
|
|
|
|
|
- return {
|
|
|
- "question": question_text,
|
|
|
- "numbers": numbers,
|
|
|
- "answers": answers
|
|
|
- }
|
|
|
+ return cls._create_question_result(question_text, numbers, answers)
|
|
|
|
|
|
@classmethod
|
|
|
def _generate_type_b(cls) -> Dict[str, Any]:
|
|
|
"""
|
|
|
一题多解1:生成恰好有两个解法的题目
|
|
|
"""
|
|
|
- if not cls._is_cache_loaded:
|
|
|
- cls._load_cache()
|
|
|
-
|
|
|
- if not cls._cache['B']:
|
|
|
- raise ValueError("缓存中没有可用的B类型题目数据")
|
|
|
-
|
|
|
- # 从缓存中随机选择一个题目
|
|
|
- item = random.choice(cls._cache['B'])
|
|
|
+ # 获取随机题目
|
|
|
+ item = cls._get_random_item('B')
|
|
|
numbers = item.get('numbers', [])
|
|
|
solutions = item.get('solutions', [])
|
|
|
|
|
|
question_text = "一题多解:请找出这组数字的所有24点解法。"
|
|
|
|
|
|
- # 转换解法格式
|
|
|
- answers = []
|
|
|
- for solution in solutions:
|
|
|
- answers.append({
|
|
|
- "expression": solution.get('expression', ''),
|
|
|
- "flag": solution.get('flag', 0)
|
|
|
- })
|
|
|
+ # 格式化解法
|
|
|
+ answers = cls._format_solutions(solutions)
|
|
|
|
|
|
- return {
|
|
|
- "question": question_text,
|
|
|
- "numbers": numbers,
|
|
|
- "answers": answers
|
|
|
- }
|
|
|
+ return cls._create_question_result(question_text, numbers, answers)
|
|
|
|
|
|
@classmethod
|
|
|
def _generate_type_c(cls) -> Dict[str, Any]:
|
|
|
"""
|
|
|
一题多解2:生成有三个或以上解法的题目
|
|
|
"""
|
|
|
- if not cls._is_cache_loaded:
|
|
|
- cls._load_cache()
|
|
|
-
|
|
|
- if not cls._cache['C']:
|
|
|
- raise ValueError("缓存中没有可用的C类型题目数据")
|
|
|
-
|
|
|
- # 从缓存中随机选择一个题目
|
|
|
- item = random.choice(cls._cache['C'])
|
|
|
+ # 获取随机题目
|
|
|
+ item = cls._get_random_item('C')
|
|
|
numbers = item.get('numbers', [])
|
|
|
solutions = item.get('solutions', [])
|
|
|
|
|
|
question_text = "一题多解:请找出这组数字的所有24点解法!"
|
|
|
|
|
|
- # 转换解法格式
|
|
|
- answers = []
|
|
|
- for solution in solutions:
|
|
|
- answers.append({
|
|
|
- "expression": solution.get('expression', ''),
|
|
|
- "flag": solution.get('flag', 0)
|
|
|
- })
|
|
|
+ # 格式化解法
|
|
|
+ answers = cls._format_solutions(solutions)
|
|
|
|
|
|
- return {
|
|
|
- "question": question_text,
|
|
|
- "numbers": numbers,
|
|
|
- "answers": answers
|
|
|
- }
|
|
|
+ return cls._create_question_result(question_text, numbers, answers)
|
|
|
|
|
|
@classmethod
|
|
|
def _generate_type_d(cls) -> Dict[str, Any]:
|
|
|
"""
|
|
|
一星题目:生成包含flag=1的题目
|
|
|
"""
|
|
|
- if not cls._is_cache_loaded:
|
|
|
- cls._load_cache()
|
|
|
-
|
|
|
- if not cls._cache['D']:
|
|
|
- raise ValueError("缓存中没有可用的D类型题目数据")
|
|
|
-
|
|
|
- # 从缓存中随机选择一个题目
|
|
|
- item = random.choice(cls._cache['D'])
|
|
|
+ # 获取随机题目
|
|
|
+ item = cls._get_random_item('D')
|
|
|
numbers = item.get('numbers', [])
|
|
|
solutions = item.get('solutions', [])
|
|
|
|
|
|
- question_text = "一星题目:请找出这组数字的24点解法。"
|
|
|
-
|
|
|
- # 转换解法格式,只保留flag=1的解法
|
|
|
- answers = []
|
|
|
- for solution in solutions:
|
|
|
- if solution.get('flag', 0) == 1:
|
|
|
- answers.append({
|
|
|
- "expression": solution.get('expression', ''),
|
|
|
- "flag": 1
|
|
|
- })
|
|
|
+ question_text = "【一星】题目:请找出这组数字的24点解法。"
|
|
|
|
|
|
- return {
|
|
|
- "question": question_text,
|
|
|
- "numbers": numbers,
|
|
|
- "answers": answers
|
|
|
- }
|
|
|
+ # 格式化解法,只保留flag=1的解法
|
|
|
+ answers = cls._format_solutions(solutions, flag_value=1)
|
|
|
+
|
|
|
+ return cls._create_question_result(question_text, numbers, answers)
|
|
|
|
|
|
@classmethod
|
|
|
def _generate_type_e(cls) -> Dict[str, Any]:
|
|
|
"""
|
|
|
二星题目:生成包含flag=2的题目
|
|
|
"""
|
|
|
- if not cls._is_cache_loaded:
|
|
|
- cls._load_cache()
|
|
|
-
|
|
|
- if not cls._cache['E']:
|
|
|
- raise ValueError("缓存中没有可用的E类型题目数据")
|
|
|
-
|
|
|
- # 从缓存中随机选择一个题目
|
|
|
- item = random.choice(cls._cache['E'])
|
|
|
+ # 获取随机题目
|
|
|
+ item = cls._get_random_item('E')
|
|
|
numbers = item.get('numbers', [])
|
|
|
solutions = item.get('solutions', [])
|
|
|
|
|
|
- question_text = "二星题目:请找出这组数字的24点解法。"
|
|
|
+ question_text = "【二星】题目:请找出这组数字的24点解法。"
|
|
|
|
|
|
- # 转换解法格式,只保留flag=2的解法
|
|
|
- answers = []
|
|
|
- for solution in solutions:
|
|
|
- if solution.get('flag', 0) == 2:
|
|
|
- answers.append({
|
|
|
- "expression": solution.get('expression', ''),
|
|
|
- "flag": 2
|
|
|
- })
|
|
|
+ # 格式化解法,只保留flag=2的解法
|
|
|
+ answers = cls._format_solutions(solutions, flag_value=2)
|
|
|
|
|
|
- return {
|
|
|
- "question": question_text,
|
|
|
- "numbers": numbers,
|
|
|
- "answers": answers
|
|
|
- }
|
|
|
+ return cls._create_question_result(question_text, numbers, answers)
|