<jupyter_start><jupyter_text>Chapter 6 Deep Learning for NLP  Information Retrieval System Usages:1. Search Engines2. Document Retrieval systems 3. Passage Retrieval systems 4. Question Answering Systems<jupyter_code>Doc1 = ['''With the Union cabinet approving the amendments to the
Motor Vehicles Act, 2016, those caught for drunken driving will
have to have really deep pockets, as the fine payable in court
has been enhanced to Rs 10,000 for first-time offenders.''']
Doc2 = ['''"Natural language processing (NLP) is an area of
computer science and artificial intelligence concerned with the
interactions between computers and human (natural) languages,
in particular how to program computers to process and analyze
large amounts of natural language data.''']
Doc3 = ['''"He points out that public transport is very good in
Mumbai and New Delhi, where there is a good network of suburban
and metro rail systems.''']
Doc4 = ['''But the man behind the wickets at the other end was
watching just as keenly. With an affirmative nod from Dhoni,
India captain <NAME> promptly asked for a review. Sure
enough, the ball would have clipped the top of middle and leg.''']
import re
from nltk.corpus import stopwords
stp = stopwords.words('english')
def preprocess(text):
    text = text.lower()
    text = re.sub(r'[^a-zA-Z0-9\s]', ' ', text)
    #text = re.sub(r'[^\w\s]+',' ', text)
    text = ' '.join([word for word in text.split() if word not in stp])
    return text
path = r'E:\GoogleNews-vectors-negative300.bin'
import gensim
import numpy as np
w2vec = gensim.models.KeyedVectors.load_word2vec_format(path, binary=True)
def get_embeddings(word):
    if word in w2vec.vocab:
        return w2vec[word]
    else:
        return np.zeros(300)
# Average embeddings
fin = Doc1+Doc2+Doc3+Doc4
out_dict = {}
import nltk
for se in fin:
    average_vector = np.mean(np.array([get_embeddings(word) for word in nltk.word_tokenize(preprocess(se))]),axis=0)
    d = {se:average_vector}
    out_dict.update(d)
# Get similarity between query and documents vectorsout_dict
import scipy
def get_similarity(query, doc):
    cos_sim = np.dot(query, doc)/(np.linalg.norm(query)*np.linalg.norm(doc))
    return cos_sim
# Function to generate ranked documents
def Rank_text(query):
    query_vector = np.mean(np.array([get_embeddings(word) for word in nltk.word_tokenize(preprocess(query))]),axis=0)
    rank = []
    for k,v in out_dict.items():
        rank.append((k,get_similarity(query_vector, v)))
    rank = sorted(rank, key=lambda x:x[1], reverse=True)
    return rank
Rank_text('who was playing cricket')<jupyter_output><empty_output><jupyter_text>Text Classification Using Deep Learning Spam ham classification from sms dataset<jupyter_code>import pandas as pd
file_content = pd.read_csv('spam.csv', encoding = "ISO-8859-1")
dataset = file_content[['v2','v1']]
dataset.columns = ['features', 'target']
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from sklearn.preprocessing import LabelEncoder
X_train, X_test, y_train, y_test = train_test_split(dataset['features'],dataset['target'],test_size=0.2)
MAX_SEQUENCE_LENGTH = 300
MAX_NB_WORDS = 20000
EMB_DIM = 100
# Tokenize for word Embeddings
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
# Tokenize features
tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
tokenizer.fit_on_texts(X_train)
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
# Label encode targets and convert to categorical
lbl = LabelEncoder()
lbl.fit(y_train)
y_train.shape
y_train = lbl.transform(y_train)
y_test = lbl.transform(y_test)
y_train.shape
y_train = to_categorical(np.asarray(y_train))
y_train.shape
y_test = to_categorical(np.asarray(y_test))
# Padding
X_train = pad_sequences(X_train,maxlen=MAX_SEQUENCE_LENGTH)
X_test = pad_sequences(X_test,maxlen=MAX_SEQUENCE_LENGTH)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Model Building 1. CNN<jupyter_code>model = keras.models.Sequential([
    keras.layers.Embedding(input_dim=MAX_NB_WORDS, output_dim=EMB_DIM, input_length=MAX_SEQUENCE_LENGTH),
    keras.layers.Dropout(0.5),
    keras.layers.Conv1D(filters=128, kernel_size=5, activation='relu'),
    keras.layers.MaxPooling1D(5),
    keras.layers.Dropout(0.5),
    keras.layers.BatchNormalization(),
    keras.layers.Conv1D(filters=128, kernel_size=5, activation='relu'),
    keras.layers.MaxPooling1D(5),
    keras.layers.Dropout(0.5),
    keras.layers.BatchNormalization(),
    keras.layers.Flatten(),
    keras.layers.Dense(units=128, activation='relu'),
    keras.layers.Dense(units=2, activation='softmax')
])
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=64 ,epochs=5, validation_data=(X_test,y_test))
y_pred = model.predict(X_test)
y_pred
y_pred.round()
print(classification_report(y_test,y_pred.round()))<jupyter_output>precision    recall  f1-score   support

           0       0.92      1.00      0.96       972
           1       1.00      0.45      0.62       143

   micro avg       0.93      0.93      0.93      1115
   macro avg       0.96      0.72      0.79      1115
weighted avg       0.93      0.93      0.92      1115
 samples avg       0.93      0.93      0.93      1115<jupyter_text>2. RNN<jupyter_code>model = keras.models.Sequential([
    keras.layers.Embedding(input_dim=MAX_NB_WORDS, output_dim=EMB_DIM, input_length=MAX_SEQUENCE_LENGTH),
    keras.layers.SimpleRNN(units=2, input_shape=(None,1)),
    keras.layers.Dense(units=2,activation='softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=5, validation_data=(X_test,y_test))
y_pred = model.predict(X_test)
model.evaluate(X_test,y_test)
print(classification_report(y_test,y_pred.round()))<jupyter_output>precision    recall  f1-score   support

           0       0.98      1.00      0.99       972
           1       1.00      0.88      0.94       143

   micro avg       0.98      0.98      0.98      1115
   macro avg       0.99      0.94      0.96      1115
weighted avg       0.99      0.98      0.98      1115
 samples avg       0.98      0.98      0.98      1115<jupyter_text>3. LSTM<jupyter_code>model = keras.models.Sequential([
    keras.layers.Embedding(input_dim=MAX_NB_WORDS, output_dim=EMB_DIM, input_length=MAX_SEQUENCE_LENGTH),
    keras.layers.LSTM(units=2, activation='relu', return_sequences=True),
    keras.layers.Dropout(0.2),
    keras.layers.BatchNormalization(),
    keras.layers.Flatten(),
    keras.layers.Dense(units=2,activation='softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train,y_train, batch_size=16, epochs=5, validation_data=(X_test,y_test))
y_pred = model.predict(X_test)
print(classification_report(y_test,y_pred.round()))<jupyter_output>precision    recall  f1-score   support

           0       0.97      1.00      0.98       972
           1       0.97      0.80      0.88       143

   micro avg       0.97      0.97      0.97      1115
   macro avg       0.97      0.90      0.93      1115
weighted avg       0.97      0.97      0.97      1115
 samples avg       0.97      0.97      0.97      1115<jupyter_text>Next Word Prediction<jupyter_code>import pandas as pd
dataset = pd.read_csv(r'spam.csv',encoding='latin1')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1060336048879837, 'fraction_numerical': 0.04276985743380855, 'mean_word_length': 4.0044585987261145, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '258124', 'license': 'MIT', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2686, 'n_words': 617, 'path': 'Natural Language Processing Recipes/Chapter 6 Deep Learning for NLP.ipynb', 'repo_name': 'sonwanesuresh95/Books-to-notebooks', 'size': '7862'}