Skip to content

Commit

Permalink
Remove empty lines from dict and user-provided input file
Browse files Browse the repository at this point in the history
  • Loading branch information
Belval authored Jun 14, 2019
1 parent 4096d3d commit 90c2e0d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion TextRecognitionDataGenerator/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def load_dict(lang):

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

def load_fonts(lang):
Expand Down
4 changes: 2 additions & 2 deletions TextRecognitionDataGenerator/string_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create_strings_from_file(filename, count):
strings = []

with open(filename, 'r', encoding="utf8") as f:
lines = [l.strip()[0:200] for l in f.readlines()]
lines = [l[0:200] for l in f.read().splitlines() if len(l) > 0]
if len(lines) == 0:
raise Exception("No lines could be read in file")
while len(strings) < count:
Expand All @@ -34,7 +34,7 @@ def create_strings_from_dict(length, allow_variable, count, lang_dict):
for _ in range(0, count):
current_string = ""
for _ in range(0, rnd.randint(1, length) if allow_variable else length):
current_string += lang_dict[rnd.randrange(dict_len)][:-1]
current_string += lang_dict[rnd.randrange(dict_len)]
current_string += ' '
strings.append(current_string[:-1])
return strings
Expand Down
2 changes: 1 addition & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_create_string_from_file(self):
)

def test_create_strings_from_dict(self):
strings = create_strings_from_dict(3, False, 2, ['TEST\n', 'TEST\n', 'TEST\n', 'TEST\n'])
strings = create_strings_from_dict(3, False, 2, ['TEST', 'TEST', 'TEST', 'TEST'])

self.assertTrue(
len(strings) == 2 and
Expand Down

0 comments on commit 90c2e0d

Please sign in to comment.