<gh_stars>0
import json
import re

import joblib
import pandas as pd
import plotly
from flask import Flask
from flask import render_template, request
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from plotly.graph_objs import Bar, Scatter
from sqlalchemy import create_engine

app = Flask(__name__)


def tokenize(text):
    """
    Tokenize the text input removing stopwords and lemmatizing the text.
    :param text: String with the text data.
    :return: List with all the tokens.
    """
    # normalize case and remove punctuation
    text = re.sub(r'[^a-zA-Z0-9]', ' ', text.lower())
    # tokenize text
    tokens = word_tokenize(text)

    stop_words = stopwords.words("english")
    lemmatizer = WordNetLemmatizer()
    # lemmatize and remove stop words
    tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
    return tokens

# load data
engine = create_engine('sqlite:///../data/DisasterResponse.db')
df = pd.read_sql_table('messages', engine)

# load model
model = joblib.load("../models/classifier.pkl")


# index webpage displays cool visuals and receives user input text for model
@app.route('/')
@app.route('/index')
def index():
    
    # extract data needed for visuals
    genre_counts = df.groupby('genre').count()['message']
    genre_names = list(genre_counts.index)

    categories = df.drop(['id', 'message', 'original', 'genre'], axis=1)
    categories_names = categories.columns
    categories_counts = categories.sum().values

    message_words = list(df.iloc[:1000]['message'].str.split().str.len())
    original_words = list(df.iloc[:1000]['original'].str.split().str.len())
    message_names = list(df['id'])
    
    # create visuals
    graphs = [
        {
            'data': [
                Bar(
                    x=genre_names,
                    y=genre_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Message Genres',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Genre"
                }
            }
        },
        {
            'data': [
                Scatter(
                    x=categories_names,
                    y=categories_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Message Categories',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Category"
                }
            }
        },
        {
            'data': [
                Scatter(
                    x=message_names,
                    y=message_words,
                    name='Message Column'
                ),
                Scatter(
                    x=message_names,
                    y=original_words,
                    name="Original Column"
                )
            ],

            'layout': {
                'title': 'Distribution of Messages Words',
                'yaxis': {
                    'title': "Words"
                },
                'xaxis': {
                    'title': "Message ID"
                }
            }
        }
    ]
    
    # encode plotly graphs in JSON
    ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)]
    graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
    
    # render web page with plotly graphs
    return render_template('master.html', ids=ids, graphJSON=graphJSON)


# web page that handles user query and displays model results
@app.route('/go')
def go():
    # save user input in query
    query = request.args.get('query', '') 

    # use model to predict classification for query
    classification_labels = model.predict([query])[0]
    classification_results = dict(zip(df.columns[4:], classification_labels))

    # This will render the go.html Please see that file. 
    return render_template(
        'go.html',
        query=query,
        classification_result=classification_results
    )


def main():
    app.run(host='0.0.0.0', port=3001, debug=True)


if __name__ == '__main__':
    main()

import json
import re

import joblib
import pandas as pd
import plotly
from flask import Flask
from flask import render_template, request
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from plotly.graph_objs import Bar, Scatter
from sqlalchemy import create_engine

app = Flask(__name__)


def tokenize(text):
    """
    Tokenize the text input removing stopwords and lemmatizing the text.
    :param text: String with the text data.
    :return: List with all the tokens.
    """
    # normalize case and remove punctuation
    text = re.sub(r'[^a-zA-Z0-9]', ' ', text.lower())
    # tokenize text
    tokens = word_tokenize(text)

    stop_words = stopwords.words("english")
    lemmatizer = WordNetLemmatizer()
    # lemmatize and remove stop words
    tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
    return tokens

# load data
engine = create_engine('sqlite:///../data/DisasterResponse.db')
df = pd.read_sql_table('messages', engine)

# load model
model = joblib.load("../models/classifier.pkl")


# index webpage displays cool visuals and receives user input text for model
@app.route('/')
@app.route('/index')
def index():
    
    # extract data needed for visuals
    genre_counts = df.groupby('genre').count()['message']
    genre_names = list(genre_counts.index)

    categories = df.drop(['id', 'message', 'original', 'genre'], axis=1)
    categories_names = categories.columns
    categories_counts = categories.sum().values

    message_words = list(df.iloc[:1000]['message'].str.split().str.len())
    original_words = list(df.iloc[:1000]['original'].str.split().str.len())
    message_names = list(df['id'])
    
    # create visuals
    graphs = [
        {
            'data': [
                Bar(
                    x=genre_names,
                    y=genre_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Message Genres',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Genre"
                }
            }
        },
        {
            'data': [
                Scatter(
                    x=categories_names,
                    y=categories_counts
                )
            ],

            'layout': {
                'title': 'Distribution of Message Categories',
                'yaxis': {
                    'title': "Count"
                },
                'xaxis': {
                    'title': "Category"
                }
            }
        },
        {
            'data': [
                Scatter(
                    x=message_names,
                    y=message_words,
                    name='Message Column'
                ),
                Scatter(
                    x=message_names,
                    y=original_words,
                    name="Original Column"
                )
            ],

            'layout': {
                'title': 'Distribution of Messages Words',
                'yaxis': {
                    'title': "Words"
                },
                'xaxis': {
                    'title': "Message ID"
                }
            }
        }
    ]
    
    # encode plotly graphs in JSON
    ids = ["graph-{}".format(i) for i, _ in enumerate(graphs)]
    graphJSON = json.dumps(graphs, cls=plotly.utils.PlotlyJSONEncoder)
    
    # render web page with plotly graphs
    return render_template('master.html', ids=ids, graphJSON=graphJSON)


# web page that handles user query and displays model results
@app.route('/go')
def go():
    # save user input in query
    query = request.args.get('query', '') 

    # use model to predict classification for query
    classification_labels = model.predict([query])[0]
    classification_results = dict(zip(df.columns[4:], classification_labels))

    # This will render the go.html Please see that file. 
    return render_template(
        'go.html',
        query=query,
        classification_result=classification_results
    )


def main():
    app.run(host='0.0.0.0', port=3001, debug=True)


if __name__ == '__main__':
    main()
Tokenize the text input removing stopwords and lemmatizing the text.     :param text: String with the text data.     :return: List with all the tokens. # normalize case and remove punctuation # tokenize text # lemmatize and remove stop words # load data # load model # index webpage displays cool visuals and receives user input text for model # extract data needed for visuals # create visuals # encode plotly graphs in JSON # render web page with plotly graphs # web page that handles user query and displays model results # save user input in query # use model to predict classification for query # This will render the go.html Please see that file.