You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Curriculum_Design/src/input_handler/input_processor.py

142 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# input_handler/input_processor.py
from PyQt5.QtCore import QObject, pyqtSignal
from typing import Optional
class InputProcessor(QObject):
# 定义信号
text_changed = pyqtSignal(str) # 文本变化信号
key_pressed = pyqtSignal(str) # 按键按下信号
input_completed = pyqtSignal() # 输入完成信号
def __init__(self):
super().__init__()
# 实现构造函数逻辑
# 1. 初始化输入缓冲区
self.input_buffer = ""
# 2. 设置初始状态
self.is_input_active = False
# 3. 初始化相关属性
self.expected_text = ""
self.current_position = 0
def process_key_event(self, key: str) -> bool:
# 实现按键事件处理逻辑
# 1. 检查按键是否有效
if not key:
return False
# 2. 根据按键类型处理(字符、功能键等)
# 3. 更新输入缓冲区
self.input_buffer += key
self.current_position += 1
# 4. 发送text_changed信号
self.text_changed.emit(key)
# 5. 检查是否完成输入如是则发送input_completed信号
if self.expected_text and self.input_buffer == self.expected_text:
self.input_completed.emit()
# 6. 返回处理结果
return True
def get_current_input(self) -> str:
# 实现获取当前输入逻辑
# 1. 返回输入缓冲区内容
return self.input_buffer
def reset_input(self):
# 实现输入重置逻辑
# 1. 清空输入缓冲区
self.input_buffer = ""
# 2. 重置相关状态变量
self.current_position = 0
self.is_input_active = False
# 3. 发送重置信号(如需要)
def set_expected_text(self, text: str):
# 实现设置期望文本逻辑
# 1. 保存期望文本
self.expected_text = text
# 2. 初始化匹配相关状态
self.current_position = 0
self.input_buffer = ""
self.is_input_active = True
class InputValidator:
def __init__(self):
# 实现构造函数逻辑
# 1. 初始化验证规则
self.case_sensitive = True
# 2. 设置默认验证参数
self.min_accuracy = 0.0
def validate_character(self, input_char: str, expected_char: str) -> bool:
# 实现字符验证逻辑
# 1. 比较输入字符与期望字符
# 2. 考虑大小写敏感性设置
if self.case_sensitive:
return input_char == expected_char
else:
return input_char.lower() == expected_char.lower()
# 3. 返回验证结果
def validate_word(self, input_word: str, expected_word: str) -> dict:
# 实现单词验证逻辑
# 1. 逐字符比较输入单词与期望单词
correct_count = 0
incorrect_count = 0
total_chars = max(len(input_word), len(expected_word))
# 2. 统计正确/错误字符数
for i in range(total_chars):
input_char = input_word[i] if i < len(input_word) else ""
expected_char = expected_word[i] if i < len(expected_word) else ""
if self.validate_character(input_char, expected_char):
correct_count += 1
else:
incorrect_count += 1
# 3. 计算准确率
accuracy = correct_count / total_chars if total_chars > 0 else 0.0
# 4. 返回验证结果字典
return {
"correct_count": correct_count,
"incorrect_count": incorrect_count,
"total_chars": total_chars,
"accuracy": accuracy
}
def calculate_accuracy(self, input_text: str, expected_text: str) -> float:
# 实现准确率计算逻辑
# 1. 比较输入文本与期望文本
# 2. 统计正确字符数
correct_count = 0
total_chars = max(len(input_text), len(expected_text))
if total_chars == 0:
return 1.0 # 两个空字符串认为是完全匹配
for i in range(total_chars):
input_char = input_text[i] if i < len(input_text) else ""
expected_char = expected_text[i] if i < len(expected_text) else ""
if self.validate_character(input_char, expected_char):
correct_count += 1
# 3. 计算准确率百分比
accuracy = correct_count / total_chars
# 4. 返回准确率
return accuracy