Appuity — Sentiment Analysis of reviews using Python API
Appuity — A small flask API which would read the comments. Do a sentiment analysis of the comments and provide the analysis in json format. In this post, we will be only setting up the project structure.
First, we will create a project directory. Let’s say ‘appuity_tutorial’. cd into the directory. Create a virtual environment folder, say, ‘appuity_venv’. Just type
$ virtualenv appuity_venv
Click here, if you want to know about setting up virtual environment.
Star the virtual environment using the command (on windows):
appuity_venv\Scripts\activate
Now install flask & flask-restful & execute
pip freeze > requirements.txt
this will create requirements.txt file inside appuity_tutorial.
Create another folder, ‘api’ inside the appuity_tutorial folder.
cd into ‘api’.
Create a file server.py in the api folder & the following content into it. Make sure you have flask & flask_restful installed. You can copy this code from here.

Now run this file:
python server.py
If you don’t get any error, you can visit http:127.0.0.1:5000 & you will see a message like this :

We are all set to go.
Create a folder resources into the api folder. Create a file into resources folder named as index.py. Put following content into that.
from flask_restful import Resource
class Index(Resource):
def get(self):
return {‘msg’: ‘Appuity api up and running.’}
Change the server.py file to look something like this.
from flask import Flask, jsonify
from flask_restful import Resource, Apifrom resources import Index
app = Flask(__name__)
api = Api(app)api.add_resource(Index, ‘/’)
if __name__ == ‘__main__’:
app.run(debug=True)
CRUD Operations
Now since our api would be making use of Database. We can use any ORM to save ourselves from writing long SQL queries. For this we will use orator ORM. It’s a very simple but powerful ORM. It supports PostgreSQL which we will be using for our API. Also it supports migrations. Migrations ?? What is that ??
Migrations are nothing but a version control for Databases. They make the applications easy to re-deploy and distribute to other (teammates).
But before that we will create a file, say, base.py file inside models folder that will handle all database operations, like migrations.
from orator import Model, DatabaseManager
from config import databasesdb = DatabaseManager(databases)
Model.set_connection_resolver(db)class Base(Model):
pass
We are just using the orator Model & putting the database configurations into db object.
For migrations to work we will need either a .yml file or an .py file which will have the information about our database. We will create one file called config.py & put the following content into that. Adding the username & password.
databases = {
‘postgres’: {
‘driver’: ‘postgres’,
‘host’: ‘localhost’,
‘port’: 5432,
‘database’: ‘appuity_dev’,
‘user’: ‘’,
‘password’: ‘’
}
}
We will create a database called as appuity_dev in postgres database using a simple command create databse appuity_dev in psql.
We will run a command
orator make:migration create_authors_table — table=authors — create
By default, the migration will be placed in a migrations folder relative to where the command has been executed, and will contain a timestamp which allows the framework to determine the order of the migrations.
It will create a file with timestamp followed by ‘create_authors_table’ as its name. Now we will add the required two columns as our requirement. Finally, it would look like this
from orator.migrations import Migration
class CreateAuthorsTable(Migration):
def up(self):
with self.schema.create(‘authors’) as table:
table.increments(‘id’)
table.string(‘name’)
table.long_text(‘avatar_url’)
table.timestamps()def down(self):
self.schema.drop(‘authors’)
Now, we will create the model class of our ‘Authors’.
Orator will automatically use the authors table for the model. By convention, it uses the pluralized lowercased version of the model name as the table name. We can change this by using the __table__ property.
from .base import Base
class Author(Base):
__table__ = ‘authors’
__fillable__ = [‘name’, ‘avatar_url’]
The __fillable__ property tells Orator which attributes can be mass-assigned using the create and update methods.
— — We will be adding to this in the next post. — —