File size: 5,269 Bytes
03fcc07
a7e7b92
03fcc07
eb9e44f
03fcc07
 
 
 
 
 
6960ade
03fcc07
 
 
 
 
 
 
 
a7e7b92
03fcc07
 
 
 
 
 
a7e7b92
6960ade
03fcc07
 
a7e7b92
03fcc07
 
 
 
 
a7e7b92
 
 
 
 
 
 
3b46464
 
 
a7e7b92
 
 
 
3b46464
a7e7b92
eb9e44f
a7e7b92
3b46464
a7e7b92
eb9e44f
741ce5e
eb9e44f
741ce5e
eb9e44f
741ce5e
3b46464
a7e7b92
eb9e44f
 
a7e7b92
 
3b46464
a7e7b92
 
03fcc07
 
a7e7b92
 
03fcc07
 
 
6960ade
03fcc07
a7e7b92
03fcc07
 
a7e7b92
6960ade
 
a7e7b92
03fcc07
6960ade
 
2fb862a
6960ade
a7e7b92
6960ade
 
a7e7b92
03fcc07
a7e7b92
 
03fcc07
 
6960ade
 
741ce5e
a7e7b92
 
 
6960ade
a7e7b92
 
 
741ce5e
 
 
 
 
 
 
 
 
 
 
 
6960ade
 
a7e7b92
 
 
 
 
 
 
6960ade
a7e7b92
03fcc07
 
 
 
2fb862a
a7e7b92
2fb862a
a7e7b92
2fb862a
a7e7b92
 
2fb862a
a7e7b92
 
 
2fb862a
6960ade
 
a7e7b92
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
146
147
148
149
150
151
152
153
import pandas as pd
import openai, json
from collections import Counter
from random import choice

INPUT_DATAPATH = "slack_processing/data/themes.json"
OUTPUT_GAME_IDEAS_DATAPATH="create_games/data/game_ideas.json"
themes = []
themes_by_person={}
people_by_theme = {}
players=[]

def ReadThemesFromFile():
    global themes
    with open(INPUT_DATAPATH, "r") as json_file:
        themes = json.load(json_file)

def PopulateThemesByPerson():
    global themes, themes_by_person
    print("PopulateThemesByPerson")
    themes_by_person = {}
    for theme in themes:
        person = theme['person']
        postId = theme['postId']
        theme_info = (theme['theme'], postId)
        themes_by_person.setdefault(person, set()).add(theme_info)
    print("end, themes_by_person, ", themes_by_person)

def PopulatePeopleByThemes():
    global themes, people_by_theme
    print("PopulatePeopleByThemes")
    people_by_theme = {}
    for theme in themes:
        person = theme['person']
        theme_name = theme['theme']
        people_by_theme.setdefault(theme_name, set()).add(person)
    print("end, people_by_theme, ", people_by_theme)

def FindCommonTheme(people_set):
    global themes_by_person, people_by_theme
    theme_counter = Counter()    
    for person in people_set:
        if person in themes_by_person:
            unique_themes = set(theme for theme, _ in themes_by_person[person])
            for theme in unique_themes:
                theme_counter[theme] += 1  

    # Case 1: Theme shared by all people
    for theme, count in theme_counter.items():
        if count == len(people_set):
            print("case 1: shared by all", theme)
            return theme    
            
    # Case 2: Theme shared by most people
    print('theme_counter', theme_counter)
    if theme_counter:
        # Find the highest frequency among themes
        max_count = theme_counter.most_common(1)[0][1]        
        # Gather all themes with that frequency
        most_common_themes = [theme for theme, count in theme_counter.items() if count == max_count]        
        # Randomly choose among them
        most_common_theme = choice(most_common_themes)        
        print("case 2: shared by most", most_common_theme)
        return most_common_theme    
    
    # Case 3: Most popular theme
    if people_by_theme:
        most_popular_theme, _ = Counter({theme: len(people) for theme, people in people_by_theme.items()}).most_common(1)[0]
        print("case 3: most_popular_theme", most_popular_theme)
        return most_popular_theme
    else:
        return None
    
def GenerateGameIdeas(common_topic, group):
    prompt = f"""Provide rules for a drawing game that this group of players '{group}' may play centered on this theme '{common_topic}'. Keep your answers short. Answer in JSON, in this format: {{"Name":"Name of game","Description":"Game rules","Rationale":"Reasoning for game selection"}}"""
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=800,
        n=1,
        temperature=0.1)
     # Process the response and extract the game ideas
    game_ideas = []
    for choice in response["choices"]:    
        choiceText = choice["text"]
        game_data = choiceText.strip('\n')
        print("--after strip game_data", game_data)
        try:
            game_data_dict = json.loads(game_data)
            game_ideas.append(game_data_dict)
            print("----game_data", game_data_dict)  
        except json.JSONDecodeError:
            print("Error decoding JSON.")
        continue
              
    if game_ideas:
        with open(OUTPUT_GAME_IDEAS_DATAPATH, 'w') as file:
            json.dump(game_ideas, file, indent=4)
        return game_ideas
    else:
        print("No game idea found.")
        return None       
   
def GetPlayersAndIndex():
    players = []  
    index = 0  
    for theme in themes:  
        person = theme['person']
        if person not in [t[0] for t in players]: 
            players.append((person, index)) 
            index += 1  
    print("\n***players", players)
    return players

def GetPlayers():
    players = []  
    index = 0  
    for theme in themes:  
        person = theme['person']
        if person not in players: 
            players.append((person)) 
          
    print("\n***players", players)
    return players

def GetPeopleByThemesDF():
    global people_by_theme
    flattened_data = [(theme, person) for theme, persons in people_by_theme.items() for person in persons]
    # Create a DataFrame
    df = pd.DataFrame(flattened_data, columns=['Theme', 'Person'])
    return df  

def LoadThemes():
    global themes,people_by_theme, themes_by_person, players
    ReadThemesFromFile()
    PopulatePeopleByThemes()
    PopulateThemesByPerson()

def CreateGameForGroup(group, retry=False):
    global themes
    if not retry:
        common_topic = FindCommonTheme(group)
    else:
        common_topic = themes   
    game_ideas = GenerateGameIdeas(common_topic, group)
    if game_ideas is None:
        if not retry:
            retry=True
            return CreateGameForGroup(players, retry)
    else:
        gameDF=pd.DataFrame(game_ideas)
        return gameDF
    return None