forked from lijiancheng0614/poem_generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
97 lines (84 loc) · 3.61 KB
/
index.py
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
# -*- coding: utf-8 -*-
import os
import time
import flask
from flask import render_template, request, g
import random
import argparse
import generate_poem
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
BASE_FOLDER = os.path.abspath(os.path.dirname(__file__))
DATA_FOLDER = os.path.join(BASE_FOLDER, 'data')
DEFAULT_FCOLLOCATIONS_V = os.path.join(DATA_FOLDER, 'collocations_v')
DEFAULT_FCOLLOCATIONS_H = os.path.join(DATA_FOLDER, 'collocations_h')
DEFAULT_FWORDS = os.path.join(DATA_FOLDER, 'words')
DEFAULT_FTOPIC_WORDS = os.path.join(DATA_FOLDER, 'topic_words')
DEFAULT_FSTART_WORDS = os.path.join(DATA_FOLDER, 'start_words.txt')
collocations_v = generate_poem.read_dump(DEFAULT_FCOLLOCATIONS_V)
collocations_h = generate_poem.read_dump(DEFAULT_FCOLLOCATIONS_H)
words = generate_poem.read_dump(DEFAULT_FWORDS)
topic_words = generate_poem.read_dump(DEFAULT_FTOPIC_WORDS)
start_words = generate_poem.read_txt(DEFAULT_FSTART_WORDS)
app = flask.Flask(__name__)
def set_arguments():
parser = argparse.ArgumentParser(description='Poem Generator')
parser.add_argument('--host', type=str, default='127.0.0.1',
help='host, default is 127.0.0.1.')
parser.add_argument('--port', type=int, default='5000',
help='port, default is 5000.')
return parser
@app.route('/', methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
sentences_count = 4
poem = ['' for i in range(sentences_count)]
try:
poem_1 = request.form['poem_1']
poem_2 = request.form['poem_2']
poem_3 = request.form['poem_3']
poem_4 = request.form['poem_4']
if poem_1:
topic_id = random.randint(0, 10) #TODO
given_poem = [poem_1, poem_2, poem_3, poem_4]
sentence_len = len(given_poem[0])
while True:
poem = generate_poem.generate_poem_with_poem4(given_poem,\
collocations_v, collocations_h, words, topic_words[topic_id], start_words)
print(poem)
flag = True
for i in range(4):
if len(poem[i]) != sentence_len:
flag = False
break
if flag:
break
else:
topic_id = random.randint(0, 10)
sentence_len = 7
start_word = random.choice(start_words)
print(start_word)
while True:
poem = generate_poem.generate_poem(topic_id, sentence_len, sentences_count, start_word,\
collocations_v, collocations_h, words, topic_words[topic_id], start_words)
print(poem)
flag = True
for i in range(4):
if len(poem[i]) != sentence_len:
flag = False
break
if flag:
break
except Exception as e:
print(e)
return render_template('index.htm',
poem_1=poem[0],
poem_2=poem[1],
poem_3=poem[2],
poem_4=poem[3])
return render_template('index.htm', g = g)
if __name__ == '__main__':
parser = set_arguments()
cmd_args = parser.parse_args()
print('{} START {}:{}'.format(time.strftime(TIME_FORMAT), cmd_args.host, cmd_args.port))
app.run(host=cmd_args.host, port=cmd_args.port)
print('{} STOP'.format(time.strftime(TIME_FORMAT)))