read_binarized.py 1.44 KB
Newer Older
Alexei Baevski's avatar
Alexei Baevski committed
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python3
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree. An additional grant of patent rights
# can be found in the PATENTS file in the same directory.

import argparse

Myle Ott's avatar
Myle Ott committed
11
from fairseq.data import data_utils, Dictionary, indexed_dataset
Alexei Baevski's avatar
Alexei Baevski committed
12
13
14
15
16


def get_parser():
    parser = argparse.ArgumentParser(
        description='writes text from binarized file to stdout')
17
    # fmt: off
18
    parser.add_argument('--dataset-impl', help='dataset implementation',
19
                        choices=indexed_dataset.get_available_dataset_impl())
20
    parser.add_argument('--dict', metavar='FP', help='dictionary containing known words', default=None)
Alexei Baevski's avatar
Alexei Baevski committed
21
    parser.add_argument('--input', metavar='FP', required=True, help='binarized file to read')
22
    # fmt: on
Alexei Baevski's avatar
Alexei Baevski committed
23
24
25
26

    return parser


27
28
29
30
31
def main():
    parser = get_parser()
    args = parser.parse_args()

    dictionary = Dictionary.load(args.dict) if args.dict is not None else None
Myle Ott's avatar
Myle Ott committed
32
33
34
35
36
37
    dataset = data_utils.load_indexed_dataset(
        args.input,
        dictionary,
        dataset_impl=args.dataset_impl,
        default='lazy',
    )
38
39
40
41
42
43
44
45

    for tensor_line in dataset:
        if dictionary is None:
            line = ' '.join([str(int(x)) for x in tensor_line])
        else:
            line = dictionary.string(tensor_line)

        print(line)
Alexei Baevski's avatar
Alexei Baevski committed
46
47
48


if __name__ == '__main__':
49
    main()