File size: 3,367 Bytes
26ead64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai, json, re, random
import pandas as pd
from utilities import date_format, prompt_constants

def Completion(slack_message):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo", 
        messages=[
            {"role": "system", "content": prompt_constants.SLACK_SENTIMENT_SYSTEM_PROMPT},
            {"role": "user", "content": slack_message} ])
    print("response")
    print(response["choices"][0]["message"]["content"])
    return response["choices"][0]["message"]["content"]

def sanitize_blob(blob_str):
    return re.sub(r"(?<=: )'", '"', re.sub(r"'(?=:)", '"', blob_str))

def FindScore(response):
    match = re.search(r"\b(0(\.\d+)?|1(\.0+)?)\b", response)
    random_offset = round(random.uniform(0.01, .099), 3)
    if match:
        value = round(float(match.group(1)), 2)
        return value + random_offset
    else:
        return 0
    
def CheckType(response):
    if isinstance(response, float):
        return round(response, 2)
    elif isinstance(response, str):
        return FindScore(response)
    
def ProcessMessage(message, summary_messages, slack_messages, id, parent_user=None ):
    user = message["user"]
    message_text = message["text"]
    timestamp = message["timestamp"]
    response = Completion(message_text)  # Assuming Completion is defined elsewhere
    summary_messages.append({"role": "user", "content": message_text})
    summary_messages.append({"role": "assistant", "content": response})

    sentiment_score = CheckType(response)  # Assuming CheckType is defined elsewhere

    sentiment = "Neutral"
    if sentiment_score == 0:
        sentiment = "Undefined"
    elif 0 < sentiment_score < 0.3:
        sentiment = "Negative"
    elif sentiment_score > 0.6:
        sentiment = "Positive"
    
    dateX, timeX, twentyfour_time = date_format.TimeStampToDateAndTime(timestamp)

    message_obj = {
        "id": id,
        "user": user,
        "message": f"{message_text}",
        "date": dateX +": " +twentyfour_time,        
        "time": timestamp,
        "twentyfour_time": twentyfour_time,
        "sentiment_score": sentiment_score,
        "sentiment": sentiment,
        "size": 8,
        "parent_user": parent_user
    }
    id=id+1
    slack_messages.append(message_obj)
    # Process nested replies if any
    if "replies" in message:
        for reply in message["replies"]:
            ProcessMessage(reply, summary_messages, slack_messages, id, parent_user=user)
            id=id+1

def ParseBlobs(blob, summary_messages):
    global id
    sanitized_blob = sanitize_blob(blob)
    try:
        response_data = json.loads(sanitized_blob)
    except json.JSONDecodeError:
        print("Invalid JSON format.")
        return None
    
    slack_messages = []
    summary_messages.append({"role": "system", "content": prompt_constants.SLACK_SENTIMENT_SYSTEM_PROMPT})
    for message in response_data["messages"]:
        ProcessMessage(message,summary_messages,slack_messages, id)
        id=id+1
    jsonobj = json.dumps(slack_messages, ensure_ascii=False)
    return jsonobj,summary_messages

def AnalyzeSentiment(blob):    
    global id
    summary_messages = []
    id=3
    slack_blobs,summary_messages=ParseBlobs(blob,summary_messages)   
    df = pd.DataFrame(summary_messages)
    sentimentDF=pd.read_json(slack_blobs)
    return df, sentimentDF, id+3