<jupyter_start><jupyter_text>Import Non-Preprocessed Data<jupyter_code>import os

import numpy as np
import tensorflow as tf
from sklearn.datasets import load_files
import pandas as pd

import autokeras as ak

from google.colab import drive 
drive.mount('/content/gdrive')
df=pd.read_csv('gdrive/My Drive/Licenta/Data/politifact_clean_binarized.csv')
df = df[['statement', 'veracity']]
df.head()
train_data = df[:-1000]
test_data = df[-1000:]
print(train_data.shape, test_data.shape)
x_train = np.array(train_data.statement)
y_train = np.array(train_data.veracity)
x_test = np.array(test_data.statement)
y_test = np.array(test_data.veracity)

print(x_train.shape)  # (25000,)
print(y_train.shape)  # (25000, 1)
print(x_train[0][:50])  # this film was just brilliant casting<jupyter_output>(10188,)
(10188,)
Sen. <NAME> is "supporting the animals of M<jupyter_text>The second step is to run the [TextClassifier](/text_classifier).  As a quickdemo, we set epochs to 2.  You can also leave the epochs unspecified for anadaptive number of epochs.  Train and Evaluate on Politifact Binarized Clean<jupyter_code>callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)
# Initialize the text classifier.
clf = ak.TextClassifier(
    overwrite=True, max_trials=2
)  # It only tries 1 model as a quick demo.
# Feed the text classifier with training data.
clf.fit(x_train, y_train, callbacks=[callback], epochs=3)
# Predict with the best model.
predicted_y = clf.predict(x_test)
# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))<jupyter_output>Trial 3 Complete [04h 26m 24s]
val_loss: 0.6354480981826782

Best val_loss So Far: 0.6354480981826782
Total elapsed time: 04h 30m 22s

Search: Running Trial #4

Hyperparameter    |Value             |Best Value So Far 
text_block_1/bl...|bert              |bert              
classification_...|0                 |0                 
optimizer         |adam_weight_decay |adam_weight_decay 
learning_rate     |2e-05             |2e-05             
text_block_1/ma...|20000             |20000             
text_block_1/be...|512               |512               

Epoch 1/3
 78/256 [========>.....................] - ETA: 59:41 - loss: 0.6340 - accuracy: 0.6900<jupyter_text>Import Data and Preprocess It We want to see if preprocessing improves performance of AutoML models.<jupyter_code>!pip install -U spacy
import sys
!{sys.executable} -m spacy download en_core_web_lg
# Packages for Data Manipulation
import numpy as np
import pandas as pd
import os
from collections import Counter
import spacy

# for NLP
import re
import unicodedata
from bs4 import BeautifulSoup

# to see all the cells and data in the dataframes
pd.set_option('display.width', 500)
pd.set_option('display.max_columns', 300)
pd.set_option('display.max_colwidth', 300) 

# to hide warnings about train/test size for train_test_split
import warnings
warnings.filterwarnings('ignore')

df=pd.read_csv('gdrive/My Drive/Licenta/Data/politifact_clean_binarized.csv')
df = df[['statement', 'veracity']]
df.head()
# this cell is most useful for imbalanced data
# ratio of true to false in the data set (i.e., our "chance" baseline)

print("true statements: ", round(len(df.veracity[df.veracity == True]),2))
print("false statements: ", round(len(df.veracity[df.veracity == False]),2))

tru_count = len(df.veracity[df.veracity == True])
fal_count = len(df.veracity[df.veracity == False])

print("chance of truth baseline: ", round(tru_count/(tru_count + fal_count),2)) # len(df) as denominator also works
print("chance of false baseline: ", round(fal_count/(tru_count + fal_count),2))<jupyter_output>true statements:  4853
false statements:  6335
chance of truth baseline:  0.43
chance of false baseline:  0.57<jupyter_text>Create Normalized Text<jupyter_code>nlp = spacy.load('en_core_web_lg')

def strip_html_tags(text):
            soup = BeautifulSoup(text, "html.parser")
            [s.extract() for s in soup(["iframe", "script"])]
            stripped_text = soup.get_text()
            stripped_text = re.sub(r'[\r|\n|\r\n]+', '\n', stripped_text)
            return stripped_text
        
def remove_accented_characters(text):
    text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode('utf-8', 'ignore')
    return text
# lemmatization function
def normalize(text, 
              remove_html = False, 
              lowercase = False,
              lemmatize = False,
              remove_extra_newlines = False,
              remove_stopwords = False,
              remove_non_alphabetic = False,
              remove_accented_char = False,
              ):
        
    # remove html
    if remove_html:
        text = strip_html_tags(text)

    # lowercase all text
    if lowercase:
        text = text.lower()
        
    # lemmatize
    if lemmatize:        
        text = nlp(text)
        text = ' '.join([word.lemma_ if word.lemma_ != '-PRON-' else word.text for word in text])
        
    # remove extra new lines    
    if remove_extra_newlines:
        text = re.sub(r'[\r|\n|\r\n]+', ' ', text)
    
    # remove stopwords
    if remove_stopwords:
        stopwords = spacy.lang.en.stop_words.STOP_WORDS
        text = nlp(text)
        text = ' '.join([token.text for token in text if token.text not in stopwords])
    
    # remove non-alphabetic characters
    if remove_non_alphabetic:
        text = nlp(text)
        text = ' '.join([token.text for token in text if token.text.isalpha()])
        
    # remove accented characters
    if remove_accented_char:
        text = remove_accented_characters(text)

    return text
# sample of light normalizer
string = "@ ! i DON'TTT, won't, a féél    can't not USING the  NLP 27x maaaah?"

normalize(string, 
          remove_html = True, 
          lowercase = True,
          remove_extra_newlines = True,
          remove_accented_char = True)
df["light_normalization"] = df.statement.apply(lambda x : normalize(x, 
                                                                  remove_html = True, 
                                                                  lowercase = True,
                                                                  remove_extra_newlines = True,
                                                                  remove_accented_char = True))
# sample of full normalizer
normalize(string, 
          remove_html = True, 
          lowercase = True,
          remove_extra_newlines = True,
          remove_accented_char = True,
          lemmatize = True, # note that lemmatize does not work properly if text is not lowercased first
          remove_stopwords = True,
          remove_non_alphabetic = True) 

# non_alphabetic will remove not only non-alphabetic characters, but also blank spaces
df["full_normalization"] = df.statement.apply(lambda x : normalize(x, 
                                                                    remove_html = True, 
                                                                    lowercase = True,
                                                                    remove_extra_newlines = True,
                                                                    remove_accented_char = True,
                                                                    lemmatize = True,
                                                                    remove_stopwords = True,
                                                                    remove_non_alphabetic = True))
df.head(3)<jupyter_output><empty_output><jupyter_text>Training and Evaluating with Light Normalization<jupyter_code>train_data = df[:-1000]
test_data = df[-1000:]
print(train_data.shape, test_data.shape)
x_train = np.array(train_data.light_normalization)
y_train = np.array(train_data.veracity)
x_test = np.array(test_data.light_normalization)
y_test = np.array(test_data.veracity)

print(x_train.shape)  # (25000,)
print(y_train.shape)  # (25000, 1)
print(x_train[0][:50])  # this film was just brilliant casting
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)
# Initialize the text classifier.
clf = ak.TextClassifier(
    overwrite=True, max_trials=2
)  # It only tries 1 model as a quick demo.
# Feed the text classifier with training data.
clf.fit(x_train, y_train, callbacks=[callback], epochs=2)
# Predict with the best model.
predicted_y = clf.predict(x_test)
# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))<jupyter_output><empty_output><jupyter_text>Training and Evaluating with Full Normalization<jupyter_code>train_data = df[:-1000]
test_data = df[-1000:]
print(train_data.shape, test_data.shape)
x_train = np.array(train_data.full_normalization)
y_train = np.array(train_data.veracity)
x_test = np.array(test_data.full_normalization)
y_test = np.array(test_data.veracity)

print(x_train.shape)  # (25000,)
print(y_train.shape)  # (25000, 1)
print(x_train[0][:50])  # this film was just brilliant casting
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=1)
# Initialize the text classifier.
clf = ak.TextClassifier(
    overwrite=True, max_trials=2
)  # It only tries 1 model as a quick demo.
# Feed the text classifier with training data.
clf.fit(x_train, y_train, callbacks=[callback], epochs=3)
# Predict with the best model.
predicted_y = clf.predict(x_test)
# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))
df.shape
df.head()
## Save csv so we don't have to repeat the process of normalization
df.to_csv('politifact_binarized_light_full_normalization.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09955361777224125, 'fraction_numerical': 0.02252673102875532, 'mean_word_length': 2.6841300191204587, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '604451', 'license': 'MIT', 'n_tokens_mistral': 3137, 'n_tokens_neox': 2919, 'n_words': 756, 'path': '3. Machine Learning & AutoKeras/3_c_AutoKeras_Truth_Detection_Text_Classification.ipynb', 'repo_name': 'the-sergiu/TruthDetection', 'size': '9640'}