#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 13:38:01 2020

@author: Dr. Lynn Marie Houston
Citation: Code adapted from pemagrg1’s Github page here: https://github.com/pemagrg1/text-classification/blob/master/text_classification.ipynb. While she developed code to analyze categories of news stories, I revised it to apply to the SMS spam data set.
"""

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
import string
from nltk.stem import PorterStemmer
from nltk import word_tokenize
from sklearn.pipeline import Pipeline
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
stemmer = PorterStemmer()

def stemming_tokenizer(text):
    stemmer = PorterStemmer()
    return [stemmer.stem(w) for w in word_tokenize(text)]

def train(classifier, X, y,predict_text):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=33)
    classifier.fit(X_train, y_train)
    prediction = (classifier.predict(predict_text))
    print ("Accuracy: %s" % classifier.score(X_test, y_test))
    print ("predicted category: ",prediction)

df = pd.read_csv('~/opt/anaconda3/lib/python3.7/site-packages/sklearn/datasets/spam.csv', encoding = "ISO-8859-1")
predict_text = ["We request customer re-register credit cards"]
actual_category = "spam"

print ("NAIVE MODEL")
naive_model = Pipeline([('vectorizer', TfidfVectorizer(tokenizer=stemming_tokenizer, stop_words=stopwords.words('english') + list(string.punctuation))),('classifier', MultinomialNB(alpha=0.05))])
print ("actual category", actual_category)
train(naive_model, df['TITLE'], df['CATEGORY'],predict_text)
