Python+Django的毕业设计-基于深度学习的酒店评论文本情感分析系统项目实战(附源码+论文)
大家好!我是岛上程序猿,感谢您阅读本文,欢迎一键三连哦。
精彩专栏推荐👇🏻👇🏻👇🏻
开发环境
开发语言:Python
框架:django
Python版本:python3.7.7
数据库:mysql 5.7
数据库工具:Navicat11
开发软件:PyCharm
浏览器:谷歌浏览器
论文目录
【如需全文请按文末获取联系】

一、项目简介
本研究开发了一套基于深度学习的酒店评论文本情感分析系统,旨在通过先进的技术手段实现对酒店评论情感倾向的精准分析和预测。系统采用 Python 语言开发,结合 Django 框架构建高效稳定的后端服务,并利用 MySQL 数据库进行数据存储与管理。引入 Scrapy 爬虫技术用于采集评论数据,借助 Hadoop 大数据平台实现数据的高效处理与存储。系统功能丰富,包括主页展示、用户管理、酒店信息查询、情感分析、酒店信息预测、留言反馈、系统管理和个人中心等模块。情感分析模块通过深度学习算法对评论文本进行情感分类,能够准确识别正面、负面和中性情感。酒店信息预测模块则基于历史评论数据和情感分析结果,预测酒店的未来发展趋势。留言反馈功能为用户提供了一个表达意见和建议的平台,系统管理模块则保障了数据的安全性和系统的稳定性。
二、系统设计
2.1软件功能模块设计
在系统功能结构设计中,我们以用户需求为出发点,将系统划分为管理员和用户两大核心模块。设计上注重操作的便捷性和数据的准确性,以确保用户能够高效地处理信息。管理员模块赋予了对数据的全面管理和审计能力,以维护系统的安全性和数据的完整性。其他用户模块则提供了一个直观且友好的界面,满足了用户日常的信息管理和查询需求。整体设计目标是提升用户的操作体验,并确保系统的稳定性和可靠性。系统各功能划分结构如图4-1所示。
2.2数据库设计
本系统的整体E-R实体属性如图4-2所示。
三、系统项目部分截图
3.1系统前台功能实现
本系统前台功能丰富,首页提供系统概览与核心功能导航;酒店信息模块支持通过名称、城市或地址查询酒店,用户可查看酒店详情、进行收藏或评论,系统基于深度学习分析情感倾向,为用户决策提供参考。公告信息模块展示最新动态与通知;留言反馈模块供用户提交意见与建议,增强互动性。系统首页页面如图5-1所示:
在酒店信息模块中,用户可以通过输入酒店名称、城市或地址进行精准查询。查询结果页面展示酒店列表,每项包含酒店名称、位置、评分和简要评价。用户可点击进入详情页,查看酒店设施、服务、价格及用户评论,并进行情感分析结果查看。用户还可对酒店进行收藏或发表评论,方便后续预订和分享体验。酒店信息页面如图5-2所示:
个人中心包含个人资料管理、密码修改及我的收藏功能,方便用户个性化使用,提升用户体验。个人中心页面如图5-3所示:
3.2管理员功能实现
管理员主页面作为系统控制中心,提供全面的管理功能。页面通常详细列出所有管理模块,包括主页、用户、酒店信息、情感分析、酒店信息预测、留言反馈、系统管理、个人中心等,确保管理员能够高效地进行日常管理工作。整个页面布局清晰,功能模块化,便于管理员快速定位和操作。管理员主页界面如图5-4所示:
在系统后台的用户管理模块中,管理员可以通过点击“用户”选项进入管理界面。管理员能够通过输入用户账号或姓名进行精准查询,快速定位目标用户。系统支持添加新用户,完善用户信息列表;管理员可以删除不再需要的用户记录,保持用户数据的整洁性。对于每个用户,管理员还能够查看其详细信息,包括注册信息、操作记录等。管理员可以对用户信息进行修改,以确保数据的准确性和时效性,也可以删除用户详细信息,以符合数据管理规范和隐私要求。这些功能为管理员提供了全面的用户管理能力,确保系统的高效运行和用户数据的安全性。用户如图5-5所示:
管理员点击“情感分析”模块后,进入情感分析管理界面。管理员可以输入特定文本(如酒店评论)进行情感倾向查询,系统会基于深度学习模型即时返回情感分析结果(正面、负面或中性)。管理员还可以添加新的文本样本及其情感标签,用于扩充训练数据集或测试模型性能。管理员能够查看情感分析的详细记录,包括文本内容、情感分类结果、分析时间等信息,并可对这些记录进行修改或删除操作,以确保情感分析数据的准确性和完整性。这些功能为管理员提供了全面的情感分析管理能力,有助于优化情感分析模型的性能和应用效果。情感分析如图5-7所示:
该看板为系统管理员提供全面的可视化数据概览。通过酒店词云直观展示评论高频词汇,反映用户关注点;现价统计和原价统计呈现酒店价格动态变化。显示用户总数、酒店信息总数、酒店预测总数,帮助管理员掌握系统数据规模。评价统计、城市统计和点评统计则从不同维度分析用户行为和酒店分布,为决策提供数据支持,提升管理效率和精准度。看板界面如图5-10所示:
四、部分核心代码
#coding:utf-8
import base64, copy, logging, os, sys, time, xlrd, json, datetime, configparser
from django.http import JsonResponse
from django.apps import apps
import numbers
from django.db.models.aggregates import Count,Sum
from django.db.models import Case, When, IntegerField, F
from django.forms import model_to_dict
import requests
from util.CustomJSONEncoder import CustomJsonEncoder
from .models import hotelinfo
from util.codes import *
from util.auth import Auth
from util.common import Common
import util.message as mes
from django.db import connection
import random
from django.core.mail import send_mail
from django.conf import settings
from django.shortcuts import redirect
from django.db.models import Q
from util.baidubce_api import BaiDuBce
from .config_model import config
import pandas as pd
def hotelinfo_register(request):
if request.method in ["POST", "GET"]:
msg = {'code': normal_code, "msg": mes.normal_code}
req_dict = request.session.get("req_dict")
error = hotelinfo.createbyreq(hotelinfo, hotelinfo, req_dict)
if error is Exception:
msg['code'] = crud_error_code
msg['msg'] = "用户已存在,请勿重复注册!"
else:
msg['data'] = error
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_login(request):
if request.method in ["POST", "GET"]:
msg = {'code': normal_code, "msg": mes.normal_code}
req_dict = request.session.get("req_dict")
datas = hotelinfo.getbyparams(hotelinfo, hotelinfo, req_dict)
if not datas:
msg['code'] = password_error_code
msg['msg'] = mes.password_error_code
return JsonResponse(msg, encoder=CustomJsonEncoder)
try:
__sfsh__= hotelinfo.__sfsh__
except:
__sfsh__=None
if __sfsh__=='是':
if datas[0].get('sfsh')!='是':
msg['code']=other_code
msg['msg'] = "账号已锁定,请联系管理员审核!"
return JsonResponse(msg, encoder=CustomJsonEncoder)
req_dict['id'] = datas[0].get('id')
return Auth.authenticate(Auth, hotelinfo, req_dict)
def hotelinfo_logout(request):
if request.method in ["POST", "GET"]:
msg = {
"msg": "登出成功",
"code": 0
}
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_resetPass(request):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code}
req_dict = request.session.get("req_dict")
columns= hotelinfo.getallcolumn( hotelinfo, hotelinfo)
try:
__loginUserColumn__= hotelinfo.__loginUserColumn__
except:
__loginUserColumn__=None
username=req_dict.get(list(req_dict.keys())[0])
if __loginUserColumn__:
username_str=__loginUserColumn__
else:
username_str=username
if 'mima' in columns:
password_str='mima'
else:
password_str='password'
init_pwd = '123456'
recordsParam = {}
recordsParam[username_str] = req_dict.get("username")
records=hotelinfo.getbyparams(hotelinfo, hotelinfo, recordsParam)
if len(records)<1:
msg['code'] = 400
msg['msg'] = '用户不存在'
return JsonResponse(msg, encoder=CustomJsonEncoder)
eval('''hotelinfo.objects.filter({}='{}').update({}='{}')'''.format(username_str,username,password_str,init_pwd))
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_session(request):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code,"msg": mes.normal_code, "data": {}}
req_dict={"id":request.session.get('params').get("id")}
msg['data'] = hotelinfo.getbyparams(hotelinfo, hotelinfo, req_dict)[0]
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_default(request):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code,"msg": mes.normal_code, "data": {}}
req_dict = request.session.get("req_dict")
req_dict.update({"isdefault":"是"})
data=hotelinfo.getbyparams(hotelinfo, hotelinfo, req_dict)
if len(data)>0:
msg['data'] = data[0]
else:
msg['data'] = {}
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_page(request):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
req_dict = request.session.get("req_dict")
global hotelinfo
#获取全部列名
columns= hotelinfo.getallcolumn( hotelinfo, hotelinfo)
if "vipread" in req_dict and "vipread" not in columns:
del req_dict["vipread"]
#当前登录用户所在表
tablename = request.session.get("tablename")
'''__authSeparate__此属性为真,params添加userid,后台只查询个人数据'''
try:
__authSeparate__=hotelinfo.__authSeparate__
except:
__authSeparate__=None
if __authSeparate__=="是":
tablename=request.session.get("tablename")
if tablename!="users" and 'userid' in columns and 'userid' not in req_dict:
try:
req_dict['userid']=request.session.get("params").get("id")
except:
pass
#当项目属性hasMessage为”是”,生成系统自动生成留言板的表messages,同时该表的表属性hasMessage也被设置为”是”,字段包括userid(用户id),username(用户名),content(留言内容),reply(回复)
#接口page需要区分权限,普通用户查看自己的留言和回复记录,管理员查看所有的留言和回复记录
try:
__hasMessage__=hotelinfo.__hasMessage__
except:
__hasMessage__=None
if __hasMessage__=="是":
tablename=request.session.get("tablename")
if tablename!="users":
req_dict["userid"]=request.session.get("params").get("id")
# 判断当前表的表属性isAdmin,为真则是管理员表
# 当表属性isAdmin=”是”,刷出来的用户表也是管理员,即page和list可以查看所有人的考试记录(同时应用于其他表)
__isAdmin__ = None
allModels = apps.get_app_config('main').get_models()
for m in allModels:
if m.__tablename__==tablename:
try:
__isAdmin__ = m.__isAdmin__
except:
__isAdmin__ = None
break
# 当前表也是有管理员权限的表
if __isAdmin__ == "是" and 'hotelinfo' != 'forum' :
if req_dict.get("userid") and 'hotelinfo' != 'chat' and 'hotelinfo' != 'examrecord':
del req_dict["userid"]
else:
if tablename!="users" and tablename!="jdfnl" and 'hotelinfo'[:7]!='discuss' and "userid" in hotelinfo.getallcolumn(hotelinfo,hotelinfo):
req_dict["userid"] = request.session.get("params").get("id")
#当列属性authTable有值(某个用户表)[该列的列名必须和该用户表的登陆字段名一致],则对应的表有个隐藏属性authTable为”是”,那么该用户查看该表信息时,只能查看自己的
try:
__authTables__=hotelinfo.__authTables__
except:
__authTables__=None
if __authTables__!=None and __authTables__!={} and __isAdmin__ == "是":
for authColumn,authTable in __authTables__.items():
if authTable==tablename:
params = request.session.get("params")
req_dict[authColumn]=params.get(authColumn)
username=params.get(authColumn)
break
q = Q()
msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
msg['data']['pageSize'] =hotelinfo.page(hotelinfo, hotelinfo, req_dict, request, q)
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_autoSort(request):
'''
.智能推荐功能(表属性:[intelRecom(是/否)],新增clicktime[前端不显示该字段]字段(调用info/detail接口的时候更新),按clicktime排序查询)
主要信息列表(如商品列表,新闻列表)中使用,显示最近点击的或最新添加的5条记录就行
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
req_dict = request.session.get("req_dict")
if "clicknum" in hotelinfo.getallcolumn(hotelinfo,hotelinfo):
req_dict['sort']='clicknum'
elif "browseduration" in hotelinfo.getallcolumn(hotelinfo,hotelinfo):
req_dict['sort']='browseduration'
else:
req_dict['sort']='clicktime'
req_dict['order']='desc'
msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
msg['data']['pageSize'] = hotelinfo.page(hotelinfo,hotelinfo, req_dict)
return JsonResponse(msg, encoder=CustomJsonEncoder)
#分类列表
def hotelinfo_lists(request):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data":[]}
msg['data'],_,_,_,_ = hotelinfo.page(hotelinfo, hotelinfo, {})
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_query(request):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
try:
query_result = hotelinfo.objects.filter(**request.session.get("req_dict")).values()
msg['data'] = query_result[0]
except Exception as e:
msg['code'] = crud_error_code
msg['msg'] = f"发生错误:{e}"
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_list(request):
'''
前台分页
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":10,"list":[]}}
req_dict = request.session.get("req_dict")
#获取全部列名
columns= hotelinfo.getallcolumn( hotelinfo, hotelinfo)
if "vipread" in req_dict and "vipread" not in columns:
del req_dict["vipread"]
#表属性[foreEndList]前台list:和后台默认的list列表页相似,只是摆在前台,否:指没有此页,是:表示有此页(不需要登陆即可查看),前要登:表示有此页且需要登陆后才能查看
try:
__foreEndList__=hotelinfo.__foreEndList__
except:
__foreEndList__=None
try:
__foreEndListAuth__=hotelinfo.__foreEndListAuth__
except:
__foreEndListAuth__=None
#authSeparate
try:
__authSeparate__=hotelinfo.__authSeparate__
except:
__authSeparate__=None
if __foreEndListAuth__ =="是" and __authSeparate__=="是":
tablename=request.session.get("tablename")
if tablename!="users" and request.session.get("params") is not None:
req_dict['userid']=request.session.get("params").get("id")
tablename = request.session.get("tablename")
if tablename == "users" and req_dict.get("userid") != None:#判断是否存在userid列名
del req_dict["userid"]
else:
__isAdmin__ = None
allModels = apps.get_app_config('main').get_models()
for m in allModels:
if m.__tablename__==tablename:
try:
__isAdmin__ = m.__isAdmin__
except:
__isAdmin__ = None
break
if __isAdmin__ == "是":
if req_dict.get("userid"):
# del req_dict["userid"]
pass
else:
#非管理员权限的表,判断当前表字段名是否有userid
if "userid" in columns:
try:
pass
except:
pass
#当列属性authTable有值(某个用户表)[该列的列名必须和该用户表的登陆字段名一致],则对应的表有个隐藏属性authTable为”是”,那么该用户查看该表信息时,只能查看自己的
try:
__authTables__=hotelinfo.__authTables__
except:
__authTables__=None
if __authTables__!=None and __authTables__!={} and __foreEndListAuth__=="是":
for authColumn,authTable in __authTables__.items():
if authTable==tablename:
try:
del req_dict['userid']
except:
pass
params = request.session.get("params")
req_dict[authColumn]=params.get(authColumn)
username=params.get(authColumn)
break
if hotelinfo.__tablename__[:7]=="discuss":
try:
del req_dict['userid']
except:
pass
q = Q()
msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
msg['data']['pageSize'] = hotelinfo.page(hotelinfo, hotelinfo, req_dict, request, q)
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_save(request):
'''
后台新增
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
req_dict = request.session.get("req_dict")
if 'clicktime' in req_dict.keys():
del req_dict['clicktime']
tablename=request.session.get("tablename")
__isAdmin__ = None
allModels = apps.get_app_config('main').get_models()
for m in allModels:
if m.__tablename__==tablename:
try:
__isAdmin__ = m.__isAdmin__
except:
__isAdmin__ = None
break
#获取全部列名
columns= hotelinfo.getallcolumn( hotelinfo, hotelinfo)
if tablename!='users' and req_dict.get("userid")!=None and 'userid' in columns and __isAdmin__!='是':
params=request.session.get("params")
req_dict['userid']=params.get('id')
if 'addtime' in req_dict.keys():
del req_dict['addtime']
idOrErr= hotelinfo.createbyreq(hotelinfo,hotelinfo, req_dict)
if idOrErr is Exception:
msg['code'] = crud_error_code
msg['msg'] = idOrErr
else:
msg['data'] = idOrErr
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_add(request):
'''
前台新增
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
req_dict = request.session.get("req_dict")
tablename=request.session.get("tablename")
#获取全部列名
columns= hotelinfo.getallcolumn( hotelinfo, hotelinfo)
try:
__authSeparate__=hotelinfo.__authSeparate__
except:
__authSeparate__=None
if __authSeparate__=="是":
tablename=request.session.get("tablename")
if tablename!="users" and 'userid' in columns:
try:
req_dict['userid']=request.session.get("params").get("id")
except:
pass
try:
__foreEndListAuth__=hotelinfo.__foreEndListAuth__
except:
__foreEndListAuth__=None
if __foreEndListAuth__ and __foreEndListAuth__!="否":
tablename=request.session.get("tablename")
if tablename!="users":
req_dict['userid']=request.session.get("params").get("id")
if 'addtime' in req_dict.keys():
del req_dict['addtime']
error= hotelinfo.createbyreq(hotelinfo,hotelinfo, req_dict)
if error is Exception:
msg['code'] = crud_error_code
msg['msg'] = error
else:
msg['data'] = error
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_thumbsup(request,id_):
'''
点赞:表属性thumbsUp[是/否],刷表新增thumbsupnum赞和crazilynum踩字段,
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
req_dict = request.session.get("req_dict")
id_=int(id_)
type_=int(req_dict.get("type",0))
rets=hotelinfo.getbyid(hotelinfo,hotelinfo,id_)
update_dict={
"id":id_,
}
if type_==1:#赞
update_dict["thumbsupnum"]=int(rets[0].get('thumbsupnum'))+1
elif type_==2:#踩
update_dict["crazilynum"]=int(rets[0].get('crazilynum'))+1
error = hotelinfo.updatebyparams(hotelinfo,hotelinfo, update_dict)
if error!=None:
msg['code'] = crud_error_code
msg['msg'] = error
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_info(request,id_):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
data = hotelinfo.getbyid(hotelinfo,hotelinfo, int(id_))
if len(data)>0:
msg['data']=data[0]
if msg['data'].__contains__("reversetime"):
if isinstance(msg['data']['reversetime'], datetime.datetime):
msg['data']['reversetime'] = msg['data']['reversetime'].strftime("%Y-%m-%d %H:%M:%S")
else:
if msg['data']['reversetime'] != None:
reversetime = datetime.datetime.strptime(msg['data']['reversetime'], '%Y-%m-%d %H:%M:%S')
msg['data']['reversetime'] = reversetime.strftime("%Y-%m-%d %H:%M:%S")
#浏览点击次数
try:
__browseClick__= hotelinfo.__browseClick__
except:
__browseClick__=None
if __browseClick__=="是" and "clicknum" in hotelinfo.getallcolumn(hotelinfo,hotelinfo):
try:
clicknum=int(data[0].get("clicknum",0))+1
except:
clicknum=0+1
click_dict={"id":int(id_),"clicknum":clicknum,"clicktime":datetime.datetime.now()}
ret=hotelinfo.updatebyparams(hotelinfo,hotelinfo,click_dict)
if ret!=None:
msg['code'] = crud_error_code
msg['msg'] = ret
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_detail(request,id_):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
data =hotelinfo.getbyid(hotelinfo,hotelinfo, int(id_))
if len(data)>0:
msg['data']=data[0]
if msg['data'].__contains__("reversetime"):
if isinstance(msg['data']['reversetime'], datetime.datetime):
msg['data']['reversetime'] = msg['data']['reversetime'].strftime("%Y-%m-%d %H:%M:%S")
else:
if msg['data']['reversetime'] != None:
reversetime = datetime.datetime.strptime(msg['data']['reversetime'], '%Y-%m-%d %H:%M:%S')
msg['data']['reversetime'] = reversetime.strftime("%Y-%m-%d %H:%M:%S")
#浏览点击次数
try:
__browseClick__= hotelinfo.__browseClick__
except:
__browseClick__=None
if __browseClick__=="是" and "clicknum" in hotelinfo.getallcolumn(hotelinfo,hotelinfo):
try:
clicknum=int(data[0].get("clicknum",0))+1
except:
clicknum=0+1
click_dict={"id":int(id_),"clicknum":clicknum,"clicktime":datetime.datetime.now()}
ret=hotelinfo.updatebyparams(hotelinfo,hotelinfo,click_dict)
if ret!=None:
msg['code'] = crud_error_code
msg['msg'] = ret
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_update(request):
'''
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
req_dict = request.session.get("req_dict")
if 'clicktime' in req_dict.keys() and req_dict['clicktime']=="None":
del req_dict['clicktime']
if req_dict.get("mima") and "mima" not in hotelinfo.getallcolumn(hotelinfo,hotelinfo) :
del req_dict["mima"]
if req_dict.get("password") and "password" not in hotelinfo.getallcolumn(hotelinfo,hotelinfo) :
del req_dict["password"]
try:
del req_dict["clicknum"]
except:
pass
error = hotelinfo.updatebyparams(hotelinfo, hotelinfo, req_dict)
if error!=None:
msg['code'] = crud_error_code
msg['msg'] = error
return JsonResponse(msg)
def hotelinfo_delete(request):
'''
批量删除
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code, "data": {}}
req_dict = request.session.get("req_dict")
error=hotelinfo.deletes(hotelinfo,
hotelinfo,
req_dict.get("ids")
)
if error!=None:
msg['code'] = crud_error_code
msg['msg'] = error
return JsonResponse(msg)
def hotelinfo_vote(request,id_):
'''
浏览点击次数(表属性[browseClick:是/否],点击字段(clicknum),调用info/detail接口的时候后端自动+1)、投票功能(表属性[vote:是/否],投票字段(votenum),调用vote接口后端votenum+1)
统计商品或新闻的点击次数;提供新闻的投票功能
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": mes.normal_code}
data= hotelinfo.getbyid(hotelinfo, hotelinfo, int(id_))
for i in data:
votenum=i.get('votenum')
if votenum!=None:
params={"id":int(id_),"votenum":votenum+1}
error=hotelinfo.updatebyparams(hotelinfo,hotelinfo,params)
if error!=None:
msg['code'] = crud_error_code
msg['msg'] = error
return JsonResponse(msg)
def hotelinfo_importExcel(request):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
excel_file = request.FILES.get("file", "")
file_type = excel_file.name.split('.')[1]
if file_type in ['xlsx', 'xls']:
data = xlrd.open_workbook(filename=None, file_contents=excel_file.read())
table = data.sheets()[0]
rows = table.nrows
try:
for row in range(1, rows):
row_values = table.row_values(row)
req_dict = {}
hotelinfo.createbyreq(hotelinfo, hotelinfo, req_dict)
except:
pass
else:
msg = {
"msg": "文件类型错误",
"code": 500
}
return JsonResponse(msg)
import math
#查找相似用户
def cosine_similarity(a, b):
numerator = sum([a[key] * b[key] for key in a if key in b])
denominator = math.sqrt(sum([a[key]**2 for key in a])) * math.sqrt(sum([b[key]**2 for key in b]))
return numerator / denominator
#收藏协同算法
def hotelinfo_autoSort2(request):
if request.method in ["POST", "GET"]:
req_dict = request.session.get("req_dict")
cursor = connection.cursor()
sorted_recommended_goods=[]
user_ratings={}
try:
cursor.execute("select * from storeup where type = 1 and tablename = 'hotelinfo' order by addtime desc")
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
#用户-订单矩阵
for item in data_dict:
if user_ratings.__contains__(item["userid"]):
ratings_dict = user_ratings[item["userid"]]
if ratings_dict.__contains__(item["refid"]):
ratings_dict[str(item["refid"])]+=1
else:
ratings_dict[str(item["refid"])] =1
else:
user_ratings[item["userid"]] = {
str(item["refid"]):1
}
try:
# 计算目标用户与其他用户的相似度
similarities = {other_user: cosine_similarity(user_ratings[request.session.get("params").get("id")], user_ratings[other_user])
for other_user in user_ratings if other_user != request.session.get("params").get("id")}
# 找到与目标用户最相似的用户
most_similar_user = sorted(similarities, key=similarities.get, reverse=True)[0]
# 找到最相似但目标用户未购买过的商品
recommended_goods = {goods: rating for goods, rating in user_ratings[most_similar_user].items() if
goods not in user_ratings[request.session.get("params").get("id")]}
# 按评分降序排列推荐
sorted_recommended_goods = sorted(recommended_goods, key=recommended_goods.get, reverse=True)
except:
pass
except:
sorted_recommended_goods=[]
L = []
where = " AND ".join([f"{key} = '{value}'" for key, value in req_dict.items() if key!="page" and key!="limit" and key!="order"and key!="sort"])
if where:
sql = f'''SELECT * FROM (SELECT * FROM hotelinfo WHERE {where}) AS table1 WHERE id IN ('{"','".join(sorted_recommended_goods)}') union all SELECT * FROM (SELECT * FROM hotelinfo WHERE {where}) AS table1 WHERE id NOT IN ('{"','".join(sorted_recommended_goods)}')'''
else:
sql = f'''select * from hotelinfo where id in ('{"','".join(sorted_recommended_goods)}') union all select * from hotelinfo where id not in('{"','".join(sorted_recommended_goods)}')'''
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
return JsonResponse({"code": 0, "msg": '', "data":{"currPage":1,"totalPage":1,"total":1,"pageSize":5,"list": L[0:int(req_dict["limit"])]}}, encoder=CustomJsonEncoder)
def hotelinfo_count(request):
'''
总数接口
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
req_dict = request.session.get("req_dict")
where = ' where 1 = 1 '
for key in req_dict:
if req_dict[key] != None:
where = where + " and key like '{0}'".format(req_dict[key])
sql = "SELECT count(*) AS count FROM hotelinfo {0}".format(where)
count = 0
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
count = online_dict['count']
msg['data'] = count
return JsonResponse(msg, encoder=CustomJsonEncoder)
# (按值统计)时间统计类型
def hotelinfo_value(request, xColumnName, yColumnName, timeStatType):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
#获取hadoop分析后的数据文件
date_type = ""
if timeStatType == '日':
date_type = "date"
if timeStatType == '月':
date_type = "month"
if timeStatType == '季':
date_type = "quarter"
if timeStatType == '年':
date_type = "year"
json_filename = f'hotelinfo_value{xColumnName}{yColumnName}{date_type}.json'
if os.path.exists(json_filename) == True:
with open(json_filename, encoding='utf-8') as f:
msg['data'] = json.load(f)
else:
where = ' where 1 = 1 '
sql = ''
if timeStatType == '日':
sql = "SELECT DATE_FORMAT({0}, '%Y-%m-%d') {0}, ROUND(sum({1}),2) total FROM hotelinfo {2} GROUP BY DATE_FORMAT({0}, '%Y-%m-%d')".format(xColumnName, yColumnName, where, '%Y-%m-%d')
if timeStatType == '月':
sql = "SELECT DATE_FORMAT({0}, '%Y-%m') {0}, ROUND(sum({1}),2) total FROM hotelinfo {2} GROUP BY DATE_FORMAT({0}, '%Y-%m')".format(xColumnName, yColumnName, where, '%Y-%m')
if timeStatType == '季':
sql = "SELECT CONCAT(YEAR(MIN({0})), '-Q', QUARTER(MIN({0}))) AS {0}, SUM({1}) AS total FROM orders {2} GROUP BY YEAR({0}), QUARTER({0})".format(xColumnName, yColumnName, where)
if timeStatType == '年':
sql = "SELECT DATE_FORMAT({0}, '%Y') {0}, ROUND(sum({1}),2) total FROM hotelinfo {2} GROUP BY DATE_FORMAT({0}, '%Y')".format(xColumnName, yColumnName, where, '%Y')
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'] = L
req_dict = request.session.get("req_dict")
if "order" in req_dict:
order = req_dict["order"]
if order == "desc":
msg['data'] = sorted((x for x in msg['data'] if x['total'] is not None),key=lambda x: x['total'],reverse=True)
else:
msg['data'] = sorted((x for x in msg['data'] if x['total'] is not None),key=lambda x: x['total'])
if "limit" in req_dict and int(req_dict["limit"]) < len(L):
msg['data'] = msg['data'][:int(req_dict["limit"])]
return JsonResponse(msg, encoder=CustomJsonEncoder)
# 按值统计
def hotelinfo_o_value(request, xColumnName, yColumnName):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
#获取hadoop分析后的数据文件
json_filename = f'hotelinfo_value{xColumnName}{yColumnName}.json'
if os.path.exists(json_filename) == True:
with open(json_filename, encoding='utf-8') as f:
msg['data'] = json.load(f)
else:
where = ' where 1 = 1 '
sql = "SELECT {0}, ROUND(sum({1}),2) AS total FROM hotelinfo {2} GROUP BY {0}".format(xColumnName, yColumnName, where)
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'] = L
req_dict = request.session.get("req_dict")
if "order" in req_dict:
order = req_dict["order"]
if order == "desc":
msg['data'] = sorted((x for x in msg['data'] if x['total'] is not None),key=lambda x: x['total'],reverse=True)
else:
msg['data'] = sorted((x for x in msg['data'] if x['total'] is not None),key=lambda x: x['total'])
if "limit" in req_dict and int(req_dict["limit"]) < len(L):
msg['data'] = msg['data'][:int(req_dict["limit"])]
return JsonResponse(msg, encoder=CustomJsonEncoder)
# (按值统计)时间统计类型(多)
def hotelinfo_valueMul(request, xColumnName, timeStatType):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": []}
req_dict = request.session.get("req_dict")
#获取hadoop分析后的数据文件
date_type = ""
if timeStatType == '日':
date_type = "date"
if timeStatType == '月':
date_type = "month"
if timeStatType == '季':
date_type = "quarter"
if timeStatType == '年':
date_type = "year"
#获取hadoop分析后的数据文件
json_filename = f'hotelinfo_value{xColumnName}{yColumnNameMul.replace(",","")}{date_type}.json'
if os.path.exists(json_filename) == True:
with open(json_filename, encoding='utf-8') as f:
msg['data'] = json.load(f)
else:
where = ' where 1 = 1 '
for item in req_dict['yColumnNameMul'].split(','):
sql = ''
if timeStatType == '日':
sql = "SELECT DATE_FORMAT({0}, '%Y-%m-%d') {0}, ROUND(sum({1}),2) total FROM hotelinfo {2} GROUP BY DATE_FORMAT({0}, '%Y-%m-%d') LIMIT 10".format(xColumnName, item, where, '%Y-%m-%d')
if timeStatType == '月':
sql = "SELECT DATE_FORMAT({0}, '%Y-%m') {0}, ROUND(sum({1}),2) total FROM hotelinfo {2} GROUP BY DATE_FORMAT({0}, '%Y-%m') LIMIT 10".format(xColumnName, item, where, '%Y-%m')
if timeStatType == '季':
sql = "SELECT CONCAT(YEAR(MIN({0})), '-Q', QUARTER(MIN({0}))) {0}, sum({1}) total FROM hotelinfo {2} GROUP BY YEAR({0}), QUARTER({0}) LIMIT 10".format(xColumnName, item, where)
if timeStatType == '年':
sql = "SELECT DATE_FORMAT({0}, '%Y') {0}, ROUND(sum({1}),2) total FROM hotelinfo {2} GROUP BY DATE_FORMAT({0}, '%Y') LIMIT 10".format(xColumnName, item, where, '%Y')
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'].append(L)
return JsonResponse(msg, encoder=CustomJsonEncoder)
# (按值统计(多))
def hotelinfo_o_valueMul(request, xColumnName):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": []}
req_dict = request.session.get("req_dict")
#获取hadoop分析后的数据文件
json_filename = f'hotelinfo_value{xColumnName}{yColumnNameMul.replace(",","")}.json'
if os.path.exists(json_filename) == True:
with open(json_filename, encoding='utf-8') as f:
msg['data'] = json.load(f)
else:
where = ' where 1 = 1 '
for item in req_dict['yColumnNameMul'].split(','):
sql = "SELECT {0}, ROUND(sum({1}),2) AS total FROM hotelinfo {2} GROUP BY {0} LIMIT 10".format(xColumnName, item, where)
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'].append(L)
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_group(request, columnName):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
#获取hadoop分析后的数据文件
json_filename = f'hotelinfo_group{columnName}.json'
if os.path.exists(json_filename) == True:
with open(json_filename, encoding='utf-8') as f:
msg['data'] = json.load(f)
else:
where = ' where 1 = 1 '
sql = "SELECT COUNT(*) AS total, " + columnName + " FROM hotelinfo " + where + " GROUP BY " + columnName
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime("%Y-%m-%d")
else:
pass
L.append(online_dict)
msg['data'] = L
req_dict = request.session.get("req_dict")
if "order" in req_dict:
order = req_dict["order"]
if order == "desc":
msg['data'] = sorted((x for x in msg['data'] if x['total'] is not None),key=lambda x: x['total'],reverse=True)
else:
msg['data'] = sorted((x for x in msg['data'] if x['total'] is not None),key=lambda x: x['total'])
if "limit" in req_dict and int(req_dict["limit"]) < len(L):
msg['data'] = msg['data'][:int(req_dict["limit"])]
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_sectionStat_nowprice(request):
'''
新增分段统计接口
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
where = ' where 1 = 1 '
token = request.META.get('HTTP_TOKEN')
decode_str = eval(base64.b64decode(token).decode("utf8"))
sql = """
SELECT '50以内' as nowprice,case when t.50以内 is null then 0 else t.50以内 end total
from
(select
sum(case when nowprice <= 50 then 1 else 0 end) as 50以内, sum(case when nowprice >= 50 and nowprice <= 100 then 1 else 0 end) as 50至100, sum(case when nowprice >= 100 and nowprice <= 150 then 1 else 0 end) as 100至150, sum(case when nowprice >= 150 and nowprice <= 200 then 1 else 0 end) as 150至200, sum(case when nowprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '50至100' as nowprice,case when t.50至100 is null then 0 else t.50至100 end total
from
(select
sum(case when nowprice <= 50 then 1 else 0 end) as 50以内, sum(case when nowprice >= 50 and nowprice <= 100 then 1 else 0 end) as 50至100, sum(case when nowprice >= 100 and nowprice <= 150 then 1 else 0 end) as 100至150, sum(case when nowprice >= 150 and nowprice <= 200 then 1 else 0 end) as 150至200, sum(case when nowprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '100至150' as nowprice,case when t.100至150 is null then 0 else t.100至150 end total
from
(select
sum(case when nowprice <= 50 then 1 else 0 end) as 50以内, sum(case when nowprice >= 50 and nowprice <= 100 then 1 else 0 end) as 50至100, sum(case when nowprice >= 100 and nowprice <= 150 then 1 else 0 end) as 100至150, sum(case when nowprice >= 150 and nowprice <= 200 then 1 else 0 end) as 150至200, sum(case when nowprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '150至200' as nowprice,case when t.150至200 is null then 0 else t.150至200 end total
from
(select
sum(case when nowprice <= 50 then 1 else 0 end) as 50以内, sum(case when nowprice >= 50 and nowprice <= 100 then 1 else 0 end) as 50至100, sum(case when nowprice >= 100 and nowprice <= 150 then 1 else 0 end) as 100至150, sum(case when nowprice >= 150 and nowprice <= 200 then 1 else 0 end) as 150至200, sum(case when nowprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '200以上' as nowprice,case when t.200以上 is null then 0 else t.200以上 end total
from
(select
sum(case when nowprice <= 50 then 1 else 0 end) as 50以内, sum(case when nowprice >= 50 and nowprice <= 100 then 1 else 0 end) as 50至100, sum(case when nowprice >= 100 and nowprice <= 150 then 1 else 0 end) as 100至150, sum(case when nowprice >= 150 and nowprice <= 200 then 1 else 0 end) as 150至200, sum(case when nowprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
""".format(where=where)
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'] = L
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_sectionStat_preprice(request):
'''
新增分段统计接口
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
where = ' where 1 = 1 '
token = request.META.get('HTTP_TOKEN')
decode_str = eval(base64.b64decode(token).decode("utf8"))
sql = """
SELECT '50以内' as preprice,case when t.50以内 is null then 0 else t.50以内 end total
from
(select
sum(case when preprice <= 50 then 1 else 0 end) as 50以内, sum(case when preprice >= 50 and preprice <= 100 then 1 else 0 end) as 50至100, sum(case when preprice >= 100 and preprice <= 150 then 1 else 0 end) as 100至150, sum(case when preprice >= 150 and preprice <= 200 then 1 else 0 end) as 150至200, sum(case when preprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '50至100' as preprice,case when t.50至100 is null then 0 else t.50至100 end total
from
(select
sum(case when preprice <= 50 then 1 else 0 end) as 50以内, sum(case when preprice >= 50 and preprice <= 100 then 1 else 0 end) as 50至100, sum(case when preprice >= 100 and preprice <= 150 then 1 else 0 end) as 100至150, sum(case when preprice >= 150 and preprice <= 200 then 1 else 0 end) as 150至200, sum(case when preprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '100至150' as preprice,case when t.100至150 is null then 0 else t.100至150 end total
from
(select
sum(case when preprice <= 50 then 1 else 0 end) as 50以内, sum(case when preprice >= 50 and preprice <= 100 then 1 else 0 end) as 50至100, sum(case when preprice >= 100 and preprice <= 150 then 1 else 0 end) as 100至150, sum(case when preprice >= 150 and preprice <= 200 then 1 else 0 end) as 150至200, sum(case when preprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '150至200' as preprice,case when t.150至200 is null then 0 else t.150至200 end total
from
(select
sum(case when preprice <= 50 then 1 else 0 end) as 50以内, sum(case when preprice >= 50 and preprice <= 100 then 1 else 0 end) as 50至100, sum(case when preprice >= 100 and preprice <= 150 then 1 else 0 end) as 100至150, sum(case when preprice >= 150 and preprice <= 200 then 1 else 0 end) as 150至200, sum(case when preprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
union all
SELECT '200以上' as preprice,case when t.200以上 is null then 0 else t.200以上 end total
from
(select
sum(case when preprice <= 50 then 1 else 0 end) as 50以内, sum(case when preprice >= 50 and preprice <= 100 then 1 else 0 end) as 50至100, sum(case when preprice >= 100 and preprice <= 150 then 1 else 0 end) as 100至150, sum(case when preprice >= 150 and preprice <= 200 then 1 else 0 end) as 150至200, sum(case when preprice >= 200 then 1 else 0 end) as 200以上 from hotelinfo
{where}
) t
""".format(where=where)
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'] = L
return JsonResponse(msg, encoder=CustomJsonEncoder)
def hotelinfo_sectionStat_commentnum(request):
'''
新增分段统计接口
'''
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
where = ' where 1 = 1 '
token = request.META.get('HTTP_TOKEN')
decode_str = eval(base64.b64decode(token).decode("utf8"))
sql = """
SELECT '200以内' as commentnum,case when t.200以内 is null then 0 else t.200以内 end total
from
(select
sum(case when commentnum <= 200 then 1 else 0 end) as 200以内, sum(case when commentnum >= 200 and commentnum <= 500 then 1 else 0 end) as 200至500, sum(case when commentnum >= 500 and commentnum <= 800 then 1 else 0 end) as 500至800, sum(case when commentnum >= 800 then 1 else 0 end) as 800以上 from hotelinfo
{where}
) t
union all
SELECT '200至500' as commentnum,case when t.200至500 is null then 0 else t.200至500 end total
from
(select
sum(case when commentnum <= 200 then 1 else 0 end) as 200以内, sum(case when commentnum >= 200 and commentnum <= 500 then 1 else 0 end) as 200至500, sum(case when commentnum >= 500 and commentnum <= 800 then 1 else 0 end) as 500至800, sum(case when commentnum >= 800 then 1 else 0 end) as 800以上 from hotelinfo
{where}
) t
union all
SELECT '500至800' as commentnum,case when t.500至800 is null then 0 else t.500至800 end total
from
(select
sum(case when commentnum <= 200 then 1 else 0 end) as 200以内, sum(case when commentnum >= 200 and commentnum <= 500 then 1 else 0 end) as 200至500, sum(case when commentnum >= 500 and commentnum <= 800 then 1 else 0 end) as 500至800, sum(case when commentnum >= 800 then 1 else 0 end) as 800以上 from hotelinfo
{where}
) t
union all
SELECT '800以上' as commentnum,case when t.800以上 is null then 0 else t.800以上 end total
from
(select
sum(case when commentnum <= 200 then 1 else 0 end) as 200以内, sum(case when commentnum >= 200 and commentnum <= 500 then 1 else 0 end) as 200至500, sum(case when commentnum >= 500 and commentnum <= 800 then 1 else 0 end) as 500至800, sum(case when commentnum >= 800 then 1 else 0 end) as 800以上 from hotelinfo
{where}
) t
""".format(where=where)
L = []
cursor = connection.cursor()
cursor.execute(sql)
desc = cursor.description
data_dict = [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
for online_dict in data_dict:
for key in online_dict:
if 'datetime.datetime' in str(type(online_dict[key])):
online_dict[key] = online_dict[key].strftime(
"%Y-%m-%d %H:%M:%S")
else:
pass
L.append(online_dict)
msg['data'] = L
return JsonResponse(msg, encoder=CustomJsonEncoder)
import math
def hotelinfo_cleanse(request):
if request.method in ["POST", "GET"]:
msg = {"code": normal_code, "msg": "成功", "data": {}}
try:
list, _, _, total,_ = hotelinfo.page(hotelinfo, hotelinfo, {})
df = pd.DataFrame(list)
#随机填充
df['preprice'].replace([None, ''], pd.NA,inplace = True)
preprice_non_na = df['preprice'].dropna() # 获取非空值
for i in df.index:
if pd.isna(df.loc[i, 'preprice']):
df.loc[i, 'preprice'] = preprice_non_na.sample(n=1,replace=True).values[0]
#随机填充
df['scoredesc'].replace([None, ''], pd.NA,inplace = True)
scoredesc_non_na = df['scoredesc'].dropna() # 获取非空值
for i in df.index:
if pd.isna(df.loc[i, 'scoredesc']):
df.loc[i, 'scoredesc'] = scoredesc_non_na.sample(n=1,replace=True).values[0]
#随机填充
df['tagname'].replace([None, ''], pd.NA,inplace = True)
tagname_non_na = df['tagname'].dropna() # 获取非空值
for i in df.index:
if pd.isna(df.loc[i, 'tagname']):
df.loc[i, 'tagname'] = tagname_non_na.sample(n=1,replace=True).values[0]
data_list = df.to_dict(orient='records')
hotelinfo.deletes(hotelinfo,hotelinfo,ids=[i["id"] for i in list])
batchList = []
for dl in data_list:
filtered_data = {k: v for k, v in dl.items() if v not in [None, '', float('nan')] and (not isinstance(v, float) or not math.isnan(v))}
batchList.append(hotelinfo(**filtered_data))
hotelinfo.objects.bulk_create(batchList)
except Exception as e:
msg["code"] = other_code
msg["msg"] = e.__str__()
return JsonResponse(msg)
获取源码或论文
如需对应的论文或源码,以及其他定制需求,也可以下方微信联系我。
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)