import argparse
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
def generate_wordcloud(input_path, output_path):
# Read text from file
with open(input_path, "r", encoding="utf-8") as f:
text = f.read()
# Define stopwords (common words to ignore)
stopwords = set(STOPWORDS)
# You can add your own stopwords if you like:
# stopwords.update(["example", "anotherword"])
# Create the word cloud object
wc = WordCloud(
width=800,
height=400,
background_color="white",
stopwords=stopwords
).generate(text)
# Display and save the word cloud
plt.figure(figsize=(10, 5))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.tight_layout(pad=0)
plt.savefig(output_path, bbox_inches="tight")
# Uncomment to display on screen instead of just saving:
# plt.show()
def main():
parser = argparse.ArgumentParser(
description="Generate a word cloud image from a text file."
)
parser.add_argument("input_file", help="Path to input text file")
parser.add_argument(
"-o", "--output",
default="wordcloud.png",
help="Path to output image (default: wordcloud.png)"
)
args = parser.parse_args()
generate_wordcloud(args.input_file, args.output)
if __name__ == "__main__":
main()