import requests
import json
from typing import Dict, List
API_URL = "http://localhost:2000/analyze"
def analyze_text(text: str) -> Dict:
response = requests.post(
API_URL,
json={"text": text},
headers={"Content-Type": "application/json"}
)
response.raise_for_status()
return response.json()
def print_analysis(result: Dict):
summary = result['summary']
issues = result['issues']
print("\n" + "="*60)
print("📊 ANALYSIS SUMMARY")
print("="*60)
print(f"Total Issues: {summary['total_issues']}")
print(f"Word Count: {summary['word_count']}")
print(f"Sentence Count: {summary['sentence_count']}")
print(f"Style Score: {summary['style_score']}/100")
if issues:
print("\n" + "="*60)
print("🔍 ISSUES FOUND")
print("="*60)
by_type = {}
for issue in issues:
issue_type = issue['type']
if issue_type not in by_type:
by_type[issue_type] = []
by_type[issue_type].append(issue)
for issue_type, issue_list in sorted(by_type.items()):
print(f"\n📌 {issue_type} ({len(issue_list)} found)")
print("-" * 60)
for issue in issue_list[:5]: print(f" Position {issue['start']}-{issue['end']}: \"{issue['string']}\"")
for suggestion in issue['suggestions']['recommendation']:
print(f" 💡 {suggestion}")
if len(issue_list) > 5:
print(f" ... and {len(issue_list) - 5} more")
def highlight_in_html(text: str, issues: List[Dict]) -> str:
sorted_issues = sorted(issues, key=lambda x: x['start'], reverse=True)
highlighted = text
colors = {
'PassiveVoice': '#ffcccc',
'Cliche': '#ffffcc',
'VagueWord': '#ffddaa',
'BusinessJargon': '#ddccff',
'OverusedWord': '#ffeecc',
'Repetition': '#ffccff',
'Grammar': '#ffaaaa',
}
for issue in sorted_issues:
start = issue['start']
end = issue['end']
issue_type = issue['type'].split('_')[0] color = colors.get(issue_type, '#dddddd')
before = highlighted[:start]
text_span = highlighted[start:end]
after = highlighted[end:]
suggestions = ' | '.join(issue['suggestions']['recommendation'])
highlighted = (
f"{before}"
f"<span style='background-color: {color}; cursor: help;' "
f"title='{issue_type}: {suggestions}'>"
f"{text_span}"
f"</span>"
f"{after}"
)
return f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Text Analysis</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }}
.text {{ background: white; padding: 20px; border: 1px solid #ddd; }}
span {{ border-radius: 2px; padding: 2px; }}
</style>
</head>
<body>
<h1>📝 Text Analysis Results</h1>
<div class="text">{highlighted}</div>
</body>
</html>
"""
if __name__ == "__main__":
test_text = """
The comprehensive report was written by the research team last week.
The results were very interesting and very significant. At the end of
the day, we need to think outside the box and leverage our synergies.
This approach will help us move the needle on our key performance indicators.
The analysis is very important. The analysis was very thorough.
"""
print("🔍 Analyzing text...")
result = analyze_text(test_text)
print_analysis(result)
html = highlight_in_html(test_text, result['issues'])
with open('analysis_result.html', 'w') as f:
f.write(html)
print("\n✅ HTML report saved to analysis_result.html")
with open('analysis_result.json', 'w') as f:
json.dump(result, f, indent=2)
print("✅ JSON report saved to analysis_result.json")