slackdemo / topic_analysis.py
svummidi's picture
POC for passive monitoring
a31ba66
raw
history blame
2.12 kB
import csv
from collections import Counter
# Initialize Counter object to keep track of the frequency of each topic
topic_counter = Counter()
include_positive = False
interested_topics = ["none", "error", "issue", "high cpu utilization", "lambdas", "authentication issue",
"error message"]
summary_list = []
topic_summary_list = {}
# Read the CSV file
try:
with open('csv/platform-engg-updated.csv', 'r', newline='', encoding='utf-8') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
# Extract the Positive Topics and Negative Topics fields and evaluate them as Python lists
positive_topics = eval(row['Positive Topics'])
negative_topics = eval(row['Negative Topics'])
# Update the counter based on the topics in this row
if include_positive:
for topic in positive_topics:
topic_counter[topic.lower()] += 1
for topic in negative_topics:
topic_counter[topic.lower()] += 1
if topic.lower() in interested_topics:
summary_list.append(row['Summary'])
if topic.lower() in topic_summary_list:
topic_summary_list[topic.lower()].append(row['Summary'])
else:
topic_summary_list[topic.lower()] = [row['Summary']]
# Display the consolidated list of topics along with their frequency
print(f"Consolidated List of Topics with Frequency({len(topic_counter.items())}):")
for topic, freq in topic_counter.most_common():
print(f"{topic}, {freq}")
print("Short listed summaries:")
for topic in topic_summary_list:
print(f"-------------------{topic}-------------------------------" )
for summary in topic_summary_list[topic]:
print(summary)
print(" ")
except FileNotFoundError:
print("File not found. Please make sure the file 'your_file.csv' exists in the same directory as this script.")
except Exception as e:
print(f"An error occurred: {e}")