proKBD commited on
Commit
03d5192
·
verified ·
1 Parent(s): f18e43c

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +29 -14
utils.py CHANGED
@@ -23,6 +23,7 @@ def analyze_company_data(company_name: str) -> Dict[str, Any]:
23
  news_extractor = NewsExtractor()
24
  sentiment_analyzer = SentimentAnalyzer()
25
  text_summarizer = TextSummarizer()
 
26
 
27
  # Get news articles
28
  articles = news_extractor.search_news(company_name)
@@ -50,20 +51,20 @@ def analyze_company_data(company_name: str) -> Dict[str, Any]:
50
 
51
  # Analyze fine-grained sentiment
52
  try:
53
- # Financial sentiment
54
- if 'financial' in sentiment_analyzer.fine_grained_models:
55
- financial_sentiment = sentiment_analyzer.fine_grained_models['financial'](article['content'])[0]
56
- article['financial_sentiment'] = financial_sentiment['label']
57
 
58
- # Emotional sentiment
59
- if 'emotion' in sentiment_analyzer.fine_grained_models:
60
- emotional_sentiment = sentiment_analyzer.fine_grained_models['emotion'](article['content'])[0]
61
- article['emotional_sentiment'] = emotional_sentiment['label']
 
 
 
 
 
 
62
 
63
- # ESG sentiment
64
- if 'esg' in sentiment_analyzer.fine_grained_models:
65
- esg_sentiment = sentiment_analyzer.fine_grained_models['esg'](article['content'])[0]
66
- article['esg_sentiment'] = esg_sentiment['label']
67
  except Exception as e:
68
  print(f"Error in fine-grained sentiment analysis: {str(e)}")
69
 
@@ -84,13 +85,27 @@ def analyze_company_data(company_name: str) -> Dict[str, Any]:
84
  # Pad shorter arrays with 'neutral' to match the longest array
85
  sentiment_scores[source].extend(['neutral'] * (max_length - len(sentiment_scores[source])))
86
 
87
- return {
 
 
 
 
88
  "articles": processed_articles,
89
- "comparative_sentiment_score": sentiment_scores,
 
 
 
 
 
 
 
90
  "final_sentiment_analysis": overall_sentiment,
 
91
  "audio_path": None
92
  }
93
 
 
 
94
  except Exception as e:
95
  print(f"Error analyzing company data: {str(e)}")
96
  return {
 
23
  news_extractor = NewsExtractor()
24
  sentiment_analyzer = SentimentAnalyzer()
25
  text_summarizer = TextSummarizer()
26
+ comparative_analyzer = ComparativeAnalyzer()
27
 
28
  # Get news articles
29
  articles = news_extractor.search_news(company_name)
 
51
 
52
  # Analyze fine-grained sentiment
53
  try:
54
+ fine_grained_results = sentiment_analyzer._get_fine_grained_sentiment(article['content'])
55
+ article['fine_grained_sentiment'] = fine_grained_results
 
 
56
 
57
+ # Add sentiment indices
58
+ sentiment_indices = sentiment_analyzer._calculate_sentiment_indices(fine_grained_results)
59
+ article['sentiment_indices'] = sentiment_indices
60
+
61
+ # Add entities and sentiment targets
62
+ entities = sentiment_analyzer._extract_entities(article['content'])
63
+ article['entities'] = entities
64
+
65
+ sentiment_targets = sentiment_analyzer._extract_sentiment_targets(article['content'], entities)
66
+ article['sentiment_targets'] = sentiment_targets
67
 
 
 
 
 
68
  except Exception as e:
69
  print(f"Error in fine-grained sentiment analysis: {str(e)}")
70
 
 
85
  # Pad shorter arrays with 'neutral' to match the longest array
86
  sentiment_scores[source].extend(['neutral'] * (max_length - len(sentiment_scores[source])))
87
 
88
+ # Get comparative analysis
89
+ comparative_analysis = comparative_analyzer.analyze_coverage(processed_articles, company_name)
90
+
91
+ # Combine all results
92
+ result = {
93
  "articles": processed_articles,
94
+ "comparative_sentiment_score": {
95
+ "sentiment_distribution": comparative_analysis.get("sentiment_distribution", {}),
96
+ "sentiment_indices": comparative_analysis.get("sentiment_indices", {}),
97
+ "source_distribution": comparative_analysis.get("source_distribution", {}),
98
+ "common_topics": comparative_analysis.get("common_topics", []),
99
+ "coverage_differences": comparative_analysis.get("coverage_differences", []),
100
+ "total_articles": len(processed_articles)
101
+ },
102
  "final_sentiment_analysis": overall_sentiment,
103
+ "ensemble_info": sentiment_analyzer._get_ensemble_sentiment("\n".join([a['content'] for a in processed_articles])),
104
  "audio_path": None
105
  }
106
 
107
+ return result
108
+
109
  except Exception as e:
110
  print(f"Error analyzing company data: {str(e)}")
111
  return {