<jupyter_start><jupyter_text>Develop Deep Learning Models for Natural Language in Python  Chapter 10 - Project: Develop a Neural Bag-of-Words Model for Sentimental Analysis<jupyter_code>import re
import os
import numpy as np
from random import shuffle<jupyter_output><empty_output><jupyter_text>10.4 - Bag-of-Words ReprRepresentation  Load Data<jupyter_code># Most Data preperation was done on chapter 9. We just load the cleaned data

root = '/content/drive/MyDrive/NLP-In-Depth/Develop Deep Learning Models for Natural Language in Python/txt_sentoken/'

def load_clean_data(root):

  positive_reviews, negative_reviews, vocab = None, None, None

  with open(root + '/positive.txt', 'r') as file:
    positive_reviews = file.read().split('\n')

  with open(root + '/negative.txt', 'r') as file:
    negative_reviews = file.read().split('\n')

  with open(root + '/vocab.txt', 'r') as file:
    vocab = file.read().split('\n')

  data = positive_reviews + negative_reviews
  labels = [1 for _ in range(len(positive_reviews))] + [0 for _ in range(len(negative_reviews))]

  return data, labels, vocab<jupyter_output><empty_output><jupyter_text>Split Data into Training, validation and test set<jupyter_code>def split_data(data, labels, split = 200):

  # Make tuple
  reviews_data = [(data[i], labels[i]) for i in range(len(data))]

  # Split
  train_data = reviews_data[split:]
  test_data = reviews_data[:split]

  # Shuffle
  shuffle(train_data)
  shuffle(test_data)

  return train_data, test_data
data, labels, vocab = load_clean_data(root)
train_data, test_data = split_data(data, labels, 200)<jupyter_output><empty_output><jupyter_text>Keras Tokenizer API<jupyter_code>from keras.preprocessing.text import Tokenizer

def create_tokenizer(vocab):
  tokenizer = Tokenizer()
  tokenizer.fit_on_texts(vocab)
  return tokenizer

# Get raw reviews
train_texts = [review for (review, label) in train_data]
test_texts = [review for (review, label) in test_data]

# Get labels
train_labels = [label for (review, label) in train_data]
test_labels = [label for (review, label) in test_data]

# Create tokenizer (With predefined vocabulary)
tokenizer = create_tokenizer(vocab)

# Convert texts to matrix
X_train = tokenizer.texts_to_matrix(train_texts, mode = 'freq')
X_test = tokenizer.texts_to_matrix(test_texts, mode = 'freq')

print(X_train.shape, X_test.shape)<jupyter_output>(1800, 26791) (200, 26791)<jupyter_text>10.5 - First Sentimental Analysis Model<jupyter_code>from keras.models import Model
from keras.layers import Dense
from keras import Sequential
from keras.utils.vis_utils import plot_model

n_words = X_train.shape[1]

model = Sequential()
model.add(Dense(units = 50, input_shape = (n_words,), activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))

model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
model.summary()
plot_model(model, show_shapes = True)
# Train model
model.fit(X_train, np.array(train_labels), epochs = 10, verbose = 2)
# Evaluate Model
loss, acc = model.evaluate(X_test, np.array(test_labels), verbose = 2)
print('Accuracy:', acc * 100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11473851030110935, 'fraction_numerical': 0.014263074484944533, 'mean_word_length': 5.816414686825054, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '65664', 'license': 'Apache-2.0', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1067, 'n_words': 278, 'path': 'Develop Deep Learning Models for Natural Language in Python/Chapter 10 - Project_ Develop a Neural Bag-of-Words Model for Sentimental Analysis.ipynb', 'repo_name': 'keivanipchihagh/NLP-In-Depth', 'size': '3155'}