forked from kristjan/cjvt-valency
working on api + frontend; todo: switch corpus
This commit is contained in:
@@ -26,9 +26,13 @@ from pathlib import Path
|
||||
from pymongo import MongoClient
|
||||
import argparse
|
||||
|
||||
CORPORA = ["ssj", "kres"]
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
app = Flask(__name__)
|
||||
|
||||
app_index = {c: {} for c in CORPORA}
|
||||
|
||||
# when running vuejs via webpack
|
||||
# CORS(app)
|
||||
CORS(app, resources={r"/api/*": {
|
||||
@@ -54,20 +58,18 @@ def home(pathname):
|
||||
return redirect(url_for("index"), code=302)
|
||||
|
||||
|
||||
# @app.route("/api/words/<corpus>")
|
||||
# def api_words(corpus):
|
||||
@app.route("/api/words")
|
||||
def api_words():
|
||||
return json.dumps({
|
||||
"sorted_words": vallex.sorted_words,
|
||||
"has_se": vallex.has_se
|
||||
"sorted_words": app_index["ssj"]["words"], # todo - make corpus as arg
|
||||
})
|
||||
|
||||
|
||||
@app.route("/api/functors")
|
||||
def api_functors():
|
||||
res = []
|
||||
for key in sorted(vallex.functors_index.keys()):
|
||||
res.append((key, len(vallex.functors_index[key])))
|
||||
return json.dumps(res)
|
||||
# return array ([functor, len])
|
||||
return json.dumps(app_index["ssj"]["functors"])
|
||||
|
||||
|
||||
@app.route("/api/register", methods=["POST"])
|
||||
@@ -376,6 +378,60 @@ def api_senses_update():
|
||||
vallex.db["v2_sense_map"].insert(data)
|
||||
return "OK"
|
||||
|
||||
def prepare_db():
|
||||
def helper_tid_to_token(tid, tokens):
|
||||
for t in tokens:
|
||||
if t["tid"] == tid:
|
||||
return t
|
||||
return None
|
||||
|
||||
# update entries (add headwords and fuctors for indexing)
|
||||
for corpus in ["ssj", "kres"]:
|
||||
for e in valdb[corpus].find({}):
|
||||
#! hw_tids are not array ids
|
||||
hw_tids = list(set([x["from"] for x in e["srl_links"]]))
|
||||
hw_tokens = [helper_tid_to_token(tid, e["tokens"]) for tid in hw_tids]
|
||||
headwords = [(t["lemma"] if t["msd"][0] == "G" else t["lemma"] + "_") for t in hw_tokens]
|
||||
e["headwords"] = headwords
|
||||
|
||||
functors = list(set([x["afun"] for x in e["srl_links"]]))
|
||||
e["headwords"] = headwords
|
||||
e["functors"] = functors
|
||||
|
||||
valdb[corpus].save(e)
|
||||
|
||||
# create app_index (used in frontend, left side word index)
|
||||
for corpus in CORPORA:
|
||||
res_hws = {}
|
||||
res_fns = {}
|
||||
for e in valdb[corpus].find({}):
|
||||
for hw in e["headwords"]:
|
||||
if hw in res_hws:
|
||||
res_hws[hw] += 1
|
||||
else:
|
||||
res_hws[hw] = 1
|
||||
for fn in e["functors"]:
|
||||
if fn in res_fns:
|
||||
res_fns[fn] += 1
|
||||
else:
|
||||
res_fns[fn] = 1
|
||||
|
||||
alphabetical = {}
|
||||
for k, e in res_hws.items():
|
||||
fst = k[0].lower()
|
||||
if fst in alphabetical:
|
||||
alphabetical[fst].append((k, e))
|
||||
else:
|
||||
alphabetical[fst] = [(k, e)]
|
||||
|
||||
for k, e in alphabetical.items():
|
||||
alphabetical[k] = sorted(e, key=lambda x: x[0])
|
||||
app_index[corpus]["words"] = alphabetical
|
||||
|
||||
functors = [(k, e) for (k, e) in res_fns.items()]
|
||||
functors = sorted(functors, key=lambda x: x[0])
|
||||
app_index[corpus]["functors"] = functors
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starting app.py main()")
|
||||
@@ -407,8 +463,10 @@ if __name__ == "__main__":
|
||||
)
|
||||
valdb = client.valdb
|
||||
|
||||
if config["prepare_db"]:
|
||||
prepare_db()
|
||||
|
||||
# log.info("[*] Starting app.py with config:\n%s".format(config))
|
||||
log.info("[*] Starting app.py with config:\n{}".format(config))
|
||||
sys.exit()
|
||||
|
||||
app.run(host=str(config["host"]), port=int(config["port"]))
|
||||
|
||||
@@ -3,4 +3,5 @@ debug: True
|
||||
port: 5004
|
||||
host: localhost
|
||||
logfile: "/var/log/valency_backend.log"
|
||||
prepare_db: True
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user