From 960d669e12c41eb404ca0bc39836c0bf5bde0943 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 19 May 2026 08:52:00 +0000 Subject: [PATCH] Add CLI text input support and enhancement list in README Agent-Logs-Url: https://github.com/cloudengine-labs/wordcloud/sessions/d05ecff4-949e-423d-8536-1a96a53ab8f9 Co-authored-by: chefgs <7605658+chefgs@users.noreply.github.com> --- README.md | 29 +++++++++++++-- show_wordcloud.py | 95 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 90 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index c8269f7..8eb17cc 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,36 @@ This python program can be used to generate a "WordCloud" image from the given ` - [x] Application to generate WordCloud - [x] Creating container image for the app - [ ] TODO: Upload the file wordcloud.png to GitHub or Cloud Storage -- [ ] TODO: Pass text as input argument to the script +- [x] Pass text as input argument to the script + +## Possible Enhancements +- Upload generated images directly to cloud storage (GCS/S3/Azure Blob) via optional flags. +- Add support for custom font, color map, and stopwords through CLI options. +- Add CI checks for linting + script execution on pull requests. +- Add automated tests for argument parsing and file output generation. +- Add API mode (Flask/FastAPI) to generate word clouds via HTTP requests. ## Executing the Script ### Method 1 -- Add the input text in the variable `sample_text` -- Run below command to execute the program, +- Run below command to execute the program with default sample text: ``` python3 -m pip install -r requirements.txt -python3 show_wordcloud.py +python3 show_wordcloud.py +``` + +- Pass inline text: +``` +python3 show_wordcloud.py --text "GitHub Actions GitHub Packages Protected branches" +``` + +- Pass text from a file: +``` +python3 show_wordcloud.py --text-file /path/to/input.txt --output /path/to/wordcloud.png +``` + +- You can also set a custom output file: +``` +python3 show_wordcloud.py --text "example text" --output mycloud.png ``` ### Method 2 diff --git a/show_wordcloud.py b/show_wordcloud.py index 98690b2..a0f1eb0 100644 --- a/show_wordcloud.py +++ b/show_wordcloud.py @@ -1,33 +1,68 @@ -""" -This program generates a WordCloud image for the given sample_text -""" -# Install required libraries -# pip install wordcloud matplotlib numpy +"""Generate a WordCloud image from text input.""" + +from argparse import ArgumentParser +from pathlib import Path + import matplotlib.pyplot as plt from wordcloud import WordCloud -# pylint: disable=C0103 -sample_text = "GitHub Actions for CI/CD \ -GitHub Packages for container hosting \ -Protected branches on all repos \ -Access to Code spaces\ -Multiple reviewers in pull requests \ -Required status checks \ -Code owners \ -Reviewers \ -Pages for static website hosting \ -Web-based support" - -# Create a WordCloud object -wordcloud_str = WordCloud(width=800, height=400, background_color='white').generate(sample_text) - -# Display the generated word cloud -# plt.switch_backend('TkAgg') -plt.figure(figsize=(10, 5)) -plt.imshow(wordcloud_str, interpolation='bilinear') -plt.axis('off') -# Show the wordcloud as an image in Matlab simulator -# plt.show() - -# Save the wordcloud as a png image file -plt.savefig('./wordcloud.png') +DEFAULT_TEXT = ( + "GitHub Actions for CI/CD " + "GitHub Packages for container hosting " + "Protected branches on all repos " + "Access to Code spaces " + "Multiple reviewers in pull requests " + "Required status checks " + "Code owners " + "Reviewers " + "Pages for static website hosting " + "Web-based support" +) + + +def parse_args(): + """Parse command-line arguments.""" + parser = ArgumentParser(description="Generate a word cloud PNG from text input.") + parser.add_argument("--text", help="Inline text input used to generate the word cloud.") + parser.add_argument( + "--text-file", + help="Path to a UTF-8 text file used to generate the word cloud.", + ) + parser.add_argument( + "--output", + default="wordcloud.png", + help="Path to output PNG file (default: wordcloud.png).", + ) + return parser.parse_args() + + +def resolve_text(args): + """Resolve text from CLI flags or fall back to default text.""" + if args.text and args.text_file: + raise ValueError("Use either --text or --text-file, not both.") + if args.text: + return args.text + if args.text_file: + return Path(args.text_file).read_text(encoding="utf-8") + return DEFAULT_TEXT + + +def generate_wordcloud(text, output_path): + """Generate and save a word cloud PNG.""" + wordcloud_str = WordCloud(width=800, height=400, background_color="white").generate(text) + plt.figure(figsize=(10, 5)) + plt.imshow(wordcloud_str, interpolation="bilinear") + plt.axis("off") + plt.savefig(output_path) + plt.close() + + +def main(): + """Run the script.""" + args = parse_args() + text = resolve_text(args) + generate_wordcloud(text, args.output) + + +if __name__ == "__main__": + main()