Skip to content

Commit 9659a7b

Browse files
authored
Merge pull request #14 from gogasca/patch-2
Replace map and lambdas with list comprehensions
2 parents ec683e2 + 28cba85 commit 9659a7b

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ def clean_str(sentence):
2020

2121

2222
def get_text_list(data_path, toy):
23-
with open (data_path, "r", encoding='utf-8') as f:
23+
with open (data_path, "r", encoding="utf-8") as f:
2424
if not toy:
25-
return list(map(lambda x: clean_str(x.strip()), f.readlines()))
25+
return [clean_str(x.strip()) for x in f.readlines()]
2626
else:
27-
return list(map(lambda x: clean_str(x.strip()), f.readlines()))[:50000]
27+
return [clean_str(x.strip()) for x in f.readlines()][:50000]
2828

2929

3030
def build_dict(step, toy=False):
@@ -70,17 +70,17 @@ def build_dataset(step, word_dict, article_max_len, summary_max_len, toy=False):
7070
else:
7171
raise NotImplementedError
7272

73-
x = list(map(lambda d: word_tokenize(d), article_list))
74-
x = list(map(lambda d: list(map(lambda w: word_dict.get(w, word_dict["<unk>"]), d)), x))
75-
x = list(map(lambda d: d[:article_max_len], x))
76-
x = list(map(lambda d: d + (article_max_len - len(d)) * [word_dict["<padding>"]], x))
77-
73+
x = [word_tokenize(d) for d in article_list]
74+
x = [[word_dict.get(w, word_dict["<unk>"]) for w in d] for d in x]
75+
x = [d[:article_max_len] for d in x]
76+
x = [d + (article_max_len - len(d)) * [word_dict["<padding>"]] for d in x]
77+
7878
if step == "valid":
7979
return x
80-
else:
81-
y = list(map(lambda d: word_tokenize(d), title_list))
82-
y = list(map(lambda d: list(map(lambda w: word_dict.get(w, word_dict["<unk>"]), d)), y))
83-
y = list(map(lambda d: d[:(summary_max_len-1)], y))
80+
else:
81+
y = [word_tokenize(d) for d in title_list]
82+
y = [[word_dict.get(w, word_dict["<unk>"]) for w in d] for d in y]
83+
y = [d[:(summary_max_len - 1)] for d in y]
8484
return x, y
8585

8686

0 commit comments

Comments
 (0)