This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
91 lines (73 loc) · 2.5 KB
/
server.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
from pathlib import Path
from flask import Flask, render_template, request, jsonify, send_from_directory
from flask_cors import CORS
from tpe import *
import json
app = Flask(__name__)
CORS(app)
# setup directories
app_dir = Path(app.root_path)
app.template_folder = app_dir / "build"
app.static_folder = app_dir / "build" / "static"
root_dir = app_dir / "data"
@app.route("/")
@app.route("/explication")
def homepage():
return render_template("index.html")
@app.route("/favicon.ico")
def get_favicon():
return send_from_directory(app.template_folder, "favicon.ico")
@app.route("/generate_markov", methods=["POST"])
def generate_markov():
request_data = json.loads(request.data)
text_name = request_data.get("textName")
current = request_data.get("current")
n = request_data.get("n")
complete_sentence = request_data.get("completeSentence")
random_choice = bool(request_data.get("randomChoice"))
print(text_name)
assert len(current) >= n or len(current) == 0
m = MarkovChain(n=n, root_path=root_dir)
try:
m.load_markov(text_name)
except FileNotFoundError:
print(f"'{text_name}' hasn't been pickled yet")
m.learn(m.get_text(text_name))
m.save_markov(text_name)
print("Learning done.")
other_next_words = None
first_words = False
if complete_sentence:
current = m.generate_sentence(current)
elif not current:
current = m.get_first_words()
first_words = True
else:
print("RANDOM CHOICE", random_choice)
data = m.next_word(current, random_choice=random_choice)
other_next_words = data.get("possibleNextWords")
current.extend(data["next"])
return jsonify({
"isFirstWords": first_words,
"currentSentence": current,
"possibleNextWords": other_next_words
})
@app.route("/generate_pcfg", methods=["POST"])
def generate_pcfg():
request_data = json.loads(request.data)
text_name = request_data.get("textName")
left_most = request_data.get("leftMost")
gra = ContextFreeGrammar(root_path=root_dir, debug=False)
try:
gra.load_pcfg(text_name)
except FileNotFoundError:
print(f"'{text_name}' hasn't been pickled yet")
gra.learn_text_sample(gra.get_text(text_name)) # TODO subprocess
gra.save_pcfg(text_name)
current = gra.derive(left_most=left_most)
return jsonify({
"currentSentence": current["sentence"],
"csv": current["csv"]
})
if __name__ == '__main__':
app.run()