import random def poetry(): filename = '2013_Wages_for_Facebook_[EN].txt' path = './manifestos/'+filename file = open(path, 'r') manifesto = file.read() manifesto = manifesto.replace('\n', ' ') manifesto = manifesto.lower() words = manifesto.split(' ') # Divide words into three lists shortwords = [] mediumwords = [] longwords = [] for word in words: if len(word) < 4: shortwords.append(word) elif 4 <= len(word) <= 7: mediumwords.append(word) else: longwords.append(word) # Pick one from each list of words line = (random.choice(shortwords), random.choice(mediumwords), random.choice(longwords)) # Join into a sentence result = " ".join(line) return result if __name__ == '__main__': result = poetry() print('result:', result)