Text Classification for Tweet Dataset using Convolutional Neural Network

This is source code for Text Classification for Different Datasets CNN based on the code from
Text Classification, Part I – Convolutional Networks

# -*- coding: utf-8 -*-

import csv
import re
from nltk.corpus import stopwords

stopword_set = set(stopwords.words("english")) 

def preprocess(raw_text):

    # keep only words
    letters_only_text = re.sub("[^a-zA-Z]", " ", raw_text)

    # convert to lower case and split 
    words = letters_only_text.lower().split()

    # remove stopwords
   
    meaningful_words = [w for w in words if w not in stopword_set]

    # join the cleaned words in a list
    cleaned_word_list = " ".join(meaningful_words)

    return cleaned_word_list

def preprocess2(raw_text):
    stopword_set = set(stopwords.words("english"))
    return " ".join([i for i in re.sub(r'[^a-zA-Z\s]', "", raw_text).lower().split() if i not in stopword_set])


def get_index (key):
    	if (dict.has_key(key)):
         return dict[key]
    	else:
         dict_count=dict.values
         new_item = {key : dict_count }
         dict.update(new_item)
         return new_item
    

# from https://pythonspot.com/reading-csv-files-in-python/
def readMyFile(filename):
    text = []
    categories = []
    dict = {}
 
    with open(filename, encoding='latin-1') as csvDataFile:
        csvReader = csv.reader(csvDataFile)
        for row in csvReader:
          	 if (row[4] in dict):
          	    ind= dict[row[4]]
          	 else:
          	    dict_count=len(dict)
          	    new_item = {row[4] : dict_count }
          	    dict.update(new_item)
          	    ind=dict_count

          	 categories.append(ind)
        
          	 text.append(row[1] + " " + row[0] + " "+ row[6])
 
    return categories, text
 
fn="C:\\Users\\New-years-resolutions-DFE.csv"
labels,new_texts = readMyFile(fn)
 
print(new_texts[0])
print(new_texts[1])
print(labels[0])
print(labels[1])
print(labels)

texts=[]
for txt in new_texts:
    txt=preprocess2(txt)
    texts.append(txt)
 

import numpy as np
import os



os.environ['KERAS_BACKEND']='tensorflow'

from keras.preprocessing.text import Tokenizer

from keras.preprocessing.sequence import pad_sequences

from keras.utils.np_utils import to_categorical

from keras.layers import Dense, Input, Flatten

from keras.layers import Conv1D, MaxPooling1D, Embedding, Merge, Dropout

from keras.models import Model


MAX_SEQUENCE_LENGTH = 1000

MAX_NB_WORDS = 20000

EMBEDDING_DIM = 100

VALIDATION_SPLIT = 0.2

tokenizer = Tokenizer(nb_words=MAX_NB_WORDS)

tokenizer.fit_on_texts(texts)

sequences = tokenizer.texts_to_sequences(texts)

word_index = tokenizer.word_index

print('Found %s unique tokens.' % len(word_index))

data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)

labels = to_categorical(np.asarray(labels))

print('Shape of data tensor:', data.shape)

print('Shape of label tensor:', labels.shape)

indices = np.arange(data.shape[0])

np.random.shuffle(indices)

data = data[indices]

labels = labels[indices]

nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0])

x_train = data[:-nb_validation_samples]

y_train = labels[:-nb_validation_samples]

x_val = data[-nb_validation_samples:]

y_val = labels[-nb_validation_samples:]



print('Number of positive and negative reviews in traing and validation set ')

print (y_train.sum(axis=0))

print (y_val.sum(axis=0))

GLOVE_DIR = "C:\\Users\\pythonrunfiles"

embeddings_index = {}

f = open(os.path.join(GLOVE_DIR, 'glove.6B.100d.txt'), encoding="utf-8")

for line in f:
   
    values = line.split()

    word = values[0]

    coefs = np.asarray(values[1:], dtype='float32')

    embeddings_index[word] = coefs

f.close()



print('Total %s word vectors in Glove 6B 100d.' % len(embeddings_index))



embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM))

for word, i in word_index.items():

    embedding_vector = embeddings_index.get(word)

    if embedding_vector is not None:

        # words not found in embedding index will be all-zeros.

        embedding_matrix[i] = embedding_vector

        

embedding_layer = Embedding(len(word_index) + 1,

                            EMBEDDING_DIM,

                            weights=[embedding_matrix],

                            input_length=MAX_SEQUENCE_LENGTH,

                            trainable=True)



sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')

embedded_sequences = embedding_layer(sequence_input)

l_cov1= Conv1D(128, 5, activation='relu')(embedded_sequences)

l_pool1 = MaxPooling1D(5)(l_cov1)

l_cov2 = Conv1D(128, 5, activation='relu')(l_pool1)

l_pool2 = MaxPooling1D(5)(l_cov2)

l_cov3 = Conv1D(128, 5, activation='relu')(l_pool2)

l_pool3 = MaxPooling1D(35)(l_cov3)  # global max pooling

l_flat = Flatten()(l_pool3)

l_dense = Dense(128, activation='relu')(l_flat)

preds = Dense(11, activation='softmax')(l_dense)    # was 2 instead 11



model = Model(sequence_input, preds)

model.compile(loss='categorical_crossentropy',

              optimizer='rmsprop',

              metrics=['acc'])



print("model fitting - simplified convolutional neural network")

model.summary()

print (x_train)
print (y_train)

model.fit(x_train, y_train, validation_data=(x_val, y_val),

          nb_epoch=1, batch_size=128)       #epoch was 10



embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM))

for word, i in word_index.items():

    embedding_vector = embeddings_index.get(word)

    if embedding_vector is not None:

        # words not found in embedding index will be all-zeros.

        embedding_matrix[i] = embedding_vector

        

embedding_layer = Embedding(len(word_index) + 1,

                            EMBEDDING_DIM,

                            weights=[embedding_matrix],

                            input_length=MAX_SEQUENCE_LENGTH,

                            trainable=True)



# applying a more complex convolutional approach

convs = []

filter_sizes = [3,4,5]



sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')

embedded_sequences = embedding_layer(sequence_input)



for fsz in filter_sizes:

    l_conv = Conv1D(nb_filter=128,filter_length=fsz,activation='relu')(embedded_sequences)

    l_pool = MaxPooling1D(5)(l_conv)

    convs.append(l_pool)

    

l_merge = Merge(mode='concat', concat_axis=1)(convs)

l_cov1= Conv1D(128, 5, activation='relu')(l_merge)

l_pool1 = MaxPooling1D(5)(l_cov1)

l_cov2 = Conv1D(128, 5, activation='relu')(l_pool1)

l_pool2 = MaxPooling1D(30)(l_cov2)

l_flat = Flatten()(l_pool2)

l_dense = Dense(128, activation='relu')(l_flat)

preds = Dense(11, activation='softmax')(l_dense)     



model = Model(sequence_input, preds)

model.compile(loss='categorical_crossentropy',

              optimizer='rmsprop',

              metrics=['acc'])



print("model fitting - more complex convolutional neural network")

model.summary()

model.fit(x_train, y_train, validation_data=(x_val, y_val),

          nb_epoch=2, batch_size=50)