-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_embedding.py
More file actions
80 lines (61 loc) · 2.33 KB
/
Copy pathconvert_embedding.py
File metadata and controls
80 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pyemblib
import sys
import os
'''
convert_embedding.py
Quick and dirty script to convert embeddings from text to binary or
vice-versa.
'''
def parse_args():
emb_path = sys.argv[1]
dest_path = sys.argv[2]
mode = sys.argv[3] # 'txt' or 'bin'.
return [emb_path,dest_path,mode]
#========1=========2=========3=========4=========5=========6=========7==
def check_valid_file(some_file):
if not os.path.isfile(some_file):
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("")
print("DIES IST KEIN GÜLTIGER SPEICHERORT FÜR DATEIEN!!!!")
print("")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
exit()
#========1=========2=========3=========4=========5=========6=========7==
def read_embedding(emb_path):
print("READING. ")
file_name_length = len(emb_path)
last_char = emb_path[file_name_length - 1]
# Decide if it's a binary or text embedding file, and read in
# the embedding as a dict object, where the keys are the tokens
# (strings), and the values are the components of the corresponding
# vectors (floats).
embedding = {}
if (last_char == 'n'):
embedding = pyemblib.read(emb_path, mode=pyemblib.Mode.Binary)
elif (last_char == 't'):
embedding = pyemblib.read(emb_path, mode=pyemblib.Mode.Text)
else:
print("Unsupported embedding format. ")
exit()
return embedding
#========1=========2=========3=========4=========5=========6=========7==
def main(emb_path, dest_path, mode):
embedding = read_embedding(emb_path)
if mode == "txt":
pyemblib.write(embedding, dest_path, mode=pyemblib.Mode.Text)
elif mode == "bin":
pyemblib.write(embedding, dest_path, mode=pyemblib.Mode.Binary)
else:
print("Mode (third argument) must be \"txt\" or \"bin\".")
#========1=========2=========3=========4=========5=========6=========7==
if __name__ == "__main__":
# stuff only to run when not called via 'import' here
args = parse_args()
emb_path = args[0]
dest_path = args[1]
mode = args[2]
main(emb_path, dest_path, mode)