-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelDatabase.py
55 lines (46 loc) · 1.87 KB
/
modelDatabase.py
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
import os
from datetime import date
from uuid import UUID, uuid4
from pony.orm import *
from emailSentiment import EmailSentiment
db_host = os.environ['DATABASE_HOST']
db_user = os.environ['DATABASE_USER']
db_pwd = os.environ['DATABASE_PWD']
db_name = os.environ['DATABASE_NAME']
database = Database()
class Email(database.Entity):
sender = Required(str)
subject = Required(str)
received_date = Required(date)
language = Optional(str)
score = Optional(float)
magnitude = Optional(float)
sentences = Set('Sentence')
PrimaryKey(sender, subject, received_date)
class Sentence(database.Entity):
id = PrimaryKey(int, auto=True)
email = Required('Email')
text = Required(str)
sentiment = Required(float)
position = Required(int)
magnitude = Optional(float)
class ModelDatabase:
def __init__(self, *args, **kwargs):
return super().__init__(*args, **kwargs)
def connect(self):
database.bind(provider='postgres', host=db_host, database=db_name, user=db_user, password=db_pwd)
database.generate_mapping(create_tables=True)
@db_session
def insert_email_data(self, email):
inserted_email = Email(sender=email.sender, subject=email.subject, received_date=email.received, score=email.score, magnitude=email.magnitude, language=email.language)
for index, sentence in enumerate(email.sentences):
Sentence(email=inserted_email, text=sentence.text, sentiment=sentence.sentiment, magnitude=sentence.magnitude, position=index)
commit()
return EmailSentiment(inserted_email)
@db_session
def retrieve_email_data(self, email):
return EmailSentiment(Email[email.sender, email.subject, email.received])
@db_session
def update_sentence_sentiment(self, sentence_id, sentiment):
sentence = Sentence[sentence_id]
sentence.sentiment = sentiment