# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Spooky Author Identification

import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline

# ## Import training data

data = pd.read_csv("../../data/train.csv")

data.info()

data.groupby('author').size()

data.head()

# ## Number of characters

data['characters'] = data['text'].str.len()

data['characters'].hist(bins=range(0,1000,20))

# ## Number of words

data['words'] = data['text'].str.split().apply(len)

data['words'].hist(bins=range(0,200,5))

# ## Number of semicolons

data['semicolons_freq'] = (data['text'].str.count(';') / data['characters'])*100

data.groupby('author')['semicolons_freq'].mean()

# ## Remove punctuation and stopwords

import nltk

nltk.download('stopwords')

stop = nltk.corpus.stopwords.words('english')

from string import punctuation

punctuation

translate = str.maketrans(punctuation,' '*len(punctuation))

data['stripped'] = data['text'].str.translate(translate).str.lower().str.split().map(lambda x: " ".join([word for word in x if word not in stop]))

# ## Fraction of unique words

data['unique_words'] = data['stripped'].str.split().map(lambda x: len(set(x))/(len(x) + 1)*100)

data.head()

# ## Number of sentences per passage

data['sentences'] = data['text'].str.count('\.')

# ## Sentence length

data['sentence_length'] = data['words'] / (data['sentences'] + 1)

# ## Try a model

from sklearn.model_selection import train_test_split

from sklearn.metrics import log_loss

data.columns

features = ['characters', 'words', 'semicolons_freq','unique_words', 'sentences', 'sentence_length']
target = 'author'

X = data[features]
y = data[target]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

from sklearn.ensemble import RandomForestClassifier as RFC

clf = RFC()

clf.fit(X_train,y_train)

clf.classes_

y_true = pd.get_dummies(y_test.map({'EAP':0, 'HPL': 1, 'MWS': 2}))
y_pred = clf.predict_proba(X_test[features])

log_loss(y_true,y_pred)

y_pred



StarcoderdataJupyterScriptsDedupFiltered