gpt4 book ai didi

algorithm - 每次按键后进行文本比较的工具/算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:15:59 25 4
gpt4 key购买 nike

我正在努力寻找一种文本比较工具或算法,可以将预期文本与正在键入的文本的当前状态进行比较。我会让一个实验者打出他眼前的一段文字。我的想法是在键入内容时将文本的当前状态与预期文本进行比较。这样我想找出主题何时以及在什么地方做错了(我还想找到不在结果文本中但在中间文本中有一段时间的错误)。有人能给我指明方向吗?

更新#1我可以访问 csv 格式的输入数据:这是我输入“foOBar”的示例输出数据。每行都有形式(时间戳、按键、按下/释放)

17293398.576653,F,P
17293398.6885,F,R
17293399.135282,LeftShift,P
17293399.626881,LeftShift,R
17293401.313254,O,P
17293401.391732,O,R
17293401.827314,LeftShift,P
17293402.073046,O,P
17293402.184859,O,R
17293403.178612,B,P
17293403.301748,B,R
17293403.458137,LeftShift,R
17293404.966193,A,P
17293405.077869,A,R
17293405.725405,R,P
17293405.815159,R,R

最佳答案

在 Python 中

给定您的输入 csv 文件(我将其命名为 keyboard_records.csv)

17293398.576653,F,P
17293398.6885,F,R
17293399.135282,LeftShift,P
17293399.626881,LeftShift,R
17293401.313254,O,P
17293401.391732,O,R
17293401.827314,LeftShift,P
17293402.073046,O,P
17293402.184859,O,R
17293403.178612,B,P
17293403.301748,B,R
17293403.458137,LeftShift,R
17293404.966193,A,P
17293405.077869,A,R
17293405.725405,R,P
17293405.815159,R,R

以下代码执行以下操作:

  1. 读取其内容并将其存储在名为steps 的列表中
  2. 对于 steps 中的每个 step 识别发生了什么并且
    • 如果是 shift 按下或释放相应地设置一个标志 (shift_on)
    • 如果按下的是箭头,则移动 cursor(我们插入字符的 current 的索引)——如果光标在开始或结束处它不应该移动的字符串,这就是那些 min()max()
    • 的原因
    • 如果是字母/数字/符号,则将其添加到 curret 中的 cursor 位置并递增 cursor

给你了

import csv

steps = [] # list of all actions performed by user
expected = "Hello"

with open("keyboard.csv") as csvfile:
for row in csv.reader(csvfile, delimiter=','):
steps.append((float(row[0]), row[1], row[2]))

# Now we parse the information
current = [] # text written by the user
shift_on = False # is shift pressed
cursor = 0 # where is the cursor in the current text
for step in steps:
time, key, action = step
if key == 'LeftShift':
if action == 'P':
shift_on = True
else:
shift_on = False
continue
if key == 'LeftArrow' and action == 'P':
cursor = max(0, cursor-1)
continue
if key == 'RightArrow' and action == 'P':
cursor = min(len(current), cursor+1)
continue
if action == 'P':
if shift_on is True:
current.insert(cursor, key.upper())
else:
current.insert(cursor, key.lower())
cursor += 1
# Now you can join current into a string
# and compare current with expected
print(''.join(current)) # printing current (just to see what's happening)
else:
# What to do when a key is released?
# Depends on your needs...
continue

比较currentexpected看看here .

注意:通过尝试上面的代码和更多的标志,您可以让它识别符号。这将取决于您的键盘。在我的 Shift + 6 = & 中,AltGr + E = €Ctrl + Shift + AltGr + è = {。我认为这是一个很好的起点。


更新

比较 2 个文本不是一项艰巨的任务,您可以 find tons网络上的页面 about it .不管怎样,我想向您展示一个面向对象的方法来解决这个问题,所以我添加了我之前在第一个解决方案中省略的 compare 部分。

这仍然是一个粗略的代码,没有对输入进行主要控制。但是,正如您所问,这为您指明了方向

class UserText:
# Initialize UserText:
# - empty text
# - cursor at beginning
# - shift off
def __init__(self, expected):
self.expected = expected
self.letters = []
self.cursor = 0
self.shift = False
# compares a and b and returns a
# list containing the indices of
# mismatches between a and b
def compare(a, b):
err = []
for i in range(min(len(a), len(b))):
if a[i] != b[i]:
err.append(i)
return err
# Parse a command given in the
# form (time, key, action)
def parse(self, command):
time, key, action = command
output = ""
if action == 'P':
if key == 'LeftShift':
self.shift = True
elif key == 'LeftArrow':
self.cursor = max(0, self.cursor - 1)
elif key == 'RightArrow':
self.cursor = min(len(self.letters), self.cursor + 1)
else:
# Else, a letter/number was pressed. Let's
# add it to self.letters in cursor position
if self.shift is True:
self.letters.insert(self.cursor, key.upper())
else:
self.letters.insert(self.cursor, key.lower())
self.cursor += 1
########## COMPARE WITH EXPECTED ##########
output += "Expected: \t" + self.expected + "\n"
output += "Current: \t" + str(self) + "\n"
errors = UserText.compare(str(self), self.expected[:len(str(self))])
output += "\t\t"
i = 0
for e in errors:
while i != e:
output += " "
i += 1
output += "^"
i += 1
output += "\n[{} errors at time {}]".format(len(errors), time)
return output


else:
if key == 'LeftShift':
self.shift = False
return output

def __str__(self):
return "".join(self.letters)


import csv
steps = [] # list of all actions performed by user
expected = "foobar"

with open("keyboard.csv") as csvfile:
for row in csv.reader(csvfile, delimiter=','):
steps.append((float(row[0]), row[1], row[2]))

# Now we parse the information
ut = UserText(expected)
for step in steps:
print(ut.parse(step))

上面 csv 文件的输出是:

Expected:       foobar
Current: f

[0 errors at time 17293398.576653]

Expected: foobar
Current: fo

[0 errors at time 17293401.313254]


Expected: foobar
Current: foO
^
[1 errors at time 17293402.073046]

Expected: foobar
Current: foOB
^^
[2 errors at time 17293403.178612]


Expected: foobar
Current: foOBa
^^
[2 errors at time 17293404.966193]

Expected: foobar
Current: foOBar
^^
[2 errors at time 17293405.725405]

关于algorithm - 每次按键后进行文本比较的工具/算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46340418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com