Skip to content
PreEmpt Sentiments (Azure AI)
import json
def generate_json(startups_info, json_file):
with open(json_file, 'w') as file:
json.dump(startups_info, file, indent=4)
if __name__ == "__main__":
startups_info = {
"meta.com": {
"topics": [
"environmental initiatives",
"social responsibility",
"governance policies",
"sustainability strategies",
"commitment to ESG principles"
]
},
"twitter.com": {
"topics": [
"environment",
"social responsibility",
"governance",
"sustainability",
"ESG"
]
}
}
generate_json(startups_info, "startups.json")!pip install azure-search-documents
!pip install nltk
!pip install requests-html
import asyncio
import json
from azure.search.documents import SearchClient
from azure.core.credentials import AzureKeyCredential
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
nltk.download('vader_lexicon')
class Startup:
def __init__(self, name, topics, search_client):
self.name = name
self.topics = topics
self.search_client = search_client
self.data = {}
self.final_scores = {}
async def collect_data(self):
for topic in self.topics:
print(f"Collecting data for {self.name} on topic '{topic}'")
articles = await self.fetch_articles(topic)
if not articles:
print(f"No articles found for topic: {topic}")
async def fetch_articles(self, query):
search_query = f"{query} AND {self.name}"
print(f"Executing search query: {search_query}") # Debugging print
results = self.search_client.search(search_query)
articles = []
for result in results:
articles.append({
'title': result.get('title', 'No Title'),
'desc': result.get('description', 'No Description'),
'content': result.get('content', 'No Content'),
'link': result.get('url', 'No URL')
})
print(f"Articles fetched: {articles}") # Debugging print
return articles
def analyze_sentiment(self, text):
sia = SentimentIntensityAnalyzer()
sentiment_score = sia.polarity_scores(text)
return sentiment_score
async def collect_data(self):
for topic in self.topics:
print(f"Collecting data for {self.name} on topic '{topic}'")
articles = await self.fetch_articles(topic)
if not articles:
print(f"No articles found for topic: {topic}")
else:
print(f"Articles found for topic '{topic}':")
for article in articles:
print(article['title'])
print(article['desc'])
print(article['link'])
print() # Add a blank line for readability
self.data[topic] = articles
await asyncio.sleep(1) # Adding delay to avoid being rate-limited
def update_final_scores(self):
for topic, articles in self.data.items():
sentiments = [self.analyze_sentiment(article['content']) for article in articles]
self.final_scores[topic] = self.aggregate_scores(sentiments)
def aggregate_scores(self, sentiments):
positive = sum(1 for sentiment in sentiments if sentiment['compound'] > 0)
neutral = sum(1 for sentiment in sentiments if sentiment['compound'] == 0)
negative = sum(1 for sentiment in sentiments if sentiment['compound'] < 0)
return {'positive': positive, 'neutral': neutral, 'negative': negative}
def display_results(self):
for topic, scores in self.final_scores.items():
print(f"Sentiment analysis results for {self.name} on topic '{topic}':")
print(f"Positive: {scores['positive']}")
print(f"Neutral: {scores['neutral']}")
print(f"Negative: {scores['negative']}")
print() # Add a blank line for readability
def to_csv_dict(self):
rows = []
for topic, scores in self.final_scores.items():
row = {
'name': self.name,
'topic': topic,
'positive': scores.get('positive', 0),
'neutral': scores.get('neutral', 0),
'negative': scores.get('negative', 0)
}
rows.append(row)
return rows
def load_startups(json_file, search_client):
with open(json_file, 'r') as file:
data = json.load(file)
startups = []
for name, info in data.items():
startups.append(Startup(name, info['topics'], search_client))
return startups
import asyncio
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
endpoint = "https://sentiment.search.windows.net"
index_name = "startup-articles-index"
api_key = "51Y1vOc3y7dMLdWYqura4qKiftJrMZGXft9VjfCeIgAzSeBJY9AJ"
credential = AzureKeyCredential(api_key)
search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)
# Questions for horizon scanning
horizon_questions = [
"environmental initiatives",
"social responsibility",
"governance policies",
"sustainability strategies",
"commitment to ESG principles"
]
# Load startups from JSON file
startups = load_startups("startups.json", search_client)
# Perform the analysis on all startups
async def analyze_startups(startups):
for startup in startups:
await startup.collect_data()
startup.update_final_scores()
startup.display_results()
# Run the analysis
# Check if the code is running inside an existing event loop
if not asyncio.get_event_loop().is_running():
asyncio.run(analyze_startups(startups))
else:
# If already in an event loop, create a new task
await analyze_startups(startups)
# Optionally, save the results to a CSV file
import csv
with open("sentiment_analysis_results.csv", "w", newline='') as csvfile:
fieldnames = ['name', 'topic', 'positive', 'neutral', 'negative']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for startup in startups:
for row in startup.to_csv_dict():
writer.writerow(row)sia = SentimentIntensityAnalyzer()
print(sia.polarity_scores("This is a great product!"))
print(sia.polarity_scores("This is a bad product."))
# Import necessary libraries
from azure.search.documents import SearchClient
from azure.core.credentials import AzureKeyCredential
# Azure Search configuration
endpoint = "https://sentiment.search.windows.net"
index_name = "startup-articles-index"
api_key = "51Y1vOc3y7dMLdWYqura4qKiftJrMZGXft9VjfCeIgAzSeBJY9AJ"
# Initialize the search client
credential = AzureKeyCredential(api_key)
search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)
# Define a simple test query
test_query = "environment AND twitter.com"
# Function to perform a search and print results
def test_search_client(search_client, query):
try:
results = search_client.search(query)
print(f"Results for query '{query}':")
for result in results:
print(result)
print("Search completed successfully.")
except Exception as e:
print(f"An error occurred: {e}")
# Run the test
test_search_client(search_client, test_query)