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

# + [markdown] id="J_nQixLUYpPf"
# # Collect the Dataset

# + [markdown] id="0NxnqJsbbgX5"
# We use, for example, the dataset present in the article, but if you want to train a model in your dataset, you must use your dataset. Or, if you're going to use the dataset present in the article with another model, you only must change the model

# + colab={"base_uri": "https://localhost:8080/"} id="3gdX0jFkYyF9" executionInfo={"status": "ok", "timestamp": 1619189008697, "user_tz": 180, "elapsed": 1375, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}} outputId="8b7fb30d-8c23-4fb2-afb7-5a5bcb5dacd1"
# !wget https://raw.githubusercontent.com/adailtonaraujo/app_review_analysis/master/Classification/Dataset/RevisoesSoftware.json

# + id="IKHt1vhzyiwF" executionInfo={"status": "ok", "timestamp": 1619189011967, "user_tz": 180, "elapsed": 643, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
import pandas as pd
import json

with open('RevisoesSoftware.json', 'r') as f:
  data = json.load(f)

df_complete = pd.DataFrame(data)

# + [markdown] id="CNX0xE2rwWxP"
# # Bag-of-Words
#
#
#

# + [markdown] id="n7-vb0N8yJjZ"
# ## Imports

# + colab={"base_uri": "https://localhost:8080/"} id="9F9q0KfNwx4J" executionInfo={"status": "ok", "timestamp": 1619189016499, "user_tz": 180, "elapsed": 3418, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}} outputId="d810b018-52fe-4991-c7be-2e8d2a8f24e8"
import re
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem.snowball import SnowballStemmer
nltk.download('stopwords') 
nltk.download('punkt') 
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer


# + [markdown] id="cmXXzi-iyR2X"
# ## Irrelevant expressions

# + id="ZrZyZxRcxhQf" executionInfo={"status": "ok", "timestamp": 1619189016503, "user_tz": 180, "elapsed": 1663, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
def list_expressions():
  lista = ['***','****','*****','***great','*bold*','*cough*','*that*','-.-','-^_^-','-_-','-no','...','..now','..quick','.99','.but','.doc','.if','.it','.love','.tri','.when','.wont','////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\','///update///','12','===','^-^','^_^','^~^','a++','a+++']
  return lista


# + [markdown] id="JrQ1UK9EyWQM"
# ## Tokenizer

# + id="X4RizQm5xk89" executionInfo={"status": "ok", "timestamp": 1619189016868, "user_tz": 180, "elapsed": 760, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
def tokenize(text):
  lista_exp = list_expressions()
  
  p = re.compile('\d')

  tokens = nltk.word_tokenize(text)

  stems  = []
  for item in tokens:
    auxiliar = 0
    for expressao in lista_exp:
      if item == expressao:
        auxiliar = 1
        break
    if len(item) > 2 and not p.match(item) and auxiliar == 0:  
      stems.append(SnowballStemmer("english").stem(item))
  return stems


# + [markdown] id="YrTgzr9lyaQ3"
# ## Bag-of-words term weights

# + id="cf5qD4UixqUh" executionInfo={"status": "ok", "timestamp": 1619189018457, "user_tz": 180, "elapsed": 590, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
stop_words = nltk.corpus.stopwords.words('english') 

dic_tw = {
    'TF' : CountVectorizer(tokenizer=tokenize, stop_words=stop_words, ngram_range=(1,1)),
    'TF-IDF' : TfidfVectorizer(tokenizer=tokenize, stop_words=stop_words, ngram_range=(1,1)),
    'Binary' : CountVectorizer(tokenizer=tokenize, stop_words=stop_words, ngram_range=(1,1), binary=True),
    'TF-Bigram' : CountVectorizer(tokenizer=tokenize, stop_words=stop_words, ngram_range=(1,2)),
    'TFIDF-Bigram' : TfidfVectorizer(tokenizer=tokenize, stop_words=stop_words, ngram_range=(1,2)),
    'Binary-Bigram' : CountVectorizer(tokenizer=tokenize, stop_words=stop_words, ngram_range=(1,2), binary=True)
}

# + [markdown] id="R0yyYAa4YzDT"
# # functions to Train the Model
#
#

# + [markdown] id="BjeMtuov20T_"
# ## import models

# + id="IdJBZW_122c9" executionInfo={"status": "ok", "timestamp": 1619189020984, "user_tz": 180, "elapsed": 961, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
from sklearn.svm import OneClassSVM as OCSVM

# + [markdown] id="zmSBKaJh4J_V"
# ## Define the algorithm that you will use

# + id="JlNqAcFBZKMp" executionInfo={"status": "ok", "timestamp": 1619189020986, "user_tz": 180, "elapsed": 453, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
clf = OCSVM()

# + [markdown] id="Am_FWlsu4Ybx"
# ## Train-Test division

# + [markdown] id="qRk_-LIxeXGp"
# First, you must define the train and the test set. *test_size* define the percent of examples of test set, consequently, the train set size is 1 - *test_size*

# + id="oG57wwK3efuZ" executionInfo={"status": "ok", "timestamp": 1619189024296, "user_tz": 180, "elapsed": 779, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
from sklearn.model_selection import train_test_split

class_interest = 'Rating'

df_train_interest, df_test_interest = train_test_split(df_complete['comment'][df_complete['label'] == class_interest],test_size=0.25, random_state=42)
df_test_outliers = df_complete['comment'][df_complete['label'] != class_interest]

# + [markdown] id="zA7uzEHd431r"
# # Execution

# + [markdown] id="jhQyVqYs5KXi"
# ## Pre-processing

# + colab={"base_uri": "https://localhost:8080/"} id="ERNQOFtc50Wz" executionInfo={"status": "ok", "timestamp": 1619189029863, "user_tz": 180, "elapsed": 3719, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}} outputId="fa88436b-bdd2-446b-cadd-ff49d21fed35"
vectorizer = dic_tw['TF-IDF']

vectorizer.fit(df_train_interest)

x_train = vectorizer.transform(df_train_interest).toarray()

x_test_interest = vectorizer.transform(df_test_interest).toarray()

x_test_outlier = vectorizer.transform(df_test_outliers).toarray()

# + [markdown] id="qrdHGCwR5Vpz"
# ## Train

# + colab={"base_uri": "https://localhost:8080/"} id="NO5Vb881dndy" executionInfo={"status": "ok", "timestamp": 1619189043073, "user_tz": 180, "elapsed": 11559, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}} outputId="a70abe62-02f5-464c-9307-a2544d5b7e79"
clf.fit(x_train)

# + [markdown] id="-iKPkmam5cNK"
# ### Saving the model

# + id="gJfzvMw3iRYH"
import pickle

pkl_filename = "pickle_OCSVM_TFIDF.pkl"
with open(pkl_filename, 'wb') as file:
    pickle.dump(clf, file) 

# + [markdown] id="DMtAWpRFia9s"
# if you want to load the model, use:
#
# with open(pkl_filename, 'rb') as file: \\
#     clf = pickle.load(file)

# + [markdown] id="1Sy9D_gdeTUm"
# ## Test

# + id="YVe9kmaDi8fY" executionInfo={"status": "ok", "timestamp": 1619189050996, "user_tz": 180, "elapsed": 11080, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
y_pred_int = clf.predict(x_test_interest)
y_pred_out = clf.predict(x_test_outlier)

# + id="cb9cmP5Ns7xh" executionInfo={"status": "ok", "timestamp": 1619189052100, "user_tz": 180, "elapsed": 1099, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
from sklearn.metrics import classification_report

def evaluation_one_class(preds_interest, preds_outliers):
  y_true = [1]*len(preds_interest) + [-1]*len(preds_outliers)
  y_pred = list(preds_interest)+list(preds_outliers)
  return classification_report(y_true, y_pred, output_dict=False)


# + colab={"base_uri": "https://localhost:8080/"} id="Ok8sHMafLecV" executionInfo={"status": "ok", "timestamp": 1619189054089, "user_tz": 180, "elapsed": 753, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}} outputId="af76106a-e620-414d-cc89-5f5ac6222b40"
print(evaluation_one_class(y_pred_int, y_pred_out))

# + [markdown] id="YM8PUukpCISB"
# # Case Study

# + id="6F7T6ELaCXFA" executionInfo={"status": "ok", "timestamp": 1619189059133, "user_tz": 180, "elapsed": 773, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
texts = ['the app always crashes !!!!!!!!!!', 'I loved this app!!']


# + id="FEJ-aV8MCOql" executionInfo={"status": "ok", "timestamp": 1619189105356, "user_tz": 180, "elapsed": 620, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}}
def Classification(text):
  bow_test = vectorizer.transform([text]).toarray()
  resp = clf.predict(bow_test)
  if resp[0] == 1:
    print('The text: "' + text + '" BELONGS to the class of interest!') 
  if resp[0] == -1:
    print('The text: "' + text + '" DOES NOT belong to the class of interest!') 


# + colab={"base_uri": "https://localhost:8080/"} id="YX0F0gkjCVb7" executionInfo={"status": "ok", "timestamp": 1619189106693, "user_tz": 180, "elapsed": 742, "user": {"displayName": "<NAME>\u00f4lo", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GibCQ81nR0vkq2Wa2jhFtwL5ubCet_iRF_FzWcukA=s64", "userId": "00854347238887874462"}} outputId="745a24fe-1466-4227-81bb-109ee351f229"
for text in texts:
  Classification(text)

StarcoderdataJupyterScriptsDedupFiltered