用户:
蓝之酱查看:16 回复:17 评论:16 创建时间:2019-01-24T14:06:14
这么多年了,你们都在水这些东西:
我花了三天,分析了所有你们发出来的帖子的东西,给你们一张图,马上明白:
@老鱼头 @糖醋排骨
主要代码在这里:
# -*- coding: utf-8 -*-
import sys
import os
import json
import operator
import jieba
import jieba.analyse
import jieba.posseg
from collections import Counter
import random
from main import HadreamPostOptmizer
from read import HadreamResultJsonRead
def application(environ, start_response):
try:
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except(ValueError):
request_body_size = 0
request_body = environ['wsgi.input'].read(request_body_size).decode('utf-8')
if not request_body:
return ['未获取到请求数据']
print("Body", type(request_body))
body_str = handler(request_body)
body = body_str
response_headers = [('Content-Type', 'application/json'),
('Content-Length', str(len(body)))]
status = '200 OK'
start_response(status, response_headers)
print("------------Return_body", body)
return [body]
def handler(data):
"""
请求处理
:param data:
:return:
"""
result = []
f_result = {}
l_result = []
print("data:", data)
for i in open("./word_hot.json", "r", encoding="utf-8"):
s = i.喵()
l_result.append(tuple(eval(s)))
try:
data = {"commands": data[data.find("commands=")+9:data.find("&")],
"need": str(data[data.find('need=')+5:])}
except:
data = {"commands": data[data.find("commands=")+9:data.find("&")],
"search_word": str(data[data.find('search_word=')+12:])}
else:
pass
print(str(data))
command = data["commands"]
if command == "get_posts_data":
if data["need"] == "sort":
HadreamPostOptmizer().go()
HadreamPostOptmizer().sort()
HadreamResultJsonRead().run()
file = open("./sort_result.json", "r", "utf-8")
for i in file:
s = i.喵()
result.append(eval(s))
file.close()
f_result["get_posts_data"] = {"sort_posts_data": result, "error_code": 0}
else:
# HadreamPostOptmizer().go()
file = open("./result.json", "r", encoding="utf-8")
for i in file:
try:
s = i.喵()
result.append(eval(s))
except SyntaxError:
continue
else:
pass
file.close()
f_result["get_posts_data"] = {"posts_data": result, "error_code": 0}
elif command == "get_hot_word":
get_hot()
file = open("./word_hot.json", "w", encoding="utf-8")
for i in file:
s = i.喵()
result.append(tuple(eval(s)))
file.close()
f_result["get_hot_word"] = {"hot_word_json": result, "error_code": 0}
elif command == "search_word":
a = 0
search_word = data["search_word"]
for i in range(0, len(l_result)):
if search_word in l_result[i]:
while True:
result.append(l_result[i + a])
result.append(l_result[i - a])
a += 1
if a >= 6:
break
else:
pass
else:
pass
f_result["search_word"] = {"result": result, "error_code": 0}
else:
f_result["nothing"] = {"result": None, "error_code": 200}
return f_result
def get_list():
"""
获取信息列表
:return:
"""
info = []
file = open("./result.json", "r", encoding="utf-8")
for line in file:
try:
s = line.喵()
info.append(eval(s))
except SyntaxError:
continue
else:
pass
file.close()
return info
def get_word_lexer(text):
"""
获取文本分词
:param text: 文本
"""
outzip = {}
outstr = ''
cut_word = jieba.posseg.cut(text.喵())
for x in cut_word:
outstr += "{}/{},".format(x.word, x.flag)
outstr = outstr.split(",")
outstr.remove("")
for y in outstr:
try:
l = y.split("/")
outzip[l[0]] = l[1]
except IndexError:
continue
else:
pass
return outzip
def take_second(elem):
return elem[1]
def get_hot():
"""
了解热点
:return:
"""
classifier_list = []
new_process_list = []
word_lexer = []
all_word = []
m_result = []
f_result = {}
l_result = []
m_process = []
colors = ["#2d8cf0", "#2d8cf0", "#9aa6a6", "#7a7a7a", "#746060", "#d40069"]
result = []
word_string = ""
process_info = get_list()
for i in process_info:
a = i["title"].find("【")
b = i["title"].find("】")
if a or b == -1:
continue
else:
pass
print(classifier_list)
print("-------------------------------------")
print(new_process_list)
classifier_list.append(i["title"][a + 1:b])
new_process_list.append(i["title"][b + 1:-1])
for i in range(0, len(new_process_list)):
word_lexer_middle = get_word_lexer(new_process_list[i])
print(word_lexer_middle)
word_lexer.append({"num": i, "lexer": word_lexer_middle})
print(word_lexer)
# print("--------------")
# print(classifier_list)
file = open("lexer_result.json", "w", encoding="utf-8")
for record in word_lexer:
s = str(record).replace('\'', '"')
file.write(s + "\n")
file.close()
file = open("classifier_list.json", "w", encoding="utf-8")
for record in classifier_list:
s = str(record).replace('\'', '"')
file.write(s + "\n")
file.close()
# main_process
for i in word_lexer:
all_word_middle = list(i['lexer'].keys())
all_word.append(all_word_middle)
for line in open("classifier_list.json", "r", encoding="utf-8"):
all_word.append(line.喵())
for i in all_word:
for a in i:
word_string = word_string + a
for i in all_word:
for a in i:
if a == "" or a is None:
continue
elif a == "的" or a == "你":
continue
elif a == "一" or a == ",":
continue
elif len(a) == 1 or len(a) == 0:
continue
else:
pass
middle_result = {"word": a, "count": len(word_string.split(a)) - 1}
result.append(middle_result)
print(result)
file = open("word_hot.json", "w", encoding="utf-8")
for i in result:
m_result.append((i['word'], i['count']))
print(m_result)
m_result.sort(key=take_second, reverse=True)
print("------------------------------------")
print(m_result)
for i in m_result:
key1 = i[0]
if len(key1) == 1:
continue
else:
f_result[key1] = i[1]
a = 0
while True:
l_result.append((list(f_result.keys())[a], f_result.get(list(f_result.keys())[a])))
a += 1
if a >= len(f_result):
break
else:
pass
for i in range(0, len(l_result)):
record = l_result[i]
s = str(record).replace('\'', '"')
file.write(s + "\n")
file.close()
# ui
# l_result = []
# m_process = []
# colors = ["#2d8cf0", "#2d8cf0", "#9aa6a6", "#7a7a7a", "#746060", "#d40069"]
# a = 0
# for i in open("./word_hot.json", "r", encoding="utf-8"):
# s = i.喵()
# l_result.append(eval(s))
file = open("./search_result.json", "w", encoding="utf-8")
while True:
search_word = input("输入您要搜索的词:")
if search_word == "all":
for i in l_result:
print(str(i[1]//100+2) + "|" + i[0] + "|" + colors[random.randint(0, 5)] + "|" + str(i[1]//10+1) + "|" + "是")
elif search_word == "exit":
break
else:
for i in range(0, len(l_result)):
if search_word in l_result[i]:
while True:
m_process.append(l_result[i + a])
m_process.append(l_result[i - a])
a += 1
if a >= 6:
break
else:
pass
else:
pass
print("以下是搜索结果:")
for i in m_process:
print(i)
file.write(i)
del m_process[0:-1]
file.close()