-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMM_utils.py
More file actions
98 lines (76 loc) · 2.73 KB
/
HMM_utils.py
File metadata and controls
98 lines (76 loc) · 2.73 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import string
def map_alphabet_to_numbers():
"""
Maps each character in the alphabet to a corresponding number.
Returns:
dict: A dictionary where characters are keys and their corresponding numbers are values.
"""
alphabet = "abcdefghijklmnopqrstuvwxyz "
mapping = {char: i for i, char in enumerate(alphabet)}
return mapping
def string_to_numbers(text, mapping):
"""
Converts a string of characters to a list of numbers based on the provided mapping.
Args:
text (str): The input string to be converted.
mapping (dict): A dictionary mapping characters to numbers.
Returns:
list: A list of numbers representing the characters in the input string.
"""
numbers = [mapping[char] for char in text]
return numbers
def find_mapping(L):
"""
Creates a mapping between indices and characters based on the given list of numbers.
Args:
L (list): A list of numbers representing indices.
Returns:
dict: A dictionary mapping each character from the alphabet to its corresponding character in the given list.
"""
alphabet = string.ascii_lowercase + " "
mapping = {}
for i, num in enumerate(L):
mapping[alphabet[i]] = alphabet[num]
return mapping
def numbers_to_string(string, mapping):
"""
Converts a list of numbers back into a string using the provided mapping.
Args:
string (list): A list of numbers representing characters.
mapping (dict): A dictionary mapping characters to their corresponding characters.
Returns:
str: The transformed string.
"""
transformed_string = ""
for char in string:
if char in mapping:
transformed_string += mapping[char]
else:
transformed_string += char
return transformed_string
def invert_mapping(mapping):
"""
Inverts the given mapping, swapping keys with values.
Args:
mapping (dict): A dictionary mapping characters to their corresponding characters.
Returns:
dict: A dictionary with the values of the original mapping as keys and the keys as values.
"""
inverted_mapping = {value: key for key, value in mapping.items()}
return inverted_mapping
def convert_numbers_to_letters(numbers):
"""
Converts i in the (i+1)-th element in the alphabet, with " " being denoted by 26.
Input:
- numbers: a numpy array containing the numbers
Output:
- letters: a list containing the string that was represented by numbers.
"""
letters = []
for number in numbers:
if number == 26:
letters.append(" ")
else:
letter = chr(number + ord("a"))
letters.append(letter)
return "".join(letters)