-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (99 loc) · 3.83 KB
/
app.py
File metadata and controls
138 lines (99 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
__author__ = 'chenliang'
import random
import sys
import name_data
import poem
import wu_ge
import importlib
from flask import Flask, jsonify, request, render_template
importlib.reload(sys) # Reload does the trick!
from logging import handlers
from wu_ge import *
from poem import *
from name_data import *
app = Flask(__name__)
app.debug = True
def init_logging():
path = get_relative_path(__file__, "log/namebb.log")
logger = handlers.RotatingFileHandler(path)
# logger.setLevel(logging.DEBUG)
app.logger.addHandler(logger)
@app.route('/')
def index():
return render_template("index.html")
def get_name(xing, word_count, gender):
print("params ", xing, word_count, gender)
if gender == "male":
gender_for_data = "boy"
elif gender == "female":
gender_for_data = "girl"
else:
if random.randint(0, 1) == 0:
gender_for_data = "boy"
else:
gender_for_data = "girl"
if word_count == 1:
word_count_for_data = 1
elif word_count == 2:
word_count_for_data = 2
else:
if random.randint(0, 1) == 0:
word_count_for_data = 1
else:
word_count_for_data = 2
target_files = []
if gender_for_data == "boy" and word_count_for_data == 1:
target_files = boy_name_one_character_files
elif gender_for_data == "boy" and word_count_for_data == 2:
target_files = boy_name_two_character_files
if gender_for_data == "girl" and word_count_for_data == 1:
target_files = girl_name_one_character_files
elif gender_for_data == "girl" and word_count_for_data == 2:
target_files = girl_name_two_character_files
target_file_path = target_files[random.randint(0, len(target_files) - 1)]
print("get name params", gender_for_data, word_count_for_data, target_file_path)
with open(target_file_path) as name_file:
names = [name for name in name_file.read().encode("utf-8").decode("utf-8").split("\n") if name.strip() != ""]
return names[random.randint(0, len(names) - 1)]
@app.route('/name/random')
def random_name():
xing = request.args.get('xing')
word_count = int(request.args.get('word_count'))
gender = request.args.get('gender')
print("xing={0} gender={1} word_count={2}".format(xing, gender, word_count))
name = get_name(xing, word_count, gender)
whole_name = xing + name
reading = get_reading_for_name(whole_name)
print("name={0} reading={1}".format(whole_name, "".join(reading)))
# scores
# 1. referenced by poem
# 2. total stroke counts
# 3. wu_ge score
#
score = {}
score["poem"] = get_poem_score(name)
score["wuge"] = get_wu_ge_score(xing, name)
score["stroke_count"] = get_stroke_count_score(name)
for k in score:
print(k, "--->", score[k])
response = jsonify(n=[c for c in whole_name], r=reading, score=score)
return response
def get_stroke_count_score(name):
return get_stroke_score(sum(map(lambda c: get_stroke_count_for_character(c), name)))
# def get_poem_score(name):
# count = len(filter(lambda c:c in all_characters_in_poems, name))
# return int(count*1.0*100/len(name))
@app.route('/name/rank')
def rank_name():
return jsonify(score=random.randint(0, 100), name=request.args.get('name', ''))
# def random_name(files):
# name_file_path = files[random.randint(0, len(files) - 1)]
# with open(name_file_path) as name_file:
# names = [name for name in name_file.read().decode("utf-8").split("\n") if name.strip() != ""]
# return names[random.randint(0, len(names) - 1)]
init_logging()
boy_name_one_character_files, boy_name_two_character_files, girl_name_one_character_files, girl_name_two_character_files = init_names(
get_relative_path(__file__, "names"))
if __name__ == '__main__':
app.run(host='0.0.0.0')