add fuzzy search
This commit is contained in:
parent
534b13b42b
commit
2ef2f9a0a5
@ -16,4 +16,6 @@
|
||||
* /keyword/get
|
||||
* 401:keyword表获取失败
|
||||
* /wxpy/acceptmsg
|
||||
* 500:校验失败
|
||||
* 500:校验失败
|
||||
* 501:模糊搜索keyword获取失败
|
||||
* 502:num自增失败
|
9
api.py
9
api.py
@ -2,7 +2,7 @@
|
||||
import sys
|
||||
sys.path.append('./lib')
|
||||
from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory, Response
|
||||
from allFunction import manageGet, managePost, manageInsertKeyword, manageDelKeyword, manageGetKeyword, manageMaterial
|
||||
from allFunction import manageGet, managePost, manageInsertKeyword, manageDelKeyword, manageGetKeyword, manageMaterial, replySomething
|
||||
from flask_apscheduler import APScheduler
|
||||
from wxRequest import requestsAccessToken
|
||||
scheduler = APScheduler()
|
||||
@ -44,8 +44,11 @@ if __name__ == '__main__':
|
||||
# 测试用根路由
|
||||
@app.route('/')
|
||||
def sayHello():
|
||||
return 'Hello world'
|
||||
|
||||
try:
|
||||
dict_content = request.args.to_dict()
|
||||
return replySomething(dict_content['msg'])
|
||||
except Exception as e:
|
||||
return 'please send some arg as msg'
|
||||
# 用于接受微信发来的消息
|
||||
@app.route('/wxpy/acceptmsg', methods=["POST", "GET"])
|
||||
def acceptMsg():
|
||||
|
@ -7,7 +7,7 @@ import reply
|
||||
# 引入md5校验
|
||||
from hashlib import md5
|
||||
# 引入数据库操作函数
|
||||
from db import insertKeyword, delKeyword, getAllKeyword
|
||||
from db import insertKeyword, delKeyword, getAllKeyword, fuzzySearch, addCount
|
||||
# 引入json
|
||||
import json
|
||||
# 引入获取微信图文信息
|
||||
@ -62,9 +62,11 @@ def managePost(request):
|
||||
if isinstance(recMsg, receive.Msg):
|
||||
toUser = recMsg.FromUserName
|
||||
fromUser = recMsg.ToUserName
|
||||
row_content = recMsg.Content
|
||||
if recMsg.MsgType == 'text':
|
||||
# 文本类型
|
||||
content = "自动回复功能开发测试中,敬请期待"
|
||||
# content = "自动回复功能开发测试中,敬请期待"
|
||||
content = replySomething(row_content)
|
||||
# 获取返回信息
|
||||
replyMsg = reply.TextMsg(toUser, fromUser, content)
|
||||
return replyMsg.send()
|
||||
@ -151,4 +153,17 @@ def checkData(data):
|
||||
md = md5()
|
||||
md.update(d.encode('utf-8'))
|
||||
r = md.hexdigest().upper()
|
||||
return r == data['sign']
|
||||
return r == data['sign']
|
||||
|
||||
# 组织回复什么
|
||||
def replySomething(content):
|
||||
# 获取数据库中查询到的
|
||||
search_content = fuzzySearch(content)
|
||||
keywords_arr = search_content['arr']
|
||||
# 没搜索到关键词进行拦截
|
||||
if len(keywords_arr) == 0:
|
||||
return '已经收到您发送的消息,感谢您的关注'
|
||||
# 有关键词
|
||||
keyword = keywords_arr[0]
|
||||
addCount(str(keyword['_id']))
|
||||
return keyword['content']
|
21
lib/db.py
21
lib/db.py
@ -1,5 +1,6 @@
|
||||
from pymongo import MongoClient
|
||||
from bson import ObjectId, json_util
|
||||
import re
|
||||
|
||||
# 主环境 (生产环境为production,开发环境为development)
|
||||
setting = 'development'
|
||||
@ -80,4 +81,22 @@ def getAllKeyword():
|
||||
arr.append(i)
|
||||
except Exception as e:
|
||||
return {'errcode': 401, 'errmsg': 'keyword表获取失败'}
|
||||
return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'}
|
||||
return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'}
|
||||
|
||||
# 模糊搜索
|
||||
def fuzzySearch(keyword):
|
||||
arr = []
|
||||
try:
|
||||
for i in col('keyword').find({'keywords': re.compile(keyword)}):
|
||||
arr.append(i)
|
||||
except Exception as e:
|
||||
return {'errcode': 501, 'errmsg': '模糊搜索keyword获取失败', 'arr': [],}
|
||||
return {'errcode': 200, 'arr': arr, 'errmsg': 'ok'}
|
||||
|
||||
# 增加查询数
|
||||
def addCount(id):
|
||||
try:
|
||||
res = col('keyword').update_one({"_id": ObjectId(id)},{"$inc": {"num": 1}})
|
||||
except Exception as e:
|
||||
return {'errcode': 502, 'errmsg': 'num自增失败',}
|
||||
return {'errcode': 200, 'errmsg': 'ok'}
|
Loading…
x
Reference in New Issue
Block a user