Skip to content
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

Option to give an existing vocab file(.pt or text) to preprocess.py #1346

Merged
merged 8 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 12 additions & 1 deletion onmt/inputters/inputter.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def _build_fv_from_multifield(multifield, counters, build_fv_args,
def build_vocab(train_dataset_files, fields, data_type, share_vocab,
src_vocab_path, src_vocab_size, src_words_min_frequency,
tgt_vocab_path, tgt_vocab_size, tgt_words_min_frequency,
vocab_size_multiple=1):
use_existing_vocab, vocab_size_multiple=1):
vince62s marked this conversation as resolved.
Show resolved Hide resolved
"""Build the fields for all data sides.

Args:
Expand All @@ -336,6 +336,17 @@ def build_vocab(train_dataset_files, fields, data_type, share_vocab,

counters = defaultdict(Counter)

if use_existing_vocab:
vince62s marked this conversation as resolved.
Show resolved Hide resolved
try:
logger.info("Using existing vocabulary...")
vocab = torch.load(src_vocab_path)
# return vocab to dump with standard name
return vocab
except torch.serialization.pickle.UnpicklingError:
logger.info("Building vocab from text file...")
# empty train_dataset_files so that vocab is only loaded from
# given paths in src_vocab_path, tgt_vocab_path
train_dataset_files = []
# Load vocabulary
if src_vocab_path:
src_vocab, src_vocab_size = _load_vocab(
Expand Down
2 changes: 2 additions & 0 deletions onmt/opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def preprocess_opts(parser):
# Dictionary options, for text corpus

group = parser.add_argument_group('Vocab')
group.add('--use_existing_vocab', '-use_existing_vocab',
action='store_true', default="", help="Use existing vocabulary.")
group.add('--src_vocab', '-src_vocab', default="",
vince62s marked this conversation as resolved.
Show resolved Hide resolved
help="Path to an existing source vocabulary. Format: "
"one word per line.")
Expand Down
2 changes: 1 addition & 1 deletion preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def build_save_vocab(train_dataset, fields, opt):
train_dataset, fields, opt.data_type, opt.share_vocab,
opt.src_vocab, opt.src_vocab_size, opt.src_words_min_frequency,
opt.tgt_vocab, opt.tgt_vocab_size, opt.tgt_words_min_frequency,
vocab_size_multiple=opt.vocab_size_multiple
opt.use_existing_vocab, vocab_size_multiple=opt.vocab_size_multiple
)
vince62s marked this conversation as resolved.
Show resolved Hide resolved

vocab_path = opt.save_data + '.vocab.pt'
Expand Down