I am trying out text2bin.py file from the below link:
https://github.com/tensorflow/models/blob/master/swivel/text2bin.py
I had to modify some codes since it was written in python 2.7, but I need 3+. Anyway, I was trying out the code and realized that the sys.argv[1:] is currently empty.
import getopt
import os
import struct
import sys
try:
opts, args = getopt.getopt(sys.argv[1:], 'o:v:', ['output=', 'vocab='])
except getopt.GetoptError as e:
print (e, file = sys.stderr)
sys.exit(2)
Below is my code for the whole model.
import getopt
import os
import struct
import sys
try:
opts, args = getopt.getopt(sys.argv[1:], 'o:v:', ['output=', 'vocab='])
except getopt.GetoptError as e:
print (e, file = sys.stderr)
sys.exit(2)
opt_output = 'vecs.bin'
opt_vocab = 'vocab.txt'
for o, a in opts:
if o in ('-o', '--output'):
opt_output = a
if o in ('-v', '--vocab'):
opt_vocab = a
def go(fhs):
fmt = None
with open(opt_vocab, 'w') as vocab_out:
with open(opt_output, 'wb') as vecs_out:
for lines in list(zip(fhs)):
parts = [str(line).split() for line in lines]
token = parts[0][0]
if any(part[0] != token for part in parts[1:]):
raise IOError('vector files must be aligned')
print(token, file = vocab_out)
vec = [sum(float(x) for x in xs) for xs in list(zip((parts)[1:]))]
if not fmt:
fmt = struct.Struct('%df' % len(vec))
vecs_out.write(fmt.pack())
if args:
fhs = [open(filename) for filename in args]
go(fhs)
for fh in fhs:
fh.close()
else:
go([sys.stdin])
Why am I getting an empty sys.argv[1:]
? Also, is my conversion from python 2.7 to 3.5 correct??