Skip to content

Commit 052f40a

Browse files
committed
storing analytics stuff to database
1 parent 1fad1bc commit 052f40a

File tree

6 files changed

+108
-3
lines changed

6 files changed

+108
-3
lines changed

Diff for: application.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@
44
from logging import Formatter, FileHandler
55
from forms import *
66
import os
7-
7+
import json
8+
from watson_developer_cloud import ToneAnalyzerV3
9+
from watson_developer_cloud import AlchemyLanguageV1
10+
import requests
11+
from ma_schema.AnalyticsSchema import AnalyticsSchema
12+
from ma_schema.UserSchema import UserSchema
13+
14+
import uuid
15+
from models.Analytics import Analytics
16+
17+
WATSON_USERNAME = '1be2c698-56e7-47d4-9944-6e4d81c9b07d',
18+
WATSON_PASSWORD = 'sPr4XOsPtNSX'
19+
ALCHEMY_API_KEY = '07551e54797d7b593f3595653a7cad5b1803d3a6'
820
app = Flask(__name__)
921
app.secret_key = 's3cr3t'
1022

@@ -16,13 +28,60 @@ def shutdown_session(exception=None):
1628

1729
@app.route('/')
1830
def home():
31+
aSchema = AnalyticsSchema()
32+
over = Analytics.query.all()
33+
uSchema = UserSchema()
34+
print over
1935
return render_template('pages/placeholder.home.html')
2036

2137

2238
@app.route('/about')
2339
def about():
2440
return render_template('pages/placeholder.about.html')
2541

42+
@app.route('/analytics')
43+
def analytics():
44+
45+
aSchema = AnalyticsSchema()
46+
#sentJson = reqData['review']
47+
48+
content = "Facebook, you suck!"
49+
50+
sentJson = {}
51+
52+
tone_analyzer = ToneAnalyzerV3(
53+
username='1be2c698-56e7-47d4-9944-6e4d81c9b07d',
54+
password=WATSON_PASSWORD,
55+
version='2016-05-19')
56+
57+
alchemy_language = AlchemyLanguageV1(api_key='07551e54797d7b593f3595653a7cad5b1803d3a6')
58+
sentiment = alchemy_language.sentiment(text=content)
59+
60+
sentJson['id'] = str(uuid.uuid1())
61+
sentJson['sentiment_score'] = sentiment['docSentiment']['score']
62+
sentJson['sentiment_type'] = sentiment['docSentiment']['type']
63+
64+
sentimentData = tone_analyzer.tone(text=content)
65+
66+
sentJson['anger'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][0]["score"]
67+
sentJson['disgust'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][1]["score"]
68+
sentJson['fear'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][2]["score"]
69+
sentJson['joy'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][3]["score"]
70+
sentJson['sadness'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][4]["score"]
71+
72+
sentJson['openness'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][0]["score"]
73+
sentJson['conscientiousness'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][1]["score"]
74+
sentJson['extraversion'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][2]["score"]
75+
sentJson['aggreablesness'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][3]["score"]
76+
sentJson['neuroticism'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][4]["score"]
77+
78+
#print sentJson
79+
ana = aSchema.load(sentJson, session=db_session).data
80+
db_session.add(ana)
81+
db_session.commit()
82+
83+
return render_template('pages/placeholder.home.html')
84+
2685

2786
@app.route('/login')
2887
def login():

Diff for: database/db.py

+1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ def init_db():
5252
# import all modules here that might define models so that
5353
# they will be registered properly on the metadata. Otherwise
5454
# you will have to import them first before calling init_db()
55+
5556
import models
5657
Base.metadata.create_all(bind=engine)

Diff for: ma_schema/AnalyticsSchema.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from marshmallow_sqlalchemy import ModelSchema
2+
from models.Analytics import Analytics
3+
4+
5+
class AnalyticsSchema(ModelSchema):
6+
class Meta:
7+
model = Analytics

Diff for: models/Analytics.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from database.db import Base
2+
from sqlalchemy import Column, String, Float
3+
from sqlalchemy.orm import relationship
4+
from sqlalchemy import ForeignKey
5+
6+
class Analytics(Base):
7+
__tablename__ = 'Analytics'
8+
id = Column(String, primary_key=True)
9+
10+
#review = relationship("Review", ForeignKey("Review.id"))
11+
12+
sentiment_score = Column(Float)
13+
sentiment_type = Column(String(30))
14+
15+
anger = Column(Float)
16+
disgust = Column(Float)
17+
fear = Column(Float)
18+
joy = Column(Float)
19+
sadness = Column(Float)
20+
21+
openness = Column(Float)
22+
conscientiousness = Column(Float)
23+
extraversion = Column(Float)
24+
neuroticism = Column(Float)
25+
aggreablesness = Column(Float)
26+
27+
def __repr__(self):
28+
return "<id:" + str(self.id) + " " + "anger" + " " + str(self.anger) + ">"

Diff for: requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Fabric==1.8.2
21
Flask==0.10.1
32
Flask-SQLAlchemy==1.0
43
Flask-WTF==0.9.4
@@ -16,4 +15,5 @@ pycrypto==2.6.1
1615
wsgiref==0.1.2
1716
psycopg2==2.6.2
1817
marshmallow-sqlalchemy
19-
flask-marshmallow
18+
flask-marshmallow
19+
watson-developer-cloud

Diff for: sample.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
from watson_developer_cloud import AlchemyLanguageV1
3+
import json
4+
ALCHEMY_API_KEY = '07551e54797d7b593f3595653a7cad5b1803d3a6'
5+
alchemy_language = AlchemyLanguageV1(api_key='07551e54797d7b593f3595653a7cad5b1803d3a6')
6+
7+
sentiment = alchemy_language.sentiment(text='Hello, you suck dude!')
8+
9+
10+
print sentiment['docSentiment']['type']

0 commit comments

Comments
 (0)