create a script that goes through each line in a file and replaces each word with a randomly generated phrase in python

Here's an example script that uses Python's random module to generate a random phrase for each word in a file:

main.py
import random

# define a dictionary of phrases
phrases = {
    "apple": ["crimson fruit", "juicy delight", "forbidden pleasure"],
    "banana": ["yellow boomerang", "sweet curve", "slippery friend"],
    "cherry": ["tiny red bomb", "sweet explosion", "pure indulgence"]
    # add more words and their phrases here
}

# open the file for reading and writing
with open("input.txt", "r") as input_file, open("output.txt", "w") as output_file:
    # loop through each line in the file
    for line in input_file:
        # loop through each word in the line
        for word in line.split():
            # check if the word is in the phrases dictionary
            if word in phrases:
                # choose a random phrase for the word
                phrase = random.choice(phrases[word])
                # write the phrase to the output file
                output_file.write(phrase + " ")
            else:
                # if the word is not in the phrases dictionary, write it as is to the output file
                output_file.write(word + " ")
        # write a newline character to the output file after each line
        output_file.write("\n")
1157 chars
28 lines

This script reads a file named input.txt and replaces each word in the file that matches a key in the phrases dictionary with a randomly chosen phrase from the corresponding list of phrases. The output is written to a new file named output.txt. You can modify the phrases dictionary to include additional words and phrases.

gistlibby LogSnag