-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
52 lines (38 loc) · 1.66 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
from transformers import BertTokenizer
from sklearn.preprocessing import LabelEncoder
import numpy as np
# Load BERT tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# Load JSON data
with open('commands.json', 'r') as f:
data = json.load(f)
# Extract voice commands and AutoHotkey commands
voice_commands = [example['voice_command'] for example in data]
autohotkey_commands = [example['autohotkey_command'] for example in data]
# Tokenize voice commands
tokenized_voice_commands = [tokenizer.tokenize(command) for command in voice_commands]
# Initialize LabelEncoder
label_encoder = LabelEncoder()
# Encode AutoHotkey commands into numerical labels
encoded_labels = label_encoder.fit_transform(autohotkey_commands)
# Convert encoded labels to a Python list
encoded_labels = encoded_labels.tolist()
# Create a dictionary to map numerical labels back to AutoHotkey commands
label_to_command_map = {label: command for label, command in zip(encoded_labels, autohotkey_commands)}
# Optionally, save the processed data
processed_data = {
'tokenized_voice_commands': tokenized_voice_commands,
'encoded_labels': encoded_labels,
'label_to_command_map': label_to_command_map
}
with open('processed_data.json', 'w') as f:
json.dump(processed_data, f)
print("Processing complete. Saved processed data to 'processed_data.json'.")
import json
# Load processed data from JSON file
with open('processed_data.json', 'r') as f:
processed_data = json.load(f)
tokenized_voice_commands = processed_data['tokenized_voice_commands']
encoded_labels = processed_data['encoded_labels']
label_to_command_map = processed_data['label_to_command_map']