计算机毕业设计-基于Python+Flask的基于深度学习的电影评论情感分析系统项目开发实战(附源码+论文)

时间:2025-03-31 07:06:59
import csv import os import sys import time from flask import Blueprint, request, jsonify, render_template, session, redirect workdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, workdir) from logger import Logger from exts import db from .models import * from ai_detection.word2vec_svm import test2 from ai_detection import serve PAGE_SIZE = 5 bp = Blueprint('inference', '__name__', url_prefix='') logger = Logger(logname='inference_views.log', loglevel=5, logger='inference').getlog() @bp.route('/get_movies',methods=['get']) def get_movies(): """ 查询所有用户信息 :return: """ keyword = request.args.get('name') page = int(request.args.get("page")) limit = int(request.args.get("limit")) response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: user_objs = Movie.query.order_by(Movie.id.desc()) else: user_objs = Movie.query.filter(Movie.name.like('%{keyword}%'.format(keyword=keyword))) results = user_objs.paginate(page=page, per_page=limit, error_out=False) users_page = results.items if users_page: for result in users_page: record = { "id": result.id, "name": result.name, "performer": result.performer, 'create_time': str(result.create_time), "country": result.country, "type": result.type, 'desc':result.desc, 'path':result.path } data.append(record) response_data['count'] = len(user_objs.all()) response_data['data'] = data return response_data @bp.route('/edit_movie',methods=['post']) def edit_movie(): response_data = {} movie_id = request.form.get('id') result = Movie.query.filter(Movie.id==movie_id).first() result.name=request.form.get('name') result.type=request.form.get('type') result.path = request.form.get('path') result.performer = request.form.get('performer') result.country = request.form.get('country') db.session.commit() response_data['msg'] = '修改信息成功!' return jsonify(response_data), 201 @bp.route('/index', methods=['GET']) def index1(): """ :return: """ # with open((workdir,'inference',''),'r',encoding='gbk') as f: # csv_reader = (f) # for l_list in csv_reader: # name= l_list[0] # pj = l_list[4].strip() # performer= l_list[5] # # #movie = Movie(name=name,performer=performer,country=country,type=lb,desc=jj,create_time=sy,image=image) # r = (==name).first() # text = Text(name=pj,owner=performer,status='',movie=,star=([1,2,3,4,5]),count=l_list[7],create_time=l_list[6].strip()) # print(r) # # (text) # () return render_template('') @bp.route('/', methods=['GET']) def login(): """ :return: """ return render_template('') @bp.route('/movies', methods=['get']) def movies(): return render_template('') @bp.route('/user', methods=['get']) def user(): return render_template('') @bp.route('texts',methods=['get']) def texts(): return render_template('') @bp.route('/get_text', methods=['get', 'post']) def get_text(): """ 查询所有用户信息 :return: """ keyword = request.args.get('name') page = int(request.args.get("page")) limit = int(request.args.get("limit")) print(page) response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: user_objs = Text.query.order_by(Text.id.desc()) else: user_objs = Text.query.filter(Text.name.like('%{keyword}%'.format(keyword=keyword))) results = user_objs.paginate(page=page, per_page=limit, error_out=False) users_page = results.items if users_page: for result in users_page: record = { "id": result.id, "name": result.name, "count": result.count, 'create_time': str(result.create_time), "star": result.star, "status": result.status, 'owner':result.owner } data.append(record) response_data['count'] = len(user_objs.all()) response_data['data'] = data return response_data @bp.route('/get_users', methods=['get', 'post']) def get_users(): """ 查询所有用户信息 :return: """ keyword = request.args.get('name') page = int(request.args.get("page")) limit = int(request.args.get("limit")) print(page) response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: user_objs = User.query.order_by(User.id.desc()) else: user_objs = User.query.filter(User.name.like('%{keyword}%'.format(keyword=keyword))) results = user_objs.paginate(page=page, per_page=limit, error_out=False) users_page = results.items if users_page: for user_obj in users_page: if user_obj.role == '3': role = '管理员' else: role = '普通用户' record = { "id": user_obj.id, "name": user_obj.name, "password": user_obj.password, 'create_time': str(user_obj.create_time), "phone": user_obj.phone, "role": role } data.append(record) response_data['count'] = len(user_objs.all()) response_data['data'] = data return response_data @bp.route('/personal', methods=['get', 'post']) def personal(): """ 个人信息 :return: """ username = session.get('username', 'admin') user_obj = User.query.filter(User.name == username).first() return render_template('', user_obj=user_obj) @bp.route('/search', methods=['get', 'post']) def search(): """ 首页搜索 :return: """ text = request.args.get('text') # 根据名字模糊查询 movie_obj = Movie.query.filter(Movie.name.like('%{keyword}%'.format(keyword=text))).first() return render_template('', movie_obj=movie_obj) @bp.route('/add_user', methods=['post']) def add_user(): """ 新增用户 :return: """ # 判断用户是否存在 others = User.query.filter_by(name=request.form.get('username')).all() if len(others) > 0: return jsonify({"code": 'error!', 'msg': '用户名重复!'}) others = User.query.filter_by(phone=request.form.get('phone')).all() if len(others) > 0: return jsonify({"code": 'error!', 'msg': '手机号重复!'}) password = request.form.get('password') user = User(name=request.form.get('username'), password=password, phone=request.form.get('phone'), role=2) try: db.session.add(user) db.session.commit() return jsonify({"code": 'success!'}) except Exception as e: logger.error(str(e)) return jsonify({'code': 'error!'}) @bp.route('/login_out', methods=['get']) def login_out(): """ 注销登录 :return: """ # 删除session if session.get("username"): del session["username"] return redirect('/') @bp.route('/register', methods=['get']) def register(): """ 注册页面 :return: """ return render_template('') @bp.route('/check_login', methods=['POST']) def check_login(): """ 登录验证 :return: """ username = request.form.get('username') password = request.form.get('password') print(username, password) user = db.session.query(User).filter(User.name == username).first() # 判断用户密码是否正确 if user: if user.password == password: session['username'] = username session['role'] = user.role session['user_id'] = user.id session.permanent = True return {"msg": "验证通过", 'role': user.role} else: return jsonify({"msg": "密码错误,请重新输入"}), 401 return jsonify({"msg": "帐号不存在,请重新输入"}), 401 @bp.route('edit_user', methods=['post']) def edit_user(): """ 编辑用户 :return: """ print(1111111) response_data = {} user_id = request.form.get('id') username = request.form.get('username') phone = request.form.get('phone') others = db.session.query(User).filter(User.id != user_id).all() if others: for other in others: if other.username == username: response_data['msg'] = '用户名重复,请重新填写!' return jsonify(response_data), 402 if other.phone == phone: response_data['msg'] = '手机号重复,请重新填写!' return jsonify(response_data), 402 user = db.session.query(User).filter(User.id == user_id).first() user.username = username user.password = request.form.get('password') user.phone = phone db.session.commit() response_data['msg'] = '修改用户信息成功!' return jsonify(response_data), 201 else: response_data['msg'] = '修改用户信息失败!' return jsonify(response_data), 402 @bp.route('/del_user', methods=['post']) def del_user(): """ 删除用户 :return: """ user_id = request.form.get('id') result = db.session.query(User).filter(User.id == user_id).first() if not result: response_data = {'msg': '删除失败!该用户不存在'} return jsonify(response_data), 402 db.session.delete(result) db.session.commit() response_data = {'msg': '删除成功!'} return jsonify(response_data), 201 @bp.route('/get_info', methods=['get']) def get_info(): """ 获取登录用户信息 :return: """ username = session.get('username', '0') return jsonify({'username': username}) @bp.route("/predict", methods=['post']) def predict(): """ 预测文本 :return: """ print(222) text = request.form.get('text') movie_id = request.form.get('movie_id') print(text,movie_id) # 调word2vac预测 result = test2.svm_predict(text) t_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) if not result: result = serve.predict_main(text) t = Text(owner=session['username'],name=text,status=result,movie=movie_id,star=3,count=1,create_time=str(t_time)) db.session.add(t) db.session.commit() return redirect('/next_page1/'+str(movie_id)) @bp.route("/next_page/<movie_id>", methods=['get']) def next_page(movie_id): """ 下一页 :param movie_id: :return: """ movie_obj = Movie.query.filter(Movie.id == movie_id).first() result1 = Text.query.filter(Text.movie == movie_id, Text.status == '积极').all() result2 = Text.query.filter(Text.movie == movie_id, Text.status == '消极').all() result3 = Text.query.filter(Text.movie == movie_id, Text.status == '一般').all() values = [{'name': '积极', 'value': len(result1)}, {'name': '消极', 'value': len(result2)}, {'name': '一般', 'value': len(result3)}] return render_template('', movie_obj=movie_obj, values=values) @bp.route('/update_password',methods=['post']) def update_password(): old_pwd = request.form.get('oldPwd') username = session.get('username') user_obj = User.query.filter(User.name==username).first() if user_obj.password == old_pwd: user_obj.password = request.form.get('newPwd') db.session.commit() return jsonify({'msg': '修改成功!', 'error': 0}) else: return jsonify({'msg':'旧密码不正确,请重新输入','error':403}) @bp.route("/next_page1/<movie_id>", methods=['get']) def next_page1(movie_id): movie_obj = Movie.query.filter(Movie.id == movie_id).first() result1 = Text.query.filter(Text.movie == movie_id, Text.status == '积极').all() result2 = Text.query.filter(Text.movie == movie_id, Text.status == '消极').all() result3 = Text.query.filter(Text.movie == movie_id, Text.status == '一般').all() values = [{'name': '积极', 'value': len(result1)}, {'name': '消极', 'value': len(result2)}, {'name': '一般', 'value': len(result3)}] return render_template('', movie_obj=movie_obj, values=values, result1=result1, result2=result2, result3=result3, length1=len(result1), length2=len(result2), length3=len(result3))