Skip to content

Commit 90c2e0d

Browse files
authored
Remove empty lines from dict and user-provided input file
1 parent 4096d3d commit 90c2e0d

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

TextRecognitionDataGenerator/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def load_dict(lang):
271271

272272
lang_dict = []
273273
with open(os.path.join('dicts', lang + '.txt'), 'r', encoding="utf8", errors='ignore') as d:
274-
lang_dict = d.readlines()
274+
lang_dict = [l for l in d.read().splitlines() if len(l) > 0]
275275
return lang_dict
276276

277277
def load_fonts(lang):

TextRecognitionDataGenerator/string_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def create_strings_from_file(filename, count):
1313
strings = []
1414

1515
with open(filename, 'r', encoding="utf8") as f:
16-
lines = [l.strip()[0:200] for l in f.readlines()]
16+
lines = [l[0:200] for l in f.read().splitlines() if len(l) > 0]
1717
if len(lines) == 0:
1818
raise Exception("No lines could be read in file")
1919
while len(strings) < count:
@@ -34,7 +34,7 @@ def create_strings_from_dict(length, allow_variable, count, lang_dict):
3434
for _ in range(0, count):
3535
current_string = ""
3636
for _ in range(0, rnd.randint(1, length) if allow_variable else length):
37-
current_string += lang_dict[rnd.randrange(dict_len)][:-1]
37+
current_string += lang_dict[rnd.randrange(dict_len)]
3838
current_string += ' '
3939
strings.append(current_string[:-1])
4040
return strings

tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_create_string_from_file(self):
5858
)
5959

6060
def test_create_strings_from_dict(self):
61-
strings = create_strings_from_dict(3, False, 2, ['TEST\n', 'TEST\n', 'TEST\n', 'TEST\n'])
61+
strings = create_strings_from_dict(3, False, 2, ['TEST', 'TEST', 'TEST', 'TEST'])
6262

6363
self.assertTrue(
6464
len(strings) == 2 and

0 commit comments

Comments
 (0)