| 1 | import mmap |
| 2 | |
| 3 | class Vocab: |
| 4 | def __init__(self, w2i): |
| 5 | self.w2i = dict(w2i) |
| 6 | self.i2w = {i:w for w,i in w2i.items()} |
| 7 | |
| 8 | @classmethod |
| 9 | def from_corpus(cls, corpus): |
| 10 | w2i = {} |
| 11 | for sent in corpus: |
| 12 | for word in sent: |
| 13 | w2i.setdefault(word, len(w2i)) |
| 14 | return Vocab(w2i) |
| 15 | |
| 16 | def size(self): |
| 17 | return len(self.w2i.keys()) |
| 18 | |
| 19 | #This corpus reader can be used when reading large text file into a memory can solve IO bottleneck of training. |
| 20 | #Use it exactly as the regular CorpusReader from the rnnlm.py |