|
| 1 | +import torch |
| 2 | + |
| 3 | + |
| 4 | +class Dict(object): |
| 5 | + def __init__(self, data=None): |
| 6 | + self.idxToLabel = {} |
| 7 | + self.labelToIdx = {} |
| 8 | + self.frequencies = {} |
| 9 | + |
| 10 | + # Special entries will not be pruned. |
| 11 | + self.special = [] |
| 12 | + |
| 13 | + if data is not None: |
| 14 | + if type(data) == str: |
| 15 | + self.loadFile(data) |
| 16 | + else: |
| 17 | + self.addSpecials(data) |
| 18 | + |
| 19 | + def size(self): |
| 20 | + return len(self.idxToLabel) |
| 21 | + |
| 22 | + # Load entries from a file. |
| 23 | + def loadFile(self, filename): |
| 24 | + for line in open(filename): |
| 25 | + fields = line.split() |
| 26 | + label = fields[0] |
| 27 | + idx = int(fields[1]) |
| 28 | + self.add(label, idx) |
| 29 | + |
| 30 | + # Write entries to a file. |
| 31 | + def writeFile(self, filename): |
| 32 | + with open(filename, 'w') as file: |
| 33 | + for i in range(self.size()): |
| 34 | + label = self.idxToLabel[i] |
| 35 | + file.write('%s %d\n' % (label, i)) |
| 36 | + |
| 37 | + file.close() |
| 38 | + |
| 39 | + def lookup(self, key, default=None): |
| 40 | + try: |
| 41 | + return self.labelToIdx[key] |
| 42 | + except KeyError: |
| 43 | + return default |
| 44 | + |
| 45 | + def getLabel(self, idx, default=None): |
| 46 | + try: |
| 47 | + return self.idxToLabel[idx] |
| 48 | + except KeyError: |
| 49 | + return default |
| 50 | + |
| 51 | + # Mark this `label` and `idx` as special (i.e. will not be pruned). |
| 52 | + def addSpecial(self, label, idx=None): |
| 53 | + idx = self.add(label, idx) |
| 54 | + self.special += [idx] |
| 55 | + |
| 56 | + # Mark all labels in `labels` as specials (i.e. will not be pruned). |
| 57 | + def addSpecials(self, labels): |
| 58 | + for label in labels: |
| 59 | + self.addSpecial(label) |
| 60 | + |
| 61 | + # Add `label` in the dictionary. Use `idx` as its index if given. |
| 62 | + def add(self, label, idx=None): |
| 63 | + if idx is not None: |
| 64 | + self.idxToLabel[idx] = label |
| 65 | + self.labelToIdx[label] = idx |
| 66 | + else: |
| 67 | + if label in self.labelToIdx: |
| 68 | + idx = self.labelToIdx[label] |
| 69 | + else: |
| 70 | + idx = len(self.idxToLabel) |
| 71 | + self.idxToLabel[idx] = label |
| 72 | + self.labelToIdx[label] = idx |
| 73 | + |
| 74 | + if idx not in self.frequencies: |
| 75 | + self.frequencies[idx] = 1 |
| 76 | + else: |
| 77 | + self.frequencies[idx] += 1 |
| 78 | + |
| 79 | + return idx |
| 80 | + |
| 81 | + # Return a new dictionary with the `size` most frequent entries. |
| 82 | + def prune(self, size): |
| 83 | + if size >= self.size(): |
| 84 | + return self |
| 85 | + |
| 86 | + # Only keep the `size` most frequent entries. |
| 87 | + freq = torch.Tensor( |
| 88 | + [self.frequencies[i] for i in range(len(self.frequencies))]) |
| 89 | + _, idx = torch.sort(freq, 0, True) |
| 90 | + |
| 91 | + newDict = Dict() |
| 92 | + |
| 93 | + # Add special entries in all cases. |
| 94 | + for i in self.special: |
| 95 | + newDict.addSpecial(self.idxToLabel[i]) |
| 96 | + |
| 97 | + for i in idx[:size]: |
| 98 | + newDict.add(self.idxToLabel[i]) |
| 99 | + |
| 100 | + return newDict |
| 101 | + |
| 102 | + # Convert `labels` to indices. Use `unkWord` if not found. |
| 103 | + # Optionally insert `bosWord` at the beginning and `eosWord` at the . |
| 104 | + def convertToIdx(self, labels, unkWord, bosWord=None, eosWord=None): |
| 105 | + vec = [] |
| 106 | + |
| 107 | + if bosWord is not None: |
| 108 | + vec += [self.lookup(bosWord)] |
| 109 | + |
| 110 | + unk = self.lookup(unkWord) |
| 111 | + vec += [self.lookup(label, default=unk) for label in labels] |
| 112 | + |
| 113 | + if eosWord is not None: |
| 114 | + vec += [self.lookup(eosWord)] |
| 115 | + |
| 116 | + return torch.LongTensor(vec) |
| 117 | + |
| 118 | + # Convert `idx` to labels. If index `stop` is reached, convert it and return. |
| 119 | + def convertToLabels(self, idx, stop): |
| 120 | + labels = [] |
| 121 | + |
| 122 | + for i in idx: |
| 123 | + labels += [self.getLabel(i)] |
| 124 | + if i == stop: |
| 125 | + break |
| 126 | + |
| 127 | + return labels |
0 commit comments