Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 65 additions & 30 deletions show_wordcloud.py
Original file line number Diff line number Diff line change
@@ -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()
Loading