shellvalier_db.py 2.7 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, join, outerjoin
from sqlalchemy import create_engine

DB_ADDRESS = 'postgresql://khelleb:haslo@localhost/shellvalier'

Base = declarative_base()

class LexicalUnit(Base):
    __tablename__ = 'meanings_lexicalunit'
    id = Column(Integer, primary_key=True)
    base = Column(String)
    sense = Column(String)
    pos = Column(String)
    luid = Column(Integer, nullable=True)

class LexicalUnitFrame(Base):
    __tablename__ = 'semantics_frame_lexical_units'
    id = Column(Integer, primary_key=True)
    frame_id = Column(Integer, nullable=True)
    lexicalunit_id = Column(Integer, nullable=True)

class SemanticRole(Base):
    __tablename__ = 'semantics_semanticrole'
    id = Column(Integer, primary_key=True)
    role = Column(String, nullable=True)

class RoleAttribute(Base):
    __tablename__ = 'semantics_roleattribute'
    id = Column(Integer, primary_key=True)
    attribute = Column(String, nullable=True)

class ArgumentRole(Base):
    __tablename__ = 'semantics_argumentrole'
    id = Column(Integer, primary_key=True)
    role_id = Column(Integer, nullable=True)
    attribute_id = Column(Integer)

class Argument(Base):
    __tablename__ = 'semantics_argument'
    id = Column(Integer, primary_key=True)
    frame_id = Column(Integer, nullable=True)
    role_id = Column(Integer, nullable=True)

def getLuToFrameId():
    engine = create_engine(DB_ADDRESS)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    j = join(LexicalUnit, LexicalUnitFrame, LexicalUnit.id == LexicalUnitFrame.lexicalunit_id)
    q = session.query(LexicalUnit.base, LexicalUnit.sense, LexicalUnit.pos, LexicalUnitFrame.frame_id).select_from(j).all()
    lu_to_frame_id = {}
    for base, sense, pos, frame_id in q:
        lu_to_frame_id[(base, pos, sense)] = frame_id

    return lu_to_frame_id

def getFrameArgumentToArgumentId():
    engine = create_engine(DB_ADDRESS)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    j1 = join(ArgumentRole, SemanticRole, ArgumentRole.role_id == SemanticRole.id)
    j2 = outerjoin(j1, RoleAttribute, ArgumentRole.attribute_id == RoleAttribute.id)
    j3 = join(Argument, j2, Argument.role_id == ArgumentRole.id)
    q = session.query(Argument.frame_id, SemanticRole.role, RoleAttribute.attribute, Argument.id).select_from(j3).all()
    frame_argument_id = {}
    for frame_id, role, attribute, argument_id in q:
        if frame_id not in frame_argument_id:
            frame_argument_id[frame_id] = {}
        frame_argument_id[frame_id][(role, attribute)] = argument_id

    return frame_argument_id