|
|
# 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. 根据按键类型处理(字符、功能键等)
|
|
|
# 处理退格键
|
|
|
if key == '\b' or key == '\x7f': # 退格键 (\b) 或删除键 (\x7f)
|
|
|
if self.input_buffer: # 只有当输入缓冲区有内容时才处理
|
|
|
# 删除最后一个字符
|
|
|
deleted_char = self.input_buffer[-1]
|
|
|
self.input_buffer = self.input_buffer[:-1]
|
|
|
self.current_position = max(0, self.current_position - 1)
|
|
|
|
|
|
# 发送退格信号,包含被删除的字符
|
|
|
self.text_changed.emit('\b')
|
|
|
|
|
|
# 发送按键信号用于记录
|
|
|
self.key_pressed.emit(f"backspace_deleted:{deleted_char}")
|
|
|
|
|
|
return True
|
|
|
else:
|
|
|
return False # 缓冲区为空,不处理退格
|
|
|
|
|
|
# 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 process_text_commit(self, text: str) -> bool:
|
|
|
"""处理输入法提交的完整文本(支持中文输入)"""
|
|
|
if not text:
|
|
|
return False
|
|
|
|
|
|
# 逐个处理提交的字符
|
|
|
for char in text:
|
|
|
self.input_buffer += char
|
|
|
self.current_position += 1
|
|
|
# 发送每个字符的text_changed信号
|
|
|
self.text_changed.emit(char)
|
|
|
|
|
|
# 发送完整的文本提交信号
|
|
|
self.key_pressed.emit(f"text_commit:{text}")
|
|
|
|
|
|
# 检查是否完成输入
|
|
|
if self.expected_text and self.input_buffer == self.expected_text:
|
|
|
self.input_completed.emit()
|
|
|
|
|
|
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 |