Skip to content

Commit 724f77a

Browse files
authored
Merge pull request #104 from RajivChitale/inMemoryVocabulary
With pre-commit-checks
2 parents d6c4ab4 + 6d7986c commit 724f77a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/generate_vocabulary.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import argparse
77

88
parser = argparse.ArgumentParser()
9-
parser.add_argument('-o', '--output',type=str, help='Output file name')
9+
parser.add_argument("-o", "--output", type=str, help="Output file name")
1010
args = parser.parse_args()
1111

1212
if args.output is None:
13-
print('Error: Output file path not provided.')
13+
print("Error: Output file path not provided.")
1414
exit(1)
1515

1616
output_file = args.output
17-
vocab_file='../vocabulary/seedEmbeddingVocab.txt'
17+
vocab_file = "../vocabulary/seedEmbeddingVocab.txt"
1818

1919
# Define headers and opening/closing of map
2020
header = """\
@@ -44,7 +44,7 @@ class Vocabulary {
4444
4545
"""
4646

47-
opening = '\nconst std::map<std::string, IR2Vec::Vector> Vocabulary::vocabulary = {\n'
47+
opening = "\nconst std::map<std::string, IR2Vec::Vector> Vocabulary::vocabulary = {\n"
4848
closing = """\
4949
};
5050
} // namespace IR2Vec
@@ -53,24 +53,24 @@ class Vocabulary {
5353
"""
5454

5555
try:
56-
with open(output_file, 'w') as fw:
56+
with open(output_file, "w") as fw:
5757
fw.write(header)
58-
with open(vocab_file, 'r') as fr:
58+
with open(vocab_file, "r") as fr:
5959
# Write vector declarations to the output file
6060
for line in fr.readlines():
61-
key, val = line.strip().split(':')
62-
e = val.find(']')
63-
fw.write(f'const IR2Vec::Vector {key}_vector = {{ {val[1:e]} }};\n')
64-
61+
key, val = line.strip().split(":")
62+
e = val.find("]")
63+
fw.write(f"const IR2Vec::Vector {key}_vector = {{ {val[1:e]} }};\n")
64+
6565
fw.write(opening)
66-
with open(vocab_file, 'r') as fr:
66+
with open(vocab_file, "r") as fr:
6767
# Write map entries to the output file
6868
for line in fr.readlines():
69-
key, _ = line.strip().split(':')
69+
key, _ = line.strip().split(":")
7070
fw.write(f' {{ "{key}", {key}_vector }},\n')
7171
fw.write(closing)
72-
73-
print(f'Generated {output_file}')
72+
73+
print(f"Generated {output_file}")
7474

7575
except FileNotFoundError:
7676
print(f"Error: Vocabulary file '{vocab_file}' not found.")

0 commit comments

Comments
 (0)