Sasmitah commited on
Commit
8cca6aa
·
verified ·
1 Parent(s): 2cb2972

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +6 -28
utils.py CHANGED
@@ -318,7 +318,8 @@ def fetch_and_save_news(company_name):
318
  if not company_name:
319
  print("❌ Error: Company name is required")
320
  return None
321
- file_name = f"{company_name}_news.txt"
 
322
  articles = []
323
  article_count = 0
324
  article_limit = 10
@@ -374,36 +375,13 @@ def fetch_and_save_news(company_name):
374
  },
375
  "Final Sentiment Analysis": analysis_result['Final Sentiment Analysis']
376
  }
 
377
  with open(file_name, "w", encoding="utf-8") as file:
378
- file.write(f'"Company": "{output_data["Company"]}",\n')
379
- file.write('"Articles": [\n')
380
- for i, article in enumerate(output_data["Articles"]):
381
- file.write('{\n')
382
- file.write(f'"Title": "{article["Title"]}",\n')
383
- file.write(f'"Summary": "{article["Summary"]}",\n')
384
- file.write(f'"Sentiment": "{article["Sentiment"]}",\n')
385
- file.write(f'"Topics": {article["Topics"]}\n')
386
- file.write('}' + (',\n' if i < len(output_data["Articles"]) - 1 else '\n'))
387
- file.write('],\n')
388
- file.write('"Comparative Sentiment Score": {\n')
389
- file.write('"Sentiment Distribution": {\n')
390
- for i, (sentiment, count) in enumerate(output_data["Comparative Sentiment Score"]["Sentiment Distribution"].items()):
391
- file.write(f'"{sentiment}": {count}' + (',' if i < 2 else '') + '\n')
392
- file.write('}\n')
393
- file.write('},\n')
394
- file.write(f'{output_data["Coverage Differences"]},\n')
395
- file.write('"Topic Overlap": {\n')
396
- file.write(f'"Common Topics": {output_data["Topic Overlap"]["Common Topics"]},\n')
397
- for i, (key, value) in enumerate([(k, v) for k, v in output_data["Topic Overlap"].items() if k != "Common Topics"]):
398
- file.write(f'"{key}": {value}' + (',\n' if i < len(output_data["Topic Overlap"]) - 2 else '\n'))
399
- file.write('},\n')
400
- file.write(f'"Final Sentiment Analysis": "{output_data["Final Sentiment Analysis"]}"\n')
401
- print("\nOutput format:")
402
- with open(file_name, "r", encoding="utf-8") as file:
403
- print(file.read())
404
- print("✅ File saved successfully!")
405
  return file_name
406
 
 
407
  if __name__ == "__main__":
408
  company_name = input("Enter company name to search for (e.g., Tesla): ")
409
  fetch_and_save_news(company_name)
 
318
  if not company_name:
319
  print("❌ Error: Company name is required")
320
  return None
321
+
322
+ file_name = f"{company_name}_news.json"
323
  articles = []
324
  article_count = 0
325
  article_limit = 10
 
375
  },
376
  "Final Sentiment Analysis": analysis_result['Final Sentiment Analysis']
377
  }
378
+ # Write output data as valid JSON
379
  with open(file_name, "w", encoding="utf-8") as file:
380
+ json.dump(output_data, file, indent=4, ensure_ascii=False)
381
+ print(" File saved successfully as JSON!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
  return file_name
383
 
384
+
385
  if __name__ == "__main__":
386
  company_name = input("Enter company name to search for (e.g., Tesla): ")
387
  fetch_and_save_news(company_name)