Spaces:
Sleeping
Sleeping
| def load_vocab(): | |
| human_vocab = {' ': 0, '.': 1, '/': 2, '0': 3, '1': 4, '2': 5, '3': 6, '4': 7, '5': 8, '6': 9, '7': 10, '8': 11, '9': 12, 'a': 13, 'b': 14, 'c': 15, 'd': 16, 'e': 17, 'f': 18, 'g': 19, 'h': 20, 'i': 21, 'j': 22, 'l': 23, 'm': 24, 'n': 25, 'o': 26, 'p': 27, 'r': 28, 's': 29, 't': 30, 'u': 31, 'v': 32, 'w': 33, 'y': 34, '<unk>': 35, '<pad>': 36} | |
| machine_vocab = {'-': 0, '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6, '6': 7, '7': 8, '8': 9, '9': 10} | |
| inv_machine_vocab = {0: '-', 1: '0', 2: '1', 3: '2', 4: '3', 5: '4', 6: '5', 7: '6', 8: '7', 9: '8', 10: '9'} | |
| return human_vocab, machine_vocab, inv_machine_vocab | |
| def string_to_int(string, length, vocab): | |
| """ | |
| Converts all strings in the vocabulary into a list of integers representing the positions of the | |
| input string's characters in the "vocab" | |
| Arguments: | |
| string -- input string, e.g. 'Wed 10 Jul 2007' | |
| length -- the number of time steps you'd like, determines if the output will be padded or cut | |
| vocab -- vocabulary, dictionary used to index every character of your "string" | |
| Returns: | |
| rep -- list of integers (or '<unk>') (size = length) representing the position of the string's character in the vocabulary | |
| """ | |
| #make lower to standardize | |
| string = string.lower() | |
| string = string.replace(',','') | |
| if len(string) > length: | |
| string = string[:length] | |
| rep = list(map(lambda x: vocab.get(x, '<unk>'), string)) | |
| if len(string) < length: | |
| rep += [vocab['<pad>']] * (length - len(string)) | |
| return rep |