frames and slots from db

This commit is contained in:
voje 2019-04-02 21:51:44 +02:00
parent 1f83f96267
commit ca942344d7
5 changed files with 45 additions and 6 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ data/samples/
src/frontend_vue/node_modules/ src/frontend_vue/node_modules/
src/frontend_vue/dist/ src/frontend_vue/dist/
dockerfiles/database/create.js dockerfiles/database/create.js
*__pycache__/

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from flask import Flask, render_template, request, url_for, redirect from flask import Flask, render_template, request, url_for, redirect
from valency import Frame, Slot from valency.Frame import Frame, Slot, frames_from_db_entry
from valency.reduce_functions import reduce_functions from valency.reduce_functions import reduce_functions
""" """
@ -39,6 +39,7 @@ CORPORA = ["ssj", "kres"]
app_index = {c: {} for c in CORPORA} app_index = {c: {} for c in CORPORA}
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
valdb = None
app = Flask(__name__) app = Flask(__name__)
@ -50,6 +51,18 @@ app = Flask(__name__)
CORS(app) CORS(app)
# DEV -------------------------------.
@app.route("/api/dev")
def api_dev():
cur = valdb.kres.find({"sid": "F0015940.37.2"})
frames = []
for ent in cur:
frames += frames_from_db_entry(ent)
return json.dumps([x.to_json() for x in frames])
# DEV -------------------------------^
# INDEX SELECTION -------------------. # INDEX SELECTION -------------------.
@app.route("/api/words/<corpus>") @app.route("/api/words/<corpus>")

View File

@ -2,10 +2,36 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def frames_from_db_entry(dbent):
def _full_tid(tid):
return ".".join([dbent["sid"], str(tid)])
frames = []
if "srl_links" not in dbent:
return []
srldict = {}
for srl in dbent["srl_links"]:
key = str(srl["from"])
if key not in srldict:
srldict[key] = [srl]
else:
srldict[key] += [srl]
for hwtid, srlarr in srldict.items():
frames += [Frame(
hw_lemma=_full_tid(hwtid),
tids=[_full_tid(x["to"]) for x in srlarr],
slots=[
Slot(
functor=srl["afun"],
tids=[_full_tid(srl["to"])]
) for srl in srlarr
]
)]
return frames
class Frame(): class Frame():
def __init__(self, tids, deep_links=None, slots=None, hw=None): def __init__(self, tids, deep_links=None, slots=None, hw_lemma=None):
self.hw = hw self.hw = hw_lemma
self.tids = tids # list of tokens with the same hw_lemma self.tids = tids # list of tokens with the same hw_lemma
# Each tid = "S123.t123"; # Each tid = "S123.t123";
# you can get sentence with vallex.get_sentence(S123) # you can get sentence with vallex.get_sentence(S123)
@ -33,7 +59,7 @@ class Frame():
slots = [] slots = []
for link in deep: for link in deep:
slots.append(Slot( slots.append(Slot(
functor=link["functor"], functor=link["afun"],
tids=[link["to"]] tids=[link["to"]]
)) ))
return slots return slots

View File

@ -1 +0,0 @@
from valency.Frame import Frame, Slot

View File

@ -2,7 +2,7 @@
# Input: list of Frame objects, output: list of Frame objects. # Input: list of Frame objects, output: list of Frame objects.
# App uses reduce_0, 1 and 5 # App uses reduce_0, 1 and 5
from valency import Frame, Slot from valency.Frame import Frame, Slot
from copy import deepcopy as DC from copy import deepcopy as DC
import logging import logging