How to create a human typing emulator in python. Mimic a person Types
- User
How to create a human typing emulator in python. Mimic a person Types
- Assistant
How to create a human typing emulator in python. Mimic a person Types
Creating a human typing emulator in Python involves simulating the typing behavior of a human, including the variability in typing speed, the possible introduction of random pauses, and potentially simulating errors and corrections. Here's a step-by-step guide on how to achieve this:
Install Required Libraries:
We'll use the pyautogui library to simulate keyboard input. Make sure to install it first. Optionally, you can use time and random libraries for controlling the typing speed and randomness.
pip install pyautogui
Write the Emulator:
Here is a basic implementation:
import pyautogui
import time
import random
def human_typing(text, min_wpm=60, max_wpm=100, error_rate=0.01):
"""
Simulate human typing of the given text.
Parameters:
text (str): Text to type.
min_wpm (int): Minimum words per minute.
max_wpm (int): Maximum words per minute.
error_rate (float): Likelihood to make a typo (0 to 1).
"""
# Calculate typing speed in seconds per character
min_cps = min_wpm * 5 / 60 # character per second (min speed)
max_cps = max_wpm * 5 / 60 # character per second (max speed)
for char in text:
# Introduce random delay to simulate typing delay per character
delay = random.uniform(1 / max_cps, 1 / min_cps)
time.sleep(delay)
# Simulate typos
if random.random() < error_rate:
# Choose a random incorrect character
typo = chr(random.randint(32, 126))
pyautogui.typewrite(typo)
time.sleep(0.1)
pyautogui.press('backspace')
# Type the actual character
pyautogui.typewrite(char)
# Example usage:
text_to_type = "This is a test sentence to simulate human typing behavior."
human_typing(text_to_type)
Explanation:
Typing Speed:
min_cps and max_cps define the range of typing speed.Random Delay:
1 / max_cps and 1 / min_cps to simulate the natural variability in typing speed.Typos Simulation:
Enhancements:
Extended Typing Patterns:
Longer Pauses:
Contextual Errors:
Here's a more advanced example with additional features like longer pauses and contextual errors:
import pyautogui
import time
import random
def human_typing(text, min_wpm=60, max_wpm=100, error_rate=0.01, long_pause_chance=0.05, long_pause_duration=1.5):
min_cps = min_wpm * 5 / 60
max_cps = max_wpm * 5 / 60
for char in text:
if char == ' ' and random.random() < long_pause_chance:
time.sleep(long_pause_duration)
delay = random.uniform(1 / max_cps, 1 / min_cps)
time.sleep(delay)
if random.random() < error_rate:
typo = chr(random.randint(32, 126))
pyautogui.typewrite(typo)
time.sleep(0.1)
pyautogui.press('backspace')
pyautogui.typewrite(char)
# Example usage:
text_to_type = "This is an advanced test sentence to simulate realistic human typing behavior."
human_typing(text_to_type)
Remember to run this script in an environment where pyautogui can control the keyboard, which may not include some IDEs or cloud-based notebooks. Additionally, always use such scripts responsibly, as controlling the keyboard can potentially disrupt workflows or cause unintended actions.
Not yet included in a published release.