shellvalier_db.py
2.7 KB
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
#! /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