-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
62 lines (50 loc) · 2.26 KB
/
Copy pathcli.py
File metadata and controls
62 lines (50 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import argparse
import requests
import base64
def analyze_text(text, output_path, topics, style):
url = "http://localhost:8000/analyze"
payload = {
"text": text,
"topics": topics,
"style": style
}
try:
response = requests.post(url, json=payload)
response.raise_for_status()
data = response.json()
print("Topics:")
for topic, words in data["topics"].items():
print(f"- {topic}: {', '.join(words)}")
if output_path:
img_data = base64.b64decode(data["wordcloud"])
with open(output_path, "wb") as f:
f.write(img_data)
print(f"Word cloud saved to {output_path}")
except requests.exceptions.RequestException as e:
print(f"Error communicating with the API: {e}")
def main():
parser = argparse.ArgumentParser(description="Semantic Text Analyzer CLI")
subparsers = parser.add_subparsers(dest="command", required=True)
analyze_parser = subparsers.add_parser("analyze", help="Analyze text to extract topics and generate a word cloud.")
analyze_parser.add_argument("--text", type=str, help="Text to analyze.")
analyze_parser.add_argument("--file", type=str, help="Path to a text file to analyze.")
analyze_parser.add_argument("--output", type=str, default="wordcloud.png", help="Output file for the word cloud image.")
analyze_parser.add_argument("--topics", type=int, default=5, help="Number of topics to extract.")
analyze_parser.add_argument("--style", type=str, default="light", choices=["light", "dark"], help="Word cloud style.")
args = parser.parse_args()
if args.command == "analyze":
if args.text:
text_content = args.text
elif args.file:
try:
with open(args.file, "r", encoding="utf-8") as f:
text_content = f.read()
except FileNotFoundError:
print(f"Error: File not found at {args.file}")
return
else:
print("Error: Either --text or --file must be provided.")
return
analyze_text(text_content, args.output, args.topics, args.style)
if __name__ == "__main__":
main()