小型鸡尾酒推荐系统运行记录(github项目)

文章介绍了一个基于Python的鸡尾酒推荐系统,该系统使用XML解析用户输入和鸡尾酒数据库,通过计算用户选择的成分与已有鸡尾酒的相似度来推荐饮品。修复后的代码能正确运行,但作者发现该项目的相似度计算方法对实际推荐帮助不大。

前言
在github上想找案例推理实现代码,找到一个简介是基于CBR的鸡尾酒推荐代码,项目链接:vizkids/Case-Based-Reasoning-using-Python 。项目源文件里有几处错误,我修改了一下已经跑通了,大概的记录一下代码细节,但这个项目对我并没有帮助 😦

文件目录

  • static:存储web界面样式设置文件style.css
  • templates:存储web界面样式文件,home.html设置首页(Home)界面,makeCocktail.html设置用户输入鸡尾酒原材料界面(Suggest Cocktail),adaptCocktail.html设置修改已有鸡尾酒组成成分界面(Modify Cocktail)。
  • adapt.txt:保存需要修改的鸡尾酒相关信息。
  • alcoholicLiqueurs:保存含酒精类酒之间的相似度。
  • cocktail_recipe_lib:保存所有鸡尾酒的组成成分,一种鸡尾酒的信息占一行。
  • cocktailList.py:保存所有实现代码。
  • cocktails.xml:保存所有鸡尾酒(108个)信息的库,包括鸡尾酒名称、组成成分和制作步骤。
  • fruit/fruit.txt:保存水果之间的相似度。
  • input.txt:保存用户选择鸡尾酒组成成分。
  • noalcoholicLiqueurs:保存不含酒精类酒之间的相似度。
  • others:保存其他组成成分之间的相似度。
  • output.txt:系统推荐的前三个鸡尾酒,包括鸡尾酒名称,与已知鸡尾酒的相似度和含酒精多少。
  • routes.py:保存Web界面相关设置。
  • tasteEnhancers:保存tasteEnhancers之间的相似度。
  • vegetables:保存蔬菜之间的相似度。

需要修改的文件是 cocktailList.py,压缩包里其他文件不需要修改

# coding: utf-8
# time:2023.03.31
# reeditor: 早知晓

from xml.dom import minidom
from lxml import etree
import os

# 接收某鸡尾酒名称,返回鸡尾酒的原材料列表
def getCocktailIngredient(cocktailname) :
	tree = etree.parse('cocktails.xml')    # 利用etree.parse解析存储鸡尾酒信息的xml文件,获取xml中的内容
	root = tree.getroot()       # 获取根标签recipes,而不是格式声明内容
	titlelist = root.findall('recipe/title')    # 获取所有鸡尾酒名称标签title
	titleValue = cocktailname
	ilist=[] 
	titleRoot = getTitleRoot(titlelist,titleValue)
	for node in titleRoot.getchildren():
			if node.tag == 'ingredients':
				ingredientsList = node.getchildren()
				for i in ingredientsList:
					ilist.append(i.text)
		
	return ilist

# 接收某鸡尾酒名称,返回鸡尾酒的制作步骤列表
def getCocktailPreparation(cocktailname) :
	tree = etree.parse('cocktails.xml')
	root = tree.getroot()
	titlelist = root.findall('recipe/title')
	titleValue = cocktailname
	plist=[] 
	titleRoot = getTitleRoot(titlelist,titleValue)
	for node in titleRoot.getchildren():
		if node.tag == "preparation":
			ingredientsList = node.getchildren()
			for i in ingredientsList:
				plist.append(i.text)
	return plist
	
def getTitleRoot(titlelist,titleValue):
	for title in titlelist:
		if title.text == titleValue:
			titleRoot = title.getparent()   # 返回title节点
			return titleRoot

def getCocktailList():
	xmldoc = minidom.parse('cocktails.xml')
	cocktaillist = xmldoc.getElementsByTagName('title')   # 获取所有title元素
	cocktaillist1 ={
   
   cocktaillist[1],cocktaillist[2],cocktaillist[3]}
	return cocktaillist1

def get_xmltext(parent, subnode_name):
	node = parent.getElementsByTagName(subnode_name)[0]
	return "".join([ch.toxml() for ch in node.childNodes])

# 将用户输入选项保存至input.txt文件中
def putCocktails(fruits,vegetables,alcoholicLiqueurs,nonalcoholicLiqueurs,tasteEnhancers,others):
	list = ",".join(others)
	text_file = open("input.txt", "w")
	text_file.write("fruit:%s" % fruits+'\n')
	text_file.write("vegetables:%s" % vegetables+'\n')
	text_file.write("alcoholicLiqueurs:%s" % alcoholicLiqueurs+'\n')
	
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早知晓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值