Skip to content

RNN word language model example #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions word_language_model/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
########################################
# Data Fetching Script for PTB
########################################

import torch
import os.path

class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = []

def addword(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1

return self.word2idx[word]

def ntokens(self):
return len(self.idx2word)


class Corpus(object):
def __init__(self, path):
self.dic = Dictionary()
self.train=self._loadfile(os.path.join(path, 'train.txt'))
self.valid=self._loadfile(os.path.join(path, 'valid.txt'))
self.test =self._loadfile(os.path.join(path, 'test.txt'))

# | Tokenize a text file.
def _loadfile(self, path):
# Read words from file.
assert(os.path.exists(path))
tokens = 0
with open(path, 'r') as f:
for line in f:
words = line.split() + ['<eos>']
for word in words:
self.dic.addword(word)
tokens += 1

with open(path, 'r') as f:
ids = torch.LongTensor(tokens)
token = 0
for line in f:
words = line.split() + ['<eos>']
for word in words:
ids[token] = self.dic.word2idx[word]
token += 1

# Final dataset.
return ids
Loading