File size: 5,519 Bytes
b396e94 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# import json
# import time
# from news_extraction import extract_news
# from news_summarisation import summarize_text
# from news_sentiment import analyze_sentiment
# from topic_extraction import preprocess_text, train_lda, extract_topic_words
# from comparative_analysis import comparative_sentiment_analysis
# from text_to_speech import text_to_speech # β
Import the TTS function
# def main():
# # User input for the company/topic
# company = input("Enter the company name for analysis: ").strip()
# # Extract news articles
# start_time = time.time()
# articles = extract_news(company)
# extraction_time = time.time() - start_time
# if not articles:
# print("No news articles found. Try a different company.")
# return
# articles_data = [] # List to store processed articles
# # Extract texts from articles for sentiment analysis
# texts = [article["text"] for article in articles]
# # Perform sentiment analysis
# start_time = time.time()
# sentiment_results = analyze_sentiment(texts)
# sentiment_time = time.time() - start_time
# # Process each article
# for i, (article, sentiment) in enumerate(zip(articles, sentiment_results["Predicted Sentiment"]), start=1):
# start_time = time.time()
# summary = summarize_text(article["text"]) # Summarize article
# summarization_time = time.time() - start_time
# # Extract topics for the specific article
# preprocessed_text = preprocess_text([article["text"]])
# lda_model, dictionary = train_lda(preprocessed_text)
# topic_words = extract_topic_words(lda_model)
# article_entry = {
# "Title": article["title"],
# "Summary": summary,
# "Sentiment": sentiment,
# "Topics": topic_words
# }
# articles_data.append(article_entry)
# # Perform comparative sentiment analysis
# analysis_result = comparative_sentiment_analysis(company, articles_data)
# # β
Generate a summary speech for the entire report
# final_summary = f"{company}βs latest news coverage is mostly {analysis_result['Final Sentiment Analysis']}."
# audio_file = text_to_speech(final_summary) # Generate Hindi TTS
# # β
Construct final JSON output
# output = {
# "Company": company,
# "Articles": articles_data,
# "Comparative Sentiment Score": analysis_result,
# "Final Sentiment Analysis": final_summary,
# "Audio": f"[Play {audio_file}]" # β
Include a playable reference
# }
# # Print JSON output
# print(json.dumps(output, indent=4, ensure_ascii=False))
# # Save JSON output to file
# with open(f"{company}_news_analysis.json", "w", encoding="utf-8") as json_file:
# json.dump(output, json_file, indent=4, ensure_ascii=False)
# if __name__ == "__main__":
# main()
import json
import time
from utils.news_extraction import extract_news
from utils.news_summarisation import summarize_text
from utils.news_sentiment import analyze_sentiment
from utils.topic_extraction import preprocess_text, train_lda, extract_topic_words
from utils.comparative_analysis import comparative_sentiment_analysis
from utils.text_to_speech import text_to_speech # β
Import the TTS function
def analyze_company_news(company):
# Extract news articles
start_time = time.time()
articles = extract_news(company)
extraction_time = time.time() - start_time
if not articles:
return {"message": "No news articles found. Try a different company."}
articles_data = [] # List to store processed articles
# Extract texts from articles for sentiment analysis
texts = [article["text"] for article in articles]
# Perform sentiment analysis
start_time = time.time()
sentiment_results = analyze_sentiment(texts)
sentiment_time = time.time() - start_time
# Process each article
for i, (article, sentiment) in enumerate(zip(articles, sentiment_results["Predicted Sentiment"]), start=1):
start_time = time.time()
summary = summarize_text(article["text"]) # Summarize article
summarization_time = time.time() - start_time
# Extract topics for the specific article
preprocessed_text = preprocess_text([article["text"]])
lda_model, dictionary = train_lda(preprocessed_text)
topic_words = extract_topic_words(lda_model)
article_entry = {
"Title": article["title"],
"Summary": summary,
"Sentiment": sentiment,
"Topics": topic_words
}
articles_data.append(article_entry)
# Perform comparative sentiment analysis
analysis_result = comparative_sentiment_analysis(company, articles_data)
# β
Generate a summary speech for the entire report
final_summary = f"{company}βs latest news coverage is mostly {analysis_result['Final Sentiment Analysis']}."
audio_file = text_to_speech(final_summary) # Generate TTS
# β
Construct final JSON output
output = {
"Company": company,
"Articles": articles_data,
"Comparative Sentiment Score": analysis_result,
"Audio": f"[Play {audio_file}]" # β
Include a playable reference
}
return output
# if __name__ == "__main__":
# company = input("Enter the company name for analysis: ").strip()
# result = analyze_company_news(company)
# print(json.dumps(result, indent=4, ensure_ascii=False)) |