Skip to content

Commit eb1798a

Browse files
committed
💃🏾 rewrote start token func with dict comp
1 parent a64e1b1 commit eb1798a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

markov.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ def nth_order_markov(order, text_list):
6565
def start_token(dictionary):
6666
""" Get words that can start a sentence; this method is O(n) worst case
6767
because one must check every word in the corpus"""
68-
start_tokens = []
69-
for key in dictionary:
70-
if key[0].islower() is False and key[0].endswith('.') is False:
71-
start_tokens.append(key)
68+
# rewritten with dict comprehension
69+
start_tokens = [key for key in dictionary if key[0].islower() is False and key[0].endswith('.') is False]
70+
71+
# for key in dictionary:
72+
# if key[0].islower() is False and key[0].endswith('.') is False:
73+
# start_tokens.append(key)
74+
7275
token = random.choice(start_tokens)
7376
return token
7477

@@ -127,7 +130,6 @@ def main(text_list):
127130
end_words = stop_token(dictionary)
128131
markov_list = create_sentence(first_word, end_words, dictionary)
129132
markov_sentence = " ".join(markov_list)
130-
# print(markov_sentence)
131133
return markov_sentence
132134

133135
if __name__ == '__main__':

0 commit comments

Comments
 (0)