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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from collections import Counter, defaultdict
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
import networkx as nx
|
7 |
+
import plotly.express as px
|
8 |
+
|
9 |
+
def create_recommendation_system(pairs):
|
10 |
+
sets_by_length = defaultdict(list)
|
11 |
+
|
12 |
+
for pair in pairs:
|
13 |
+
sets_by_length[len(pair)].append(set(pair))
|
14 |
+
|
15 |
+
sorted_keys = sorted(sets_by_length.keys(), reverse=True)
|
16 |
+
|
17 |
+
def recommend(restaurant):
|
18 |
+
recommended = set()
|
19 |
+
for length in sorted_keys:
|
20 |
+
for rest_set in sets_by_length[length]:
|
21 |
+
if restaurant in rest_set:
|
22 |
+
recommended.update(rest_set - {restaurant})
|
23 |
+
return list(recommended)
|
24 |
+
|
25 |
+
return recommend
|
26 |
+
|
27 |
+
def recommend(input_string):
|
28 |
+
f = open("task1_output_n_c1_sup4.txt", "r")
|
29 |
+
data = f.read()
|
30 |
+
data = data.split("Frequent Itemsets:")
|
31 |
+
|
32 |
+
clean_string = re.sub(r'\n', '', data[1])
|
33 |
+
pattern = r"\([^\)]+\)"
|
34 |
+
|
35 |
+
extracted_tuples = re.findall(pattern, clean_string.replace("'", ""))
|
36 |
+
|
37 |
+
tuple_list = [tuple(s.strip('()').split(', ')) for s in extracted_tuples]
|
38 |
+
|
39 |
+
recommendation_system = create_recommendation_system(tuple_list)
|
40 |
+
|
41 |
+
output_list = recommendation_system(input_string)
|
42 |
+
|
43 |
+
images_labels = [("https://logowik.com/content/uploads/images/aw-restaurants5299.jpg", "A&W Restaurant"),
|
44 |
+
("https://inspirebrands.com/wp-content/uploads/2017/10/Arbys.jpg","Arbys"),
|
45 |
+
("https://www.shutterstock.com/image-vector/vinnytsia-ukraine-october-8-2023-600nw-2372329457.jpg","Burger King"),
|
46 |
+
("https://static.wixstatic.com/media/cc8696_2f0a91e1d9d245efa98b100adfcdd887~mv2.png/v1/crop/x_0,y_0,w_242,h_71/fill/w_339,h_94,al_c,lg_1,q_85,enc_auto/logo.png","California Kitchen"),
|
47 |
+
("https://logos-world.net/wp-content/uploads/2022/11/Carls-Jr.-Logo-500x281.png","Carls Jr."),
|
48 |
+
("https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Chick-fil-A_Logo.svg/873px-Chick-fil-A_Logo.svg.png","Chick-Fil-A"),
|
49 |
+
("https://upload.wikimedia.org/wikipedia/en/thumb/3/38/CECLogo2019.svg/330px-CECLogo2019.svg.png","Chuck E. Cheese"),
|
50 |
+
("https://upload.wikimedia.org/wikipedia/commons/thumb/c/cc/Culver%27s_logo.svg/558px-Culver%27s_logo.svg.png","Culvers"),
|
51 |
+
("https://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/Dairy_Queen_logo.svg/300px-Dairy_Queen_logo.svg.png", "Dairy Queen"),
|
52 |
+
("https://logowik.com/content/uploads/images/493_dominospizza.jpg", "Dominos Pizza"),
|
53 |
+
("https://logos-world.net/wp-content/uploads/2023/03/Five-Guys-Logo-500x281.png", "Five Guys"),
|
54 |
+
("https://entrackr.com/storage/2023/06/Good-Flippin.jpg", "Good Flippin Burger"),
|
55 |
+
("https://logowik.com/content/uploads/images/hardees4024.jpg", "Hardees"),
|
56 |
+
("https://logos-marques.com/wp-content/uploads/2023/04/In-N-Out-Burger-Logo-768x432.png", "In-N-Out"),
|
57 |
+
("https://logos-world.net/wp-content/uploads/2022/08/Jack-in-the-Box-Logo-500x281.png", "Jack in the Box"),
|
58 |
+
("https://1000logos.net/wp-content/uploads/2021/05/Jollibee-logo-768x432.png", "Jollibee"),
|
59 |
+
("https://logowik.com/content/uploads/images/674_kfc.jpg", "KFC"),
|
60 |
+
("https://visitguernseycounty.com/wp-content/uploads/2022/05/Little-Caesars-Logo-1024x576.jpg", "Little Caesars"),
|
61 |
+
("https://logowik.com/content/uploads/images/mcdonalds-icon.jpg", "McDonalds"),
|
62 |
+
("https://images.squarespace-cdn.com/content/v1/53a47e51e4b0e78ae9ed2e97/4b8df8ab-662a-4381-92a6-97839099d5a7/Papa-Johns-logo.jpg", "Papa Johns"),
|
63 |
+
("https://cdn.nwe.io/files/x/d5/cc/c9810c9ad13188a3e0b2bc6577d7.jpg", "Pizza Inn"),
|
64 |
+
("https://logowik.com/content/uploads/images/294_pizza_hut_new_logo.jpg", "Pizza hut"),
|
65 |
+
("https://upload.wikimedia.org/wikipedia/commons/a/a6/PizzaExpress_Logo.jpg", "PizzaExpress"),
|
66 |
+
("https://images.squarespace-cdn.com/content/v1/53a47e51e4b0e78ae9ed2e97/be0aea01-84cc-4b89-a5e8-314ed394dc3c/Popeyes+logo.jpg", "Popeyes"),
|
67 |
+
("https://upload.wikimedia.org/wikipedia/commons/d/d9/Original_Round_Table_Pizza_Logo.jpg", "Round Table Pizza"),
|
68 |
+
("https://logowik.com/content/uploads/images/328_sbarro.jpg", "Sbarro"),
|
69 |
+
("https://1000logos.net/wp-content/uploads/2023/04/Shake-Shack-Logo-768x432.png", "Shake Shack"),
|
70 |
+
("https://upload.wikimedia.org/wikipedia/en/thumb/6/6d/Shakey%27s_US_logo.svg/330px-Shakey%27s_US_logo.svg.png", "Shakeys Pizza"),
|
71 |
+
("https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/SONIC_New_Logo_2020.svg/1199px-SONIC_New_Logo_2020.svg.png", "Sonic"),
|
72 |
+
("https://images.getbento.com/accounts/236fb3743b9522eafb90c6d2d20b8115/media/accounts/media/V1G5LTCrSDqyixbNUDpr_logo-tasty-burger.png", "Tasty burger"),
|
73 |
+
("https://cdn.worldvectorlogo.com/logos/uno-pizzeria.svg", "Uno Pizzeria"),
|
74 |
+
("https://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Wahlburgers_Logo.svg/768px-Wahlburgers_Logo.svg.png", "Wahlburgers"),
|
75 |
+
("https://logowik.com/content/uploads/images/866_wendys.jpg", "Wendys"),
|
76 |
+
("https://logowik.com/content/uploads/images/whataburger8433.jpg", "Whataburger"),
|
77 |
+
("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTsUZPMFzUmACfaeSrMSZsgGn5gnbXNyYRPxIrS1PfCrFWI9QJhrlkmdG9IhFbi", "White Castle")]
|
78 |
+
|
79 |
+
output = []
|
80 |
+
for i in images_labels:
|
81 |
+
if(i[1] in output_list):
|
82 |
+
output.append(i)
|
83 |
+
|
84 |
+
return output
|
85 |
+
|
86 |
+
|
87 |
+
def plot_heatmap():
|
88 |
+
f = open("task1_output_n_c1_sup4.txt", "r")
|
89 |
+
data = f.read()
|
90 |
+
data = data.split("Frequent Itemsets:")
|
91 |
+
|
92 |
+
clean_string = re.sub(r'\n', '', data[1])
|
93 |
+
pattern = r"\([^\)]+\)"
|
94 |
+
|
95 |
+
extracted_tuples = re.findall(pattern, clean_string.replace("'", ""))
|
96 |
+
|
97 |
+
frequent_itemsets = [tuple(s.strip('()').split(', ')) for s in extracted_tuples]
|
98 |
+
|
99 |
+
|
100 |
+
# Initialize a defaultdict for item pair frequencies
|
101 |
+
pair_counts = defaultdict(int)
|
102 |
+
|
103 |
+
# Count frequencies of item pairs, excluding self-pairings
|
104 |
+
for itemset in frequent_itemsets:
|
105 |
+
unique_items = list(set(itemset))
|
106 |
+
for i in range(len(unique_items)):
|
107 |
+
for j in range(i + 1, len(unique_items)):
|
108 |
+
pair = tuple(sorted([unique_items[i], unique_items[j]]))
|
109 |
+
pair_counts[pair] += 1
|
110 |
+
|
111 |
+
# Get a sorted list of unique items
|
112 |
+
all_items = sorted(set(item for pair in pair_counts for item in pair))
|
113 |
+
|
114 |
+
# Create a DataFrame for the heatmap
|
115 |
+
heatmap_data = pd.DataFrame(0, index=all_items, columns=all_items)
|
116 |
+
for (item1, item2), count in pair_counts.items():
|
117 |
+
heatmap_data.at[item1, item2] = count
|
118 |
+
heatmap_data.at[item2, item1] = count # Ensure symmetry
|
119 |
+
|
120 |
+
# Create the heatmap
|
121 |
+
fig = px.imshow(heatmap_data, labels=dict(x="Item", y="Item", color="Frequency"),
|
122 |
+
x=all_items, y=all_items,
|
123 |
+
color_continuous_scale=px.colors.sequential.Blues)
|
124 |
+
|
125 |
+
fig.update_layout(title='Heatmap of Item Co-occurrences (Excluding Self-Pairings)',
|
126 |
+
title_x=0.5)
|
127 |
+
|
128 |
+
return fig
|
129 |
+
|
130 |
+
def plot_bar():
|
131 |
+
f = open("task1_output_n_c1_sup4.txt", "r")
|
132 |
+
data = f.read()
|
133 |
+
data = data.split("Frequent Itemsets:")
|
134 |
+
|
135 |
+
clean_string = re.sub(r'\n', '', data[1])
|
136 |
+
pattern = r"\([^\)]+\)"
|
137 |
+
|
138 |
+
extracted_tuples = re.findall(pattern, clean_string.replace("'", ""))
|
139 |
+
|
140 |
+
frequent_itemsets = [tuple(s.strip('()').split(', ')) for s in extracted_tuples]
|
141 |
+
|
142 |
+
# Flatten the list of itemsets and count occurrences of each item
|
143 |
+
items = [item for itemset in frequent_itemsets for item in itemset]
|
144 |
+
item_counts = Counter(items)
|
145 |
+
|
146 |
+
# Convert the counter to a DataFrame
|
147 |
+
df = pd.DataFrame(item_counts.items(), columns=['Item', 'Count'])
|
148 |
+
|
149 |
+
# Sort the DataFrame in descending order by 'Count'
|
150 |
+
df = df.sort_values(by='Count', ascending=False)
|
151 |
+
|
152 |
+
# Create the bar chart
|
153 |
+
fig = px.bar(df, x='Item', y='Count', title='Frequency of Items in Frequent Itemsets',
|
154 |
+
labels={'Count': 'Frequency'}, color='Count', height=600)
|
155 |
+
|
156 |
+
fig.update_layout(xaxis_title='Item', yaxis_title='Frequency', title_x=0.5)
|
157 |
+
|
158 |
+
return fig
|
159 |
+
|
160 |
+
def plot_association():
|
161 |
+
f = open("task1_output_n_c1_sup4.txt", "r")
|
162 |
+
data = f.read()
|
163 |
+
data = data.split("Frequent Itemsets:")
|
164 |
+
|
165 |
+
clean_string = re.sub(r'\n', '', data[1])
|
166 |
+
pattern = r"\([^\)]+\)"
|
167 |
+
|
168 |
+
extracted_tuples = re.findall(pattern, clean_string.replace("'", ""))
|
169 |
+
|
170 |
+
frequent_itemsets = [tuple(s.strip('()').split(', ')) for s in extracted_tuples]
|
171 |
+
|
172 |
+
G = nx.DiGraph()
|
173 |
+
|
174 |
+
for itemset in frequent_itemsets:
|
175 |
+
if len(itemset) == 2:
|
176 |
+
G.add_edge(itemset[0], itemset[1])
|
177 |
+
else:
|
178 |
+
for i in range(len(itemset)):
|
179 |
+
for j in range(i + 1, len(itemset)):
|
180 |
+
G.add_edge(itemset[i], itemset[j])
|
181 |
+
|
182 |
+
pos = nx.spring_layout(G)
|
183 |
+
|
184 |
+
edge_x = []
|
185 |
+
edge_y = []
|
186 |
+
for edge in G.edges():
|
187 |
+
x0, y0 = pos[edge[0]]
|
188 |
+
x1, y1 = pos[edge[1]]
|
189 |
+
edge_x.append(x0)
|
190 |
+
edge_x.append(x1)
|
191 |
+
edge_x.append(None)
|
192 |
+
edge_y.append(y0)
|
193 |
+
edge_y.append(y1)
|
194 |
+
edge_y.append(None)
|
195 |
+
|
196 |
+
edge_trace = go.Scatter(
|
197 |
+
x=edge_x, y=edge_y,
|
198 |
+
line=dict(width=0.5, color='#888'),
|
199 |
+
hoverinfo='none',
|
200 |
+
mode='lines')
|
201 |
+
|
202 |
+
node_x = []
|
203 |
+
node_y = []
|
204 |
+
for node in G.nodes():
|
205 |
+
x, y = pos[node]
|
206 |
+
node_x.append(x)
|
207 |
+
node_y.append(y)
|
208 |
+
|
209 |
+
node_trace = go.Scatter(
|
210 |
+
x=node_x, y=node_y,
|
211 |
+
mode='markers+text',
|
212 |
+
text=[node for node in G.nodes()],
|
213 |
+
textposition="top center",
|
214 |
+
hoverinfo='text',
|
215 |
+
marker=dict(
|
216 |
+
showscale=True,
|
217 |
+
colorscale='YlGnBu',
|
218 |
+
size=10,
|
219 |
+
colorbar=dict(
|
220 |
+
thickness=15,
|
221 |
+
title='Node Connections',
|
222 |
+
xanchor='left',
|
223 |
+
titleside='right'
|
224 |
+
),
|
225 |
+
line_width=2))
|
226 |
+
|
227 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
228 |
+
layout=go.Layout(
|
229 |
+
title='<br>Association Rule Graph',
|
230 |
+
titlefont_size=16,
|
231 |
+
showlegend=False,
|
232 |
+
hovermode='closest',
|
233 |
+
margin=dict(b=20,l=5,r=5,t=40),
|
234 |
+
annotations=[ dict(
|
235 |
+
text="Association Rule Network",
|
236 |
+
showarrow=False,
|
237 |
+
xref="paper", yref="paper",
|
238 |
+
x=0.005, y=-0.002 ) ],
|
239 |
+
xaxis=dict(showgrid=False, zeroline=False),
|
240 |
+
yaxis=dict(showgrid=False, zeroline=False))
|
241 |
+
)
|
242 |
+
return fig
|
243 |
+
|
244 |
+
sorted_unique_restaurants = [
|
245 |
+
'A&W Restaurant',
|
246 |
+
'Arbys',
|
247 |
+
'Burger King',
|
248 |
+
'California Kitchen',
|
249 |
+
'Carls Jr.',
|
250 |
+
'Chick-Fil-A',
|
251 |
+
'Chuck E. Cheese',
|
252 |
+
'Culvers',
|
253 |
+
'Dairy Queen',
|
254 |
+
'Dominos Pizza',
|
255 |
+
'Five Guys',
|
256 |
+
'Good Flippin Burger',
|
257 |
+
'Hardees',
|
258 |
+
'In-N-Out',
|
259 |
+
'Jack in the Box',
|
260 |
+
'Jollibee',
|
261 |
+
'KFC',
|
262 |
+
'Little Caesars',
|
263 |
+
'McDonalds',
|
264 |
+
'Papa Johns',
|
265 |
+
'Pizza Inn',
|
266 |
+
'Pizza hut',
|
267 |
+
'PizzaExpress',
|
268 |
+
'Popeyes',
|
269 |
+
'Round Table Pizza',
|
270 |
+
'Sbarro',
|
271 |
+
'Shake Shack',
|
272 |
+
'Shakeys Pizza',
|
273 |
+
'Sonic',
|
274 |
+
'Tasty burger',
|
275 |
+
'Uno Pizzeria',
|
276 |
+
'Wahlburgers',
|
277 |
+
'Wendys',
|
278 |
+
'Whataburger',
|
279 |
+
'White Castle']
|
280 |
+
|
281 |
+
recommender = gr.Interface(fn=recommend, inputs=gr.Dropdown(choices=sorted_unique_restaurants, value="Burger King"), outputs=gr.Gallery(label="Generated images", show_label=False, elem_id="gallery", columns=[4], object_fit="contain", height="auto"), title='Restaurant Recommender System', description='Find your ideal restaurant')
|
282 |
+
vizz_heatmap = gr.Interface(fn=plot_heatmap, inputs = None, outputs=gr.Plot(label="Heatmap"), description = "Restaurant Itemset Frequency")
|
283 |
+
vizz_bar = gr.Interface(fn=plot_bar, inputs = None, outputs=gr.Plot(label="Bar Graph"), description = "Restaurant Frequency")
|
284 |
+
vizz_graph = gr.Interface(fn=plot_association, inputs = None, outputs=gr.Plot(label="Association Graph"), description = "Association Graph of Restaurants")
|
285 |
+
|
286 |
+
|
287 |
+
tabbed = gr.TabbedInterface([recommender, vizz_heatmap, vizz_bar, vizz_graph], ["Restaurant Recommender", "Heatmap", "Restaurant Frequency", "Association Graph"])
|
288 |
+
|
289 |
+
tabbed.queue().launch(share = True, debug = True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
plotly==5.15.0
|
2 |
+
networkx==3.3
|
task1_output_n_c1_sup4.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|