local_db.py
2.24 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from db_address import DB_ADDRESS
from transform_frame import TransformationRules
Base = declarative_base()
VERYSHORT = 50
SHORT = 250
LONG = 1250
class Subtree(Base):
__tablename__ = 'descendant'
id = Column(Integer, primary_key=True)
ancestor = Column(Integer, nullable=False)
descendant = Column(Integer, nullable = False)
class AbstractTransformationTable(AbstractConcreteBase, Base):
__phantom__ = None
def make_table(name):
class TransformationTable(AbstractTransformationTable):
__tablename__ = name
__mapper_args__ = { 'polymorphic_identity': name, 'concrete':True}
__table_args__ = {'extend_existing': True}
id = Column(Integer, primary_key=True)
frame1 = Column(Integer, index=True)
signature1 = Column(String(SHORT))
frame2 = Column(Integer)
signature2 = Column(String(SHORT))
similarity = Column(Float, index=True)
def __init__(self, frame1, signature1, frame2, signature2, similarity):
self.frame1 = frame1
self.signature1 = signature1
self.frame2 = frame2
self.signature2 = signature2
self.similarity = similarity
return TransformationTable
class FrameIdTranslator(Base):
__tablename__ = 'frame_id_translator'
id = Column(Integer, primary_key=True)
slowal_id = Column(Integer)
unifier_id = Column(Integer)
class ArgumentIdTranslator(Base):
__tablename__ = 'argument_id_translator'
id = Column(Integer, primary_key=True)
slowal_id = Column(Integer)
unifier_id = Column(Integer)
def get_db_data(): # session, dictionary of TransformationTable
rules = TransformationRules.get_rules()
TT_dict = {str(rule): make_table('similarity_{}'.format(rule.sign())) for rule in rules}
engine = create_engine(DB_ADDRESS)
DBSession = sessionmaker(bind=engine)
session = DBSession()
return (session, TT_dict)
if __name__ == '__main__':
# creating empty database disabled -- db_import instead
pass