70.0% ≤ sim ≤ 100%

HW6_LIST

11 clusters, 25 submissions

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #1(2)

# 6430418221 (point = 31.82%) sim = 100.0% def compute_similarity(food_name1, food_name2): def match_foods(nutrient, food_name): # --------------------------------------------------- def get_nutrient(nutrient,food_name): # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): # ---------------------------------------------------# 6432195621 (point = 31.82%) sim = 100.0% def compute_similarity(food_name1, food_name2): # --------------------------------------------------- def match_foods(nutrient, food_name): # --------------------------------------------------- def get_nutrient(nutrient,food_name): # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #2(2)

# 6430348021 (point = 31.82%) sim = 100.0% def compute_similarity(food_name1, food_name2): a=food_name1.split() b=food_name2.split() count=0 for x in b : if x in a : count += 1 return count*2/(len(a)+len(b)) # --------------------------------------------------- def match_foods(nutrient, food_name): a=[] for item in nutrient: a.append((item[0],compute_similarity(item[1],food_name))) mx = 0 for item in a : if item[1]>mx : mx=item[1] ans=[] for item in a : if item[1]>0.5 and item[1]==mx : ans.append(item[0]) return sorted(ans) # --------------------------------------------------- def get_nutrient(nutrient,food_name): a=match_foods(nutrient,food_name) if a == [] : return [] ans = [0.0] * 6 cnt = [0] * 6 for id in a: for item in nutrient: if item[0] == id: for i in range(6): if item[i+2] != 'NA': ans[i] += item[i+2] cnt[i] += 1 for i in range(6): if cnt[i] == 0: ans[i] = 'NA' else: ans[i] /= cnt[i] return ans # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): ans = [] for p in intakes: day, food = p val = get_nutrient(nutrient, food) idx = -1 for i in range(len(ans)): if ans[i][0] == day: idx = i if idx == -1: idx = len(ans) ans.append([day, 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', True]) if val == []: ans[idx][7] = False else: for i in range(6): if val[i] != 'NA': if ans[idx][i+1] == 'NA': ans[idx][i+1] = 0.0 ans[idx][i+1] += val[i] return sorted(ans, reverse=True, key=lambda elem: elem[0]) # ---------------------------------------------------# 6430364021 (point = 31.82%) sim = 100.0% def compute_similarity(food_name1, food_name2): a=food_name1.split() b=food_name2.split() count=0 for x in b : if x in a : count += 1 return count*2/(len(a)+len(b)) # --------------------------------------------------- def match_foods(nutrient, food_name): a=[] for item in nutrient: a.append((item[0],compute_similarity(item[1],food_name))) mx = 0 for item in a : if item[1]>mx : mx=item[1] ans=[] for item in a : if item[1]>0.5 and item[1]==mx : ans.append(item[0]) return sorted(ans) # --------------------------------------------------- def get_nutrient(nutrient,food_name): a=match_foods(nutrient,food_name) if a == [] : return [] ans = [0.0] * 6 cnt = [0] * 6 for id in a: for item in nutrient: if item[0] == id: for i in range(6): if item[i+2] != 'NA': ans[i] += item[i+2] cnt[i] += 1 for i in range(6): if cnt[i] == 0: ans[i] = 'NA' else: ans[i] /= cnt[i] return ans # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): ans = [] for p in intakes: day, food = p val = get_nutrient(nutrient, food) idx = -1 for i in range(len(ans)): if ans[i][0] == day: idx = i if idx == -1: idx = len(ans) ans.append([day, 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', True]) if val == []: ans[idx][7] = False else: for i in range(6): if val[i] != 'NA': if ans[idx][i+1] == 'NA': ans[idx][i+1] = 0.0 ans[idx][i+1] += val[i] return sorted(ans, reverse=True, key=lambda elem: elem[0]) # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #3(2)

# 6230027321 (point = 31.82%) sim = 94.27% def compute_similarity(food_name1, food_name2): split_food1 = food_name1.split() split_food2 = food_name2.split() c = 0 for e in split_food1 : if e in split_food2 : c += 1 return c*2 / (len(split_food1)+len(split_food2)) # --------------------------------------------------- def match_foods(nutrient, food_name): # --------------------------------------------------- def get_nutrient(nutrient,food_name): # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): # ---------------------------------------------------# 6430298221 (point = 31.82%) sim = 94.27% def compute_similarity(food_name1, food_name2): f1 = food_name1.split() f2 = food_name2.split() counts = 0 for a in f1: if a in f2: counts += 1 b = counts*2 /(len(f1)+len(f2)) return b # --------------------------------------------------- def match_foods(nutrient, food_name): # --------------------------------------------------- def get_nutrient(nutrient,food_name): # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #4(4)

# 6430206521 (point = 31.82%) sim = 82.99% def compute_similarity(food_name1, food_name2): f1=food_name1.split() f2=food_name2.split() c=0 s=len(f1)+len(f2) for e in f1: if e in f2: c+=1 r=(c*2)/s return r # --------------------------------------------------- def match_foods(nutrient, food_name): p = [] for n in nutrient: k = compute_similarity(food_name, n[1]) if 0.5<k: p.append([k, n[0]]) if len(p) == 0: return [] m = max(p)[0] l = [] for x in p: if x[0]==m: l.append(x[1]) return l # --------------------------------------------------- def get_nutrient(nutrient,food_name): p = match_foods(nutrient, food_name) if len(p) == 0: return [] nu = [] for r in nutrient: if r[0] in p: nu.append(r) h = ["NA"] * 6 for i in range(6): Sum = 0 co = 0 for j in range(len(nu)): if nu[j][i+2] != "NA": Sum += nu[j][i+2] co += 1 if co != 0: h[i] = Sum / co return h # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): intakes = sorted(intakes) summary = [] for meal in intakes: summary.append([meal[0], get_nutrient(nutrient, meal[1])]) day = [] wan = [] date = summary[0][0] for meal in summary: if meal[0] != date: wan.append([date, day]) day = [] date = meal[0] day.append(meal[1]) wan.append([date, day]) wan.sort() final = [] for date in wan: data = ["NA"] * 6 for i in range(6): for meal in date[1]: if len(meal) == 0: continue if meal[i] != "NA": if data[i] != "NA": data[i] += meal[i] else: data[i] = meal[i] ans = [date[0]] + data if [] in date[1]: ans += [False] else: ans += [True] final.append(ans) return final[::-1] # ---------------------------------------------------# 6430351821 (point = 31.82%) sim = 82.99% def compute_similarity(food_name1, food_name2): food1 = food_name1.split() food2 = food_name2.split() a = 0 for x in food1: if x in food2: a += 1 simi = (a*2) / (len(food2) + len(food1)) return simi # --------------------------------------------------- def match_foods(nutrient, food_name): most = 0 matched = [] for food in nutrient: similar_score = compute_similarity(food_name, food[1]) if similar_score > 0.5: matched.append([similar_score, food[0]]) if len(matched) == 0: return [] m = matched m.sort(reverse = True) score = m[0][0] result = [] for i in m: if i[0] == score: result.append(i[1]) result.sort() return result # --------------------------------------------------- def get_nutrient(nutrient,food_name): ids = match_foods(nutrient, food_name) if len(ids) == 0: return [] data = [] for food in nutrient: if food[0] in ids: data.append(food) res = ['NA','NA','NA','NA','NA','NA'] for i in range(6): sum = 0 c = 0 for j in range(len(data)): if data[j][i+2] != 'NA': sum += data[j][i+2] c += 1 if c != 0: res[i] = sum / c return res # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): intake = intakes intake.sort() summary = [] for m in intake: summary.append([m[0], get_nutrient(nutrient, m[1])]) meal_date = [] dates = [] date = summary[0][0] for m in summary: if m[0] != date: dates.append([date, meal_date]) meal_date = [] date = m[0] meal_date.append(m[1]) dates.append([date, meal_date]) d = dates d.sort() final = [] for date in d: date_data = ['NA','NA','NA','NA','NA','NA'] for i in range(6): for m in date[1]: if len(m) == 0: continue if m[i] != 'NA': if date_data[i] != 'NA': date_data[i] += m[i] else: date_data[i] = m[i] result = [date[0]] + date_data if [] in date[1]: result += [False] else: result += [True] final.append(result) return final[::-1] # ---------------------------------------------------# 6432099021 (point = 31.82%) sim = 76.72% def compute_similarity(food_name1, food_name2): food_name1_pieces = food_name1.split() food_name2_pieces = food_name2.split() matching_pieces = 0 for i in range(len(food_name1_pieces)) : if food_name1_pieces[i] in food_name2_pieces : matching_pieces += 1 similarity = (matching_pieces * 2) / ( len(food_name1_pieces) + len(food_name2_pieces) ) return similarity # --------------------------------------------------- def match_foods(nutrient, food_name): final_score = 0 selected_foods = [] for anyfood in nutrient : current_score = compute_similarity(food_name, anyfood[1]) if current_score > final_score : final_score = current_score if final_score <= 0.5 : return [] for anyfood in nutrient : current_score = compute_similarity(food_name, anyfood[1]) if current_score == final_score : selected_foods.append(anyfood[0]) return selected_foods # --------------------------------------------------- def get_nutrient(nutrient,food_name): selected_foods = match_foods(nutrient, food_name) if selected_foods == [] : return [] selected_foods_name = [] for anyfood in nutrient : if anyfood[0] in selected_foods : selected_foods_name.append(anyfood) completed_list = [] for i in range(6) : completed_list.append("NA") for i in range(6): counter = 0 summary = 0 for n in range(len(selected_foods_name)) : if selected_foods_name[n][i+2] != "NA" : summary += selected_foods_name[n][i+2] counter += 1 if counter != 0: completed_list[i] = summary / counter return completed_list # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): summary = [] intakes = sorted(intakes) for anyfoods in intakes : summary.append([anyfoods[0], get_nutrient(nutrient, anyfoods[1])]) dailymeals = [] selected_dates = [] focused_dates = summary[0][0] for anyfoods in summary : if anyfoods[0] != focused_dates : selected_dates.append([focused_dates, dailymeals]) dailymeals = [] focused_dates = anyfoods[0] dailymeals.append(anyfoods[1]) selected_dates.append([focused_dates, dailymeals]) selected_dates = sorted(selected_dates) summarized = [] for focused_dates in selected_dates: details = [] for i in range(6) : details.append("NA") for i in range(6) : for anyfoods in focused_dates[1] : if len(anyfoods) == 0 : continue if anyfoods[i] != "NA" : if details[i] != "NA" : details[i] += anyfoods[i] else: details[i] = anyfoods[i] finalized = [focused_dates[0]] + details if [] in focused_dates[1] : finalized += [False] else: finalized += [True] summarized.append(finalized) return summarized[::-1] # ---------------------------------------------------# 6432103921 (point = 31.82%) sim = 74.9% def compute_similarity(food_name1, food_name2): food_1 = food_name1.split() food_2 = food_name2.split() match = 0 s = (len(food_1) + len(food_2)) for food_name1 in food_1: for food_name2 in food_2: if food_name1 == food_name2: match += 1 return (match * 2) / s # --------------------------------------------------- def match_foods(nutrient, food_name): matched = list() for similar in nutrient: n = compute_similarity(similar[1] , food_name) if n > 0.5: matched.append([n , similar[0]]) if matched == []: return [] Max_food = max(matched)[0] last_maxfood = list() for similar in matched: if similar[0] == Max_food: last_maxfood.append(similar[1]) return last_maxfood # --------------------------------------------------- def get_nutrient(nutrient,food_name): matched = match_foods(nutrient, food_name) space = [] space_2 = list() if matched == []: return space for i in nutrient: for food in matched: if food == i[0]: space_2.append(i) nu = [0] * 6 last_nutrient = ["NA"] * 6 for x in space_2: x = x[2::] for i in range(len(x)): if x[i] != "NA": if last_nutrient[i] == "NA" : last_nutrient[i] = 0 last_nutrient[i] += x[i] nu[i] += 1 for i in range(len(last_nutrient)): if last_nutrient[i] != "NA": last_nutrient[i] /= nu[i] return last_nutrient # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): intakes = sorted(intakes) space = [] for i in intakes: space.append([i[0], get_nutrient(nutrient, i[1])]) per_day = [] day = [] date = space[0][0] for i in space : if i[0] != date: day.append([date, per_day]) per_day = [] date = i[0] per_day.append(i[1]) day.append([date, per_day]) day = sorted(day) for date in day: inf = ["NA"] * 6 for i in range(6): for meal_per_day in date[1]: if len(meal_per_day) == 0: continue if meal_per_day[i] != 'NA': if inf[i] != 'NA': inf[i] += meal_per_day[i] else: inf[i] = meal_per_day[i] result = [date[0]] + inf if [] in date[1]: result.append(False) else: result.append(True) space.append(result) return space[::-1] # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #5(2)

# 6432199121 (point = 31.82%) sim = 81.96% def compute_similarity(food_name1, food_name2): x = food_name1.split() y = food_name2.split() count = 0 s = len(x) + len(y) for e in x: for u in y: if e == u: count += 1 return (count * 2) / s # --------------------------------------------------- def match_foods(nutrient, food_name): tod = [] for temp in nutrient: if compute_similarity(temp[1], food_name) > 0.5: tod += [[temp[0], compute_similarity(temp[1], food_name)]] if tod == []: return [] mx = -1 for i in tod: mx = max(mx, i[1]) ans = [] for t in tod: if t[1] == mx: ans += [t[0]] return ans # --------------------------------------------------- def get_nutrient(nutrient,food_name): Match = match_foods(nutrient, food_name) if Match == []: return [] Nu = list() for i in nutrient: for j in Match: if i[0] in j: Nu += [i] TIME = [0] * 6 SUM = ['NA'] * 6 for temp in Nu: temp = temp[2::] for i in range(len(temp)): if temp[i] != 'NA': if SUM[i] == 'NA': SUM[i] = 0 SUM[i] += temp[i] TIME[i] += 1 AVG = ['NA'] * 6 for i in range(len(SUM)): if SUM[i] != 'NA': AVG[i] = SUM[i] / TIME[i] return AVG # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): tl = list() df = list() for t in intakes : idx = find_date_index(t[0], tl) if idx == -1 : tl.append([t[0]]) if match_foods(nutrient,t[1]) == [] : df.append(t[0]) tl[idx].append(t[1]) energy = list() for t in tl : idx = find_date_index(t[0], energy) if idx == -1 : energy.append([t[0]] + ['NA'] * 6 + [True]) for food_name in t[1::] : g_nu = get_nutrient(nutrient, food_name) for x in range(len(g_nu)) : if g_nu[x] != 'NA' : if energy[idx][x+1] == 'NA' : energy[idx][x+1] = 0 energy[idx][x+1] += g_nu[x] for d in df : idx = find_date_index(d, energy) energy[idx][-1] = False return sorted(energy)[::-1] def find_date_index(search, List): for t in range(len(List)): if List[t][0] == search: return t return -1 # ---------------------------------------------------# 6432200021 (point = 31.82%) sim = 81.96% def compute_similarity(food_name1, food_name2): f1 = food_name1.split() f2 = food_name2.split() c = 0 #จำนวนคำที่ตรง s = len(f1)+len(f2) for i in f1: for j in f2: if i==j: c +=1 return c*2/s # --------------------------------------------------- def match_foods(nutrient, food_name): d = [] for n in nutrient: c = compute_similarity(n[1],food_name) if c > 0.5: d.append([c,n[0]]) if d == []: return [] mx = max(d)[0] out = [] for c,n in d: if c==mx: out.append(n) return out # --------------------------------------------------- def get_nutrient(nutrient,food_name): mf = match_foods(nutrient,food_name) if mf==[]: return [] nu = [] for i in nutrient: for j in mf: if j==i[0]: nu.append(i) t = [0]*6 s = ['NA']*6 for n in nu: n = n[2:] for i in range(len(n)): if n[i]!='NA': if s[i]=='NA': s[i] = 0 s[i] += n[i] t[i] += 1 for i in range(len(s)): if s[i]!='NA': s[i] /= t[i] return s # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): L = [] TL = [] day_false = [] E = [] for t in intakes: idx = search_id(t[0],TL) if idx==-1: TL += [[t[0]]] if match_foods(nutrient, t[1])==[]: day_false += [t[0]] TL[idx] += [t[1]] for t in TL: idx = search_id(t[0],E) if idx==-1: E += [[t[0]] + ['NA']*6 + [True]] for food_name in t[1::]: g_nu = get_nutrient(nutrient,food_name) for x in range(len(g_nu)): if g_nu[x] != 'NA': if E[idx][x+1]=='NA': E[idx][x+1] = 0 E[idx][x+1] += g_nu[x] for d in day_false: idx = search_id(d,E) E[idx][-1] = False return sorted(E)[::-1] # --------------------------------------------------- def search_id(s, L): for i in range(len(L)): if L[i][0] == s: return i return -1 # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #6(2)

# 6430450221 (point = 31.82%) sim = 77.95% def compute_similarity(food_name1, food_name2): food1 = food_name1.split() food2 = food_name2.split() x = 0 for food_name1 in food1: for food_name2 in food2: if food_name1 == food_name2: x += 1 z = x*2/(len(food1)+len(food2)) return z # --------------------------------------------------- def match_foods(nutrient, food_name): listfood = [] for food in nutrient: similarity = compute_similarity(food_name,food[1]) if similarity > 0.5: listfood += [[similarity,food[0]]] if len(listfood) == 0: return [] max_similarity = max(listfood)[0] out = [] for food in listfood: if food[0] == max_similarity: out += [food[1]] return out # --------------------------------------------------- def get_nutrient(nutrient,food_name): matched_foods = match_foods(nutrient, food_name) listnutrient = [] for food in matched_foods: for n in nutrient: if food == n[0]: listnutrient.append(n[2:]) if len(listnutrient) == 0: return [] result = ["NA"] * 6 for i in range(6): s = [] for listnutrients in listnutrient: if listnutrients[i] != "NA": s.append(listnutrients[i]) if len(s) > 0: result[i] = sum(s) / len(s) return result # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): summary_daily_nutrient = [] for i in intakes: n = get_nutrient(nutrient, i[1]) date = i[0] exist = False for j in summary_daily_nutrient: if date == j[0]: exist = True j[1].append(n) if not exist: summary_daily_nutrient.append([date, [n]]) summary_daily_nutrient.sort(key=lambda x: x[0], reverse=1) result_daily_nutrient = [] for i in summary_daily_nutrient: result = ["NA"] * 6 complete = i[1].count([]) == 0 for nutrient_info in i[1]: for j, n in enumerate(nutrient_info): if nutrient_info[j] != "NA": if result[j] == "NA": result[j] = n else: result[j] += n result_daily_nutrient.append([i[0], *result, complete]) return result_daily_nutrient # ---------------------------------------------------# 6432028521 (point = 31.82%) sim = 77.95% def compute_similarity(food_name1, food_name2): fn_1 = [] fn_2 = [] match = [] for i in food_name1.split(): fn_1.append(i) for j in food_name2.split(): fn_2.append(j) for n1 in range(len(fn_1)): for n2 in range(len(fn_2)): if fn_1[n1] == fn_2[n2]: match.append(fn_1[n1]) similarlity = (len(match)*2) / (len(fn_1) + len(fn_2)) return similarlity # --------------------------------------------------- def match_foods(nutrient, food_name): most_similar_score = 0 matched = [] for food in nutrient: similar_score = compute_similarity(food_name, food[1]) if similar_score > most_similar_score: most_similar_score = similar_score if most_similar_score < 0.5: return [] for food in nutrient: similar_score = compute_similarity(food_name, food[1]) if similar_score == most_similar_score: matched.append(food[0]) return matched # --------------------------------------------------- def get_nutrient(nutrient,food_name): matched_foods = match_foods(nutrient, food_name) nutrient_infos = [] for food in matched_foods: for n in nutrient: if food == n[0]: nutrient_infos.append(n[2:]) if len(nutrient_infos) == 0: return nutrient_infos result_nutrient_infos = ["NA"] * 6 for i in range(6): s = [] for nutrient_info in nutrient_infos: if nutrient_info[i] != "NA": s.append(nutrient_info[i]) if len(s) > 0: result_nutrient_infos[i] = sum(s) / len(s) return result_nutrient_infos # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): summary_daily_nutrient = [] for i in intakes: n = get_nutrient(nutrient, i[1]) current_date = i[0] exist = False for j in summary_daily_nutrient: if current_date == j[0]: exist = True j[1].append(n) if not exist: summary_daily_nutrient.append([current_date, [n]]) summary_daily_nutrient.sort(key=lambda x: x[0], reverse=1) result_daily_nutrient = [] for i in summary_daily_nutrient: result = ["NA"] * 6 complete = i[1].count([]) == 0 for nutrient_info in i[1]: for j, n in enumerate(nutrient_info): if nutrient_info[j] != "NA": if result[j] == "NA": result[j] = n else: result[j] += n result_daily_nutrient.append([i[0], *result, complete]) return result_daily_nutrient # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #7(2)

# 6430343821 (point = 31.82%) sim = 77.64% def compute_similarity(food_name1, food_name2): food_name1 = food_name1.split() food_name2 = food_name2.split() n = 0 for i in food_name1: if i in food_name2: n +=1 return n*2/len(food_name1 + food_name2) # --------------------------------------------------- def match_foods(nutrient, food_name): c = compute_similarity(food_name,nutrient[0][1]) match_food = [] for e in range(len(nutrient)): x = compute_similarity(food_name,nutrient[e][1]) if x > 0.5 and x > c: match_food = [nutrient[e][0]] c = x elif x > 0.5 and x == c: match_food.append(nutrient[e][0]) match_food.sort() return match_food # --------------------------------------------------- def get_nutrient(nutrient,food_name): mfood = match_foods(nutrient, food_name) nutrient1 = [] for food in mfood: for n in nutrient: if food == n[0]: nutrient1.append(n[2:]) if len(nutrient1) == 0: return nutrient1 result_nutrient = ["NA"] * 6 for i in range(6): z = [] for nutrient2 in nutrient1: if nutrient2[i] != "NA": z.append(nutrient2[i]) if len(z) > 0: result_nutrient[i] = sum(z) / len(z) return result_nutrient # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): summ = [] for e in range(len(intakes)): summ.append(intakes[e]) summ.sort() i = 1 ; k = 0 for e in range(len(summ)): k = summ[e][0] summ[e] = get_nutrient(nutrient,summ[e][1]) summ[e].insert(0,k) d = summ[0][0] p = [True] while i != len(summ) : if len(summ[i]) > 1 : if d == summ[i][0]: for x in range(1,7): if summ[i-1][x] != 'NA' and summ[i][x] != 'NA': summ[i-1][x] += summ[i][x] elif summ[i-1][x] == 'NA': summ[i-1][x] = summ[i][x] summ.pop(i) else: d = summ[i][0] i += 1 p.append(True) else: summ.pop(i) p[i-1] = False for c in range(len(summ)): summ[c].append(p[c]) summ = summ[-1::-1] return summ # ---------------------------------------------------# 6432108021 (point = 31.82%) sim = 77.64% def compute_similarity(food_name1, food_name2): food_name1 = food_name1.split() ; food_name2 = food_name2.split() n = 0 for e in range(len(food_name1)): if food_name1[e] in food_name2 : n += 1 similarity = (n*2)/(len(food_name1)+len(food_name2)) return similarity # --------------------------------------------------- def match_foods(nutrient, food_name): c = compute_similarity(food_name,nutrient[0][1]) mf = [] for e in range(len(nutrient)): c1 = compute_similarity(food_name,nutrient[e][1]) if c1 > 0.5 and c1 > c: mf = [nutrient[e][0]] c = c1 elif c1 > 0.5 and c1 == c: mf.append(nutrient[e][0]) mf.sort() return mf # --------------------------------------------------- def get_nutrient(nutrient,food_name): mf = match_foods(nutrient, food_name) gn = [] pas = [] more = [] for e in range(len(nutrient)): pas.append(nutrient[e][0]) if len(mf) == 0 : gn = [] elif len(mf) == 1: gn = nutrient[pas.index(mf[0])][2:] for e in range(len(gn)): if gn[e] != 'NA': gn[e] = float(gn[e]) else: gn = [0]*6 na = 0 for e in range(len(mf)): more.append(nutrient[pas.index(mf[e])][2:]) for i in range(6): for e in range(len(mf)): if more[e][i] != 'NA': gn[i] += more[e][i] else : na += 1 if len(mf)-na != 0: gn[i] = gn[i]/(len(mf)-na) na = 0 if gn[i] == 0 : gn[i] = 'NA' return gn # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): sdi = [] for e in range(len(intakes)): sdi.append(intakes[e]) sdi.sort() i = 1 ; m = 0 for e in range(len(sdi)): m = sdi[e][0] sdi[e] = get_nutrient(nutrient,sdi[e][1]) sdi[e].insert(0,m) d = sdi[0][0] com = [True] while i != len(sdi) : if len(sdi[i]) > 1 : if d == sdi[i][0]: for x in range(1,7): if sdi[i-1][x] != 'NA' and sdi[i][x] != 'NA': sdi[i-1][x] += sdi[i][x] elif sdi[i-1][x] == 'NA': sdi[i-1][x] = sdi[i][x] sdi.pop(i) else: d = sdi[i][0] i += 1 com.append(True) else: sdi.pop(i) com[i-1] = False for c in range(len(sdi)): sdi[c].append(com[c]) sdi = sdi[-1::-1] return sdi # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #8(2)

# 6432009621 (point = 31.82%) sim = 77.44% def compute_similarity(food_name1, food_name2): food_name1=food_name1.split() food_name2=food_name2.split() x=len(food_name1)+len(food_name2) c=0 for i in food_name1: if i in food_name2: c+=1 return c*2/x # --------------------------------------------------- def match_foods(nutrient, food_name): food_id=[] food=[] for i in nutrient: food_list=i[1] if compute_similarity(food_name, food_list)>0.5: food.append([compute_similarity(food_name, food_list),i[0]]) food.sort() food=food[-1::-1] if len(food)==1: food_id+=[food[0][1]] for i in range(len(food)-1): food_id+=[food[i][1]] if food[i][0]!=food[i+1][0]: break food_id.sort() return food_id # --------------------------------------------------- def get_nutrient(nutrient,food_name): food_id=match_foods(nutrient, food_name) food_ans=[] avr=[] for i in nutrient: for e in range(len(food_id)): if food_id[e]==i[0]: food_ans.append(i[2:]) if len(food_ans)>=1: avr=[0]*6 c=[0]*6 for e in food_ans: for n in range(6) : if e[n]!="NA": avr[n]+=e[n] c[n]+=1 for n in range(6): if c[n]==0: avr[n]="NA" else:avr[n]=avr[n]/c[n] return avr # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): date=[] c=0 food=[] total=[] for i in intakes: if i[0] not in date: date+=[i[0]] date.sort() complete=[True]*len(date) for i in date: for e in intakes: if i==e[0]: if get_nutrient(nutrient,e[1])==[]: complete[c]=False else: food.append(get_nutrient(nutrient,e[1])) t=["NA"]*6 for y in food: for e in range(6): if y[e]!="NA": if t[e]=="NA": t[e]=0 t[e]+=y[e] total+=[[i]+t+[complete[c]]] c+=1 return total[-1::-1] # ---------------------------------------------------# 6432016021 (point = 31.82%) sim = 77.44% def compute_similarity(food_name1, food_name2): food_name1 = food_name1.split() food_name2 = food_name2.split() x = len(food_name1+food_name2) count = 0 for i in food_name1: if i in food_name2: count += 1 y =(count*2)/x return y # --------------------------------------------------- def match_foods(nutrient, food_name): food_id = [] food_name1 = food_name y = [] for i in nutrient: food_name2 = i[1] sim = compute_similarity(food_name1, food_name2) if sim > 0.5 : y.append([sim,i[0]]) y.sort() y = y[-1::-1] if len(y) == 1 : food_id += [y[0][1]] for i in range(len(y)-1) : food_id += [y[i][1]] if y[i][0] != y[i+1][0] : break food_id.sort() return food_id # --------------------------------------------------- def get_nutrient(nutrient,food_name): food_id = match_foods(nutrient, food_name) a = [] b = [] for n in nutrient : for i in range(len(food_id)) : if food_id[i] == n[0] : a.append(n[2:]) if len(a) >= 1 : b = [0]*6 count = [0]*6 for i in a : for c in range(6) : if i[c] != 'NA' : b[c] += i[c] count[c] += 1 for c in range(6) : if count[c] == 0 : b[c] = "NA" else : b[c] /= count[c] return b # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): date = [] count = 0 summary = [] daily=[] food=[] for i in intakes : if i[0] not in date : date += [i[0]] date.sort() complete = [True]*len(date) for i in date : food+=[[i]] for n in intakes : if i == n[0] : food_name = n[1] get = get_nutrient(nutrient,food_name) if get == [] : complete[count] = False else : food[count] += [get] count += 1 for a in food: total = ['NA']*6 if len(a)>1: for j in range(len(a)-1): for n in range(6): if a[j+1][n] != 'NA' : if total[n] == 'NA' : total[n] = 0 total[n] += a[j+1][n] daily.append(total) for i in range(len(date)): summary += [[date[i]]+daily[i]+[complete[i]]] return summary[-1::-1] # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #9(3)

# 6430169021 (point = 31.82%) sim = 75.25% # HW6_LIST (ไม่ลบหรือแก้ไขบรรทัดนี้ หรือเพิ่มอะไรก่อนบรรทัดนี้ โดยเด็ดขาด) # - เขียนในเซลล์นี้เท่านั้น # - ถ้าต้องการเขียนฟังก์ชันเพิ่ม ก็เขียนในเซลล์นี้ def compute_similarity(food_name1, food_name2): """ รับ food_name1 และ food_name2 เป็นสตริงเก็บชื่ออาหารที่ตัดคำมาให้แล้ว โดยมีวรรคคั่นระหว่างคำ คืน ค่าความคล้ายของชื่ออาหาร ซึ่งเป็นเลขจำนวนจริงที่มีค่าระหว่าง [0,1] """ count = 0 f1 = food_name1.split() f2 = food_name2.split() for i in range(len(f1)): if f1[i] in f2: count += 1 a = count*2/(len(f1)+len(f2)) return a # --------------------------------------------------- def match_foods(nutrient, food_name): """ รับ nutrient เก็บตารางข้อมูลโภชนาการรูปแบบของลิสต์ที่อธิบายข้างบน รับ food_name เป็นสตริงชื่ออาหารที่ผู้บริโภครับประทานที่ตัดคำมาให้แล้ว โดยมีวรรคคั่นระหว่างคำ คืน ลิสต์ผลลัพธ์ที่เก็บรหัสอาหาร food_id ที่มีชื่ออาหารตรงกับ food_name มากที่สุด รายละเอียดดังอธิบายไว้ข้างบน """ food_id = [] for i in range(len(nutrient)-1): if compute_similarity(food_name,nutrient[i][1]) > 0.5: if compute_similarity(food_name,nutrient[i+1][1]) > compute_similarity(food_name,nutrient[i][1]) : food_id.append(nutrient[i+1][0]) return food_id elif compute_similarity(food_name,nutrient[i+1][1])==compute_similarity(food_name,nutrient[i][1]): food_id = food_id.extend(nutrient[i+1][0]) return food_id.sort() else: return [] # --------------------------------------------------- def get_nutrient(nutrient,food_name): """ รับ nutrient เก็บตารางข้อมูลโภชนาการ ในรูปแบบของลิสต์ รับ food_name เป็นสตริงชื่ออาหาร คืน ลิสต์ผลลัพธ์ที่เก็บข้อมูลพลังงานและสารอาหาร รายละเอียดดังอธิบายไว้ข้างบน """ for i in range(len(nutrient)): if match_foods(nutrient, food_name)==nutrient[i][0]: sum_p.append(nutrient[i][2:7]) if len(sum_p) > 1: for i in range(len(sum_p)): sum_1 += sum_p[i][0] sum_2 += sum_p[i][1] sum_3 += sum_p[i][2] sum_4 += sum_p[i][3] sum_5 += sum_p[i][4] sum_1 = sum_1/len(sum_p) sum_2 = sum_2/len(sum_p) sum_3 = sum_3/len(sum_p) sum_4 = sum_4/len(sum_p) sum_5 = sum_5/len(sum_p) sum_up = [sum_1,sum_2,sum_3,sum_4,sum_5] return sum_up # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): """ รับ nutrient เก็บตารางข้อมูลโภชนาการ ในรูปแบบของลิสต์ที่อธิบายข้างต้น รับ intakes เก็บรายการการรับประทานอาหารของผู้ใช้ ในรูปแบบของลิสต์ที่อธิบายข้างต้น คืน ลิสต์ผลลัพธ์ที่เก็บข้อมูลสรุปโภชนาการสารอาหารที่ได้รับในแต่ละวัน รายละเอียดตามที่อธิบายข้างบน """ # --------------------------------------------------- def main(): nutrient = [['R010007', 'ลาบ หมู', 267, 17, 23, 12, 2, 1470], ['R010014', 'ส้มตำ ไทย', 143, 143, 31, 5, 27, 1064], ['R010005', 'ก๋วยเตี๋ยว ผัดไทย ใส่ ไข่', 447, 49, 21, 18, 'NA', 1139], ['P010019', 'ขนมจีน ซาวน้ำ', 437, 62, 9, 17, 'NA', 810], ['P010021', 'ขนมจีน น้ำยา', 348, 41, 14, 14, 11, 1210], ['P010023', 'ขนมจีน น้ำพริก', 497, 75, 11, 17, 'NA', 'NA'], ['P010041', 'ข้าว หมก ไก่', 481, 74, 19, 12, 'NA', 900], ['P020008', 'ข้าว ราด กะเพรา ไก่', 458, 60, 20, 15, 'NA', 1200], ['P010049', 'ข้าว ไข่ พะโล้', 464, 55, 20, 18, 'NA', 946], ['P010025', 'ข้าว ไก่ ผัด กะเพรา', 432, 54, 20, 15, 'NA', 'NA'], ['F010003', 'กล้วย ไข่', 62, 14, 0, 0, 7, 4], ['D010032', 'บัวลอย เผือก', 336, 62, 2, 9, 'NA', 'NA'], ['D010033', 'ลำใย เผือก', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']] intakes = [['2022/01/15', 'ลาบ หมู'], ['2022/01/15', 'ส้มตำ ไทย'], ['2022/01/18', 'กาแฟ เย็น'], ['2022/01/14', 'แซนวิซ'], ['2022/01/19', 'ซาวน้ำ'], ['2022/01/15', 'ขนมจีน น้ำยา'], ['2022/01/16', 'ขนมจีน น้ำพริก'], ['2022/01/16', 'ส้มตำ ไทย'], ['2022/01/16', 'สลัด ทูน่า'], ['2022/01/18', 'ข้าว ไก่ ผัด กะเพรา'], ['2022/01/18', 'ส้มตำ ไทย'], ['2022/01/18', 'แตงโม'], ['2022/01/14', 'ส้มตำ ไทย'], ['2022/01/14', 'ลาบ หมู'], ['2022/01/19', 'บัวลอย']] print(compute_similarity('ข้าว กะเพรา ไก่', 'ข้าว ไก่ ผัด กะเพรา')) print(match_foods(nutrient, 'ข้าว กะเพรา ไก่')) print(get_nutrient(nutrient,'ห่อ หมก')) print(summarize_daily_intake(nutrient, intakes)) main()# 6430336421 (point = 31.82%) sim = 75.25% # HW6_LIST (ไม่ลบหรือแก้ไขบรรทัดนี้ หรือเพิ่มอะไรก่อนบรรทัดนี้ โดยเด็ดขาด) # - เขียนในเซลล์นี้เท่านั้น # - ถ้าต้องการเขียนฟังก์ชันเพิ่ม ก็เขียนในเซลล์นี้ def compute_similarity(food_name1, food_name2): """ รับ food_name1 และ food_name2 เป็นสตริงเก็บชื่ออาหารที่ตัดคำมาให้แล้ว โดยมีวรรคคั่นระหว่างคำ คืน ค่าความคล้ายของชื่ออาหาร ซึ่งเป็นเลขจำนวนจริงที่มีค่าระหว่าง [0,1] """ fn1 = food_name1.split() fn2 = food_name2.split() num_of_similar = 0 for a in fn1 : for b in fn2 : if a == b : num_of_similar += 1 else : num_of_similar += 0 return(num_of_similar*2 / (lens(fn1)+lens(fn2))) # --------------------------------------------------- def match_foods(nutrient, food_name): """ รับ nutrient เก็บตารางข้อมูลโภชนาการรูปแบบของลิสต์ที่อธิบายข้างบน รับ food_name เป็นสตริงชื่ออาหารที่ผู้บริโภครับประทานที่ตัดคำมาให้แล้ว โดยมีวรรคคั่นระหว่างคำ คืน ลิสต์ผลลัพธ์ที่เก็บรหัสอาหาร food_id ที่มีชื่ออาหารตรงกับ food_name มากที่สุด รายละเอียดดังอธิบายไว้ข้างบน """ output = [] max_similarity = 0 for i in nutrient: similarity = compute_similarity(i[1], food_name) if similarity > 0.5: if similarity == max_similarity: output.append(i[0]) elif similarity > max_similarity: output = [i[0]] max_similarity = similarity return sorted(output)) # --------------------------------------------------- def get_nutrient(nutrient,food_name): """ รับ nutrient เก็บตารางข้อมูลโภชนาการ ในรูปแบบของลิสต์ รับ food_name เป็นสตริงชื่ออาหาร คืน ลิสต์ผลลัพธ์ที่เก็บข้อมูลพลังงานและสารอาหาร รายละเอียดดังอธิบายไว้ข้างบน """ match = match_foods(nutrient, food_name) if len(match) == 0: return match else: match_n = [] for i in match: for n in nutrient: if i == n[0]: match_n.append(n[2:]) sumlist = ['NA']*6 count = [0]*6 for x in match_n: for i in range(len(x)): if sumlist[i] == 'NA': if x[i] != 'NA': sumlist[i] = x[i] count[i] += 1 else: if x[i] != 'NA': sumlist[i] += x[i] count[i] += 1 avg = [] for i in range(len(sumlist)): if sumlist[i] != 'NA': avg.append(sumlist[i]/count[i]) else: avg.append('NA') return avg # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): """ รับ nutrient เก็บตารางข้อมูลโภชนาการ ในรูปแบบของลิสต์ที่อธิบายข้างต้น รับ intakes เก็บรายการการรับประทานอาหารของผู้ใช้ ในรูปแบบของลิสต์ที่อธิบายข้างต้น คืน ลิสต์ผลลัพธ์ที่เก็บข้อมูลสรุปโภชนาการสารอาหารที่ได้รับในแต่ละวัน รายละเอียดตามที่อธิบายข้างบน """ complete = True menubydate = [] for x in intakes: match = match_foods(nutrient, x[1]) if len(match) == 0: complete = False # --------------------------------------------------- def main(): nutrient = [['R010007', 'ลาบ หมู', 267, 17, 23, 12, 2, 1470], ['R010014', 'ส้มตำ ไทย', 143, 143, 31, 5, 27, 1064], ['R010005', 'ก๋วยเตี๋ยว ผัดไทย ใส่ ไข่', 447, 49, 21, 18, 'NA', 1139], ['P010019', 'ขนมจีน ซาวน้ำ', 437, 62, 9, 17, 'NA', 810], ['P010021', 'ขนมจีน น้ำยา', 348, 41, 14, 14, 11, 1210], ['P010023', 'ขนมจีน น้ำพริก', 497, 75, 11, 17, 'NA', 'NA'], ['P010041', 'ข้าว หมก ไก่', 481, 74, 19, 12, 'NA', 900], ['P020008', 'ข้าว ราด กะเพรา ไก่', 458, 60, 20, 15, 'NA', 1200], ['P010049', 'ข้าว ไข่ พะโล้', 464, 55, 20, 18, 'NA', 946], ['P010025', 'ข้าว ไก่ ผัด กะเพรา', 432, 54, 20, 15, 'NA', 'NA'], ['F010003', 'กล้วย ไข่', 62, 14, 0, 0, 7, 4], ['D010032', 'บัวลอย เผือก', 336, 62, 2, 9, 'NA', 'NA'], ['D010033', 'ลำใย เผือก', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']] intakes = [['2022/01/15', 'ลาบ หมู'], ['2022/01/15', 'ส้มตำ ไทย'], ['2022/01/18', 'กาแฟ เย็น'], ['2022/01/14', 'แซนวิซ'], ['2022/01/19', 'ซาวน้ำ'], ['2022/01/15', 'ขนมจีน น้ำยา'], ['2022/01/16', 'ขนมจีน น้ำพริก'], ['2022/01/16', 'ส้มตำ ไทย'], ['2022/01/16', 'สลัด ทูน่า'], ['2022/01/18', 'ข้าว ไก่ ผัด กะเพรา'], ['2022/01/18', 'ส้มตำ ไทย'], ['2022/01/18', 'แตงโม'], ['2022/01/14', 'ส้มตำ ไทย'], ['2022/01/14', 'ลาบ หมู'], ['2022/01/19', 'บัวลอย']] print(compute_similarity('ข้าว กะเพรา ไก่', 'ข้าว ไก่ ผัด กะเพรา')) print(match_foods(nutrient, 'ข้าว กะเพรา ไก่')) print(get_nutrient(nutrient,'ห่อ หมก')) print(summarize_daily_intake(nutrient, intakes)) main()# 6331007021 (point = 31.82%) sim = 74.94% # HW6_LIST (ไม่ลบหรือแก้ไขบรรทัดนี้ หรือเพิ่มอะไรก่อนบรรทัดนี้ โดยเด็ดขาด) # - เขียนในเซลล์นี้เท่านั้น # - ถ้าต้องการเขียนฟังก์ชันเพิ่ม ก็เขียนในเซลล์นี้ def compute_similarity(food_name1, food_name2): """ รับ food_name1 และ food_name2 เป็นสตริงเก็บชื่ออาหารที่ตัดคำมาให้แล้ว โดยมีวรรคคั่นระหว่างคำ คืน ค่าความคล้ายของชื่ออาหาร ซึ่งเป็นเลขจำนวนจริงที่มีค่าระหว่าง [0,1] """ n1 = food_name1.split(" ") n2 = food_name2.split(" ") sum = 0 for i in n1: for j in n2: if(i == j): sum += 1 # print(i,j) return 2*sum/(len(n1)+len(n2)) # --------------------------------------------------- def match_foods(nutrient, food_name): """ รับ nutrient เก็บตารางข้อมูลโภชนาการรูปแบบของลิสต์ที่อธิบายข้างบน รับ food_name เป็นสตริงชื่ออาหารที่ผู้บริโภครับประทานที่ตัดคำมาให้แล้ว โดยมีวรรคคั่นระหว่างคำ คืน ลิสต์ผลลัพธ์ที่เก็บรหัสอาหาร food_id ที่มีชื่ออาหารตรงกับ food_name มากที่สุด รายละเอียดดังอธิบายไว้ข้างบน """ ans = [] for i in nutrient: com = compute_similarity(i[1],food_name) if(com > 0.5): ans.append([com,i[0]]) ans.sort() newans = [] i = len(ans)-1 if( i == 0): return [ans[0][1]] else: while(i>=0 and ans[i][0] == ans[i-1][0]): newans.append(ans[i][1]) newans.append(ans[i-1][1]) # print(i) i -= 1 newans.sort() return newans # --------------------------------------------------- def get_nutrient(nutrient,food_name): """ รับ nutrient เก็บตารางข้อมูลโภชนาการ ในรูปแบบของลิสต์ รับ food_name เป็นสตริงชื่ออาหาร คืน ลิสต์ผลลัพธ์ที่เก็บข้อมูลพลังงานและสารอาหาร รายละเอียดดังอธิบายไว้ข้างบน """ computed = match_foods(nutrient, food_name) # print(computed) n = len(computed) if(n == 0): return [] # elif(n == 1): # for i in nutrient: # if(i[0] == computed[0]): # return i[2:] else: all = [] sol = [] for i in nutrient: for j in computed: if(i[0] == j): # print(i[0]) all.append(i[2:]) for i in range(6): sum = 0 counter = 0 for j in all: # print(j[i]) if(j[i] == 'NA'): continue # print(j[i]) sum +=j[i] counter += 1 if(counter == 0): sol.append('NA') else: sol.append(sum/counter) return sol # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): """ รับ nutrient เก็บตารางข้อมูลโภชนาการ ในรูปแบบของลิสต์ที่อธิบายข้างต้น รับ intakes เก็บรายการการรับประทานอาหารของผู้ใช้ ในรูปแบบของลิสต์ที่อธิบายข้างต้น คืน ลิสต์ผลลัพธ์ที่เก็บข้อมูลสรุปโภชนาการสารอาหารที่ได้รับในแต่ละวัน รายละเอียดตามที่อธิบายข้างบน """ intakes.sort() i = 1 day = intakes[i][0] all = [] while(i < len(intakes)): all = day while(intakes[i][0] == day): computed = compute_similarity(intakes[i]) return intakes # --------------------------------------------------- def main(): nutrient = [['R010007', 'ลาบ หมู', 267, 17, 23, 12, 2, 1470], ['R010014', 'ส้มตำ ไทย', 143, 143, 31, 5, 27, 1064], ['R010005', 'ก๋วยเตี๋ยว ผัดไทย ใส่ ไข่', 447, 49, 21, 18, 'NA', 1139], ['P010019', 'ขนมจีน ซาวน้ำ', 437, 62, 9, 17, 'NA', 810], ['P010021', 'ขนมจีน น้ำยา', 348, 41, 14, 14, 11, 1210], ['P010023', 'ขนมจีน น้ำพริก', 497, 75, 11, 17, 'NA', 'NA'], ['P010041', 'ข้าว หมก ไก่', 481, 74, 19, 12, 'NA', 900], ['P020008', 'ข้าว ราด กะเพรา ไก่', 458, 60, 20, 15, 'NA', 1200], ['P010049', 'ข้าว ไข่ พะโล้', 464, 55, 20, 18, 'NA', 946], ['P010025', 'ข้าว ไก่ ผัด กะเพรา', 432, 54, 20, 15, 'NA', 'NA'], ['F010003', 'กล้วย ไข่', 62, 14, 0, 0, 7, 4], ['D010032', 'บัวลอย เผือก', 336, 62, 2, 9, 'NA', 'NA'], ['D010033', 'ลำใย เผือก', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']] intakes = [['2022/01/15', 'ลาบ หมู'], ['2022/01/15', 'ส้มตำ ไทย'], ['2022/01/18', 'กาแฟ เย็น'], ['2022/01/14', 'แซนวิซ'], ['2022/01/19', 'ซาวน้ำ'], ['2022/01/15', 'ขนมจีน น้ำยา'], ['2022/01/16', 'ขนมจีน น้ำพริก'], ['2022/01/16', 'ส้มตำ ไทย'], ['2022/01/16', 'สลัด ทูน่า'], ['2022/01/18', 'ข้าว ไก่ ผัด กะเพรา'], ['2022/01/18', 'ส้มตำ ไทย'], ['2022/01/18', 'แตงโม'], ['2022/01/14', 'ส้มตำ ไทย'], ['2022/01/14', 'ลาบ หมู'], ['2022/01/19', 'บัวลอย']] # print(compute_similarity('ข้าว กะเพรา ไก่', 'ข้าว ไก่ ผัด กะเพรา')) # print(compute_similarity('กล้วย ไข่', 'ข้าว ไก่ ผัด กะเพรา')) # print(compute_similarity('ข้าว ไข่ พะโล้', 'ข้าว ไก่ ผัด กะเพรา')) # print(compute_similarity('ข้าว ผัด กะเพรา ไก่', 'ข้าว ไก่ ผัด กะเพรา')) # print(match_foods(nutrient, 'ข้าว กะเพรา ไก่')) # print(match_foods(nutrient, 'ห่อ หมก')) # print(get_nutrient(nutrient,'ห่อ หมก')) # print(get_nutrient(nutrient,'บัวลอย เผือก')) # print(get_nutrient(nutrient,'ข้าว กะเพรา ไก่')) # print(get_nutrient(nutrient,'ลำใย เผือก')) print(summarize_daily_intake(nutrient, intakes)) main()

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #10(2)

# 6432182421 (point = 31.82%) sim = 73.36% def compute_similarity(food_name1, food_name2): name1 = food_name1.split() name2 = food_name2.split() count = 0 x = len(name1)+len(name2) for i in name1: for j in name2: if i == j: count += 1 return count*2/x # --------------------------------------------------- def match_foods(nutrient, food_name): matched = list() score = 0 most = 0 s = list() for e in nutrient : score = compute_similarity(e[1],food_name) if score > 0.5: matched += [[score,e[0]]] if score > most: most = score if matched == []: return [] for e in matched : if e[0] == most: s += [e[1]] return s # --------------------------------------------------- def get_nutrient(nutrient,food_name): sum = ["NA","NA","NA","NA","NA","NA"] time =[0,0,0,0,0,0] a = match_foods(nutrient, food_name) y = list() b = 0 if a == []: return [] for i in y: for j in a: if j == i[0]: y += [i] for e in y: e = e[2::] for i in range(len(e)): if e[i] != "NA": if sum[i] != "NA": sum[i] = 0 sum[i] = e[i] time[i] +=1 for i in range(len(sum)): if sum[i] != "NA": sum[i]/=time[i] return sum # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): # ---------------------------------------------------# 6432183021 (point = 31.82%) sim = 73.36% def compute_similarity(food_name1, food_name2): split_food_name1 = food_name1.split() split_food_name2 = food_name2.split() all_word = len(split_food_name1) + len(split_food_name2) how_much_same = 0 for word_food1 in split_food_name1: for word_food2 in split_food_name2: if word_food1 == word_food2: how_much_same += 1 return (how_much_same*2)/all_word # --------------------------------------------------- def match_foods(nutrient, food_name): all = [] for each in nutrient: same_value = compute_similarity(each[1], food_name) if same_value > 0.5: all.append([same_value, each[0]]) if all == []: return [] else: max_value = max(all)[0] all2 = [] for i in all: if i[0] == max_value: all2.append(i[1]) return all2 # --------------------------------------------------- def get_nutrient(nutrient,food_name): match_food = match_foods(nutrient, food_name) if match_food == []: return [] blank = [] for i1 in nutrient: for i2 in match_food: if i2 == i1[0]: blank.append(i1) sum = ['NA']*6 divide = [0]*6 for each in blank: new_each = each[2::] for i in range(len(new_each)): if new_each[i] != 'NA': if sum[i] == 'NA': sum[i] = 0 sum[i] += new_each[i] divide[i] += 1 for i in range(len(sum)): if sum[i] != 'NA': sum[i] = sum[i]/divide[i] return sum # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): # ---------------------------------------------------

compute_similarity,match_foods,get_nutrient,summarize_daily_intake: cluster #11(2)

# 6432025621 (point = 31.82%) sim = 70.98% def compute_similarity(food_name1, food_name2): food_name1 = food_name1.split() food_name2 = food_name2.split() s = len(food_name1) + len(food_name2) m = 0 for i in food_name1 : for j in food_name2 : if i == j : m += 1 return float(2*m/s) # --------------------------------------------------- def match_foods(nutrient, food_name): x = [] for i in nutrient : a = compute_similarity(i[1], food_name) if not(a <= 0.5) : x.append([a,i[1]]) b = sorted(x,reverse = True) if len(b) == 0 : return [] an = [] m = b[0][0] for j in b : if j[0] == m : an.append(j[1]) s = sorted(an) return s # --------------------------------------------------- def get_nutrient(nutrient,food_name): m = match_foods(nutrient,food_name) if len(m) == 0: return [] n = [0,0,0,0,0,0] a = [0,0,0,0,0,0] for i in m : for j in nutrient : if j[0] == i : for k in range(2,len(j)): if j[k] != 'NA' : a[k - 2] += j[k] n[k - 2] += 1 for l in range(len(a)) : if n[l] == 0 : a[l] = 'NA' else : a[l] = float(a[l]/n[l]) return a # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): iu = sorted(intakes,reverse = True) ans = [] apd = ['NA']*6 com = True day = iu[0][0] for c in iu : if c[0] == day : x = get_nutrient(nutrient, c[1]) if len(x) == 0 : com = True else : apd = ['NA']*6 com = False day = c[0] ans.append([day,apd[0],apd[1],apd[2],apd[3],apd[4],apd[5],com]) return ans # ---------------------------------------------------# 6432036521 (point = 31.82%) sim = 70.98% def compute_similarity(food_name1, food_name2): x = food_name1.split() y = food_name2.split() all = len(x) + len(y) exist = 0 for c in x: for d in y: if c == d : exist += 1 break return float(2*exist/all) # --------------------------------------------------- def match_foods(nutrient, food_name): x = [] for c in nutrient: a = compute_similarity(c[1], food_name) if a > 0.5 : x.append([a, c[0]]) x.sort(reverse = True) if len(x) == 0 : return [] ans = [] m = x[0][0] for c in x: if c[0] == m : ans.append(c[1]) ans.sort() return ans # --------------------------------------------------- def get_nutrient(nutrient,food_name): menu = match_foods(nutrient, food_name) if len(menu) == 0 : return [] num = [0,0,0,0,0,0] ans = [0,0,0,0,0,0] for c in menu: for d in nutrient: if d[0] == c : for i in range(2,len(d)): if d[i] != 'NA' : ans[i - 2] += d[i] num[i - 2] += 1 break for i in range(len(ans)): if num[i] == 0 : ans[i] = 'NA' else: ans[i] = float(ans[i]/num[i]) return ans # --------------------------------------------------- def summarize_daily_intake(nutrient, intakes): intakes2 = sorted(intakes, reverse = True) ans = [] apd = ['NA']*6 complete = True day = intakes2[0][0] for c in intakes2: if c[0] == day: x = get_nutrient(nutrient, c[1]) if len(x)== 0 : complete = False else: for i in range(len(apd)): if x[i] != 'NA': if apd[i] == 'NA': apd[i] = x[i] else: apd[i] += x[i] else: ans.append([day,apd[0],apd[1],apd[2],apd[3],apd[4],apd[5],complete]) apd = get_nutrient(nutrient, c[1]) if len(apd) != 0 : complete = True else: apd = ['NA']*6 complete = False day = c[0] ans.append([day,apd[0],apd[1],apd[2],apd[3],apd[4],apd[5],complete]) return ans # ---------------------------------------------------