Kaggle competition: Natural Language Processing with Disaster Tweets
train = pd.read_csv(".. /input/word2vec-nlp-tutorial/labeledTrainData.tsv", header=0, \
delimiter="\t", quoting=3)
 train = train.drop(['id'], axis=1)
 train.head()<load_from_csv>
!pip install /kaggle/input/bert-for-tf2/py-params-0.8.2/py-params-0.8.2/
!pip install /kaggle/input/bert-for-tf2/params-flow-0.7.4/params-flow-0.7.4/
!pip install /kaggle/input/bert-for-tf2/bert-for-tf2-0.13.2/bert-for-tf2-0.13.2/
!pip install sentencepiece
df2 = pd.read_csv('.. /input/imdb-review-dataset/imdb_master.csv',encoding="latin-1")
<drop_column>
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
import keras
from tensorflow.keras.layers import Dense, Input,LeakyReLU, Dropout, Softmax
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
import bert
import matplotlib.pyplot as plt
import re
df2 = df2.drop(['Unnamed: 0','type','file'],axis=1)
 df2.columns = ["review","sentiment"]
df2.head()<categorify>
train_df = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )
df2 = df2[df2.sentiment != 'unsup']
df2['sentiment'] = df2['sentiment'].map({'pos': 1, 'neg': 0})
 df2.head()<concatenate>
train_df = train_df.astype({"id" : int, "target" : int, "text" : str})
 train_df.head(5 )
training = pd.concat([train, df2] ).reset_index(drop=True)
 training.shape 
<categorify>
test_df = test_df.astype({"id" : int, "text" : str})
 test_df.head(5 )
def review_to_words(raw_review):
review_text = BeautifulSoup(raw_review, ).get_text()
letters_only = re.sub("[^a-zA-Z]", " ", review_text)
 words = letters_only.lower().split()
stops = set(stopwords.words("english"))
meaningful_words = [w for w in words if not w in stops]
return(" ".join(meaningful_words))<define_variables>
def remove_emoji(string):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', string)
 def clean_text(text):
text = re.sub(r'https?://\S+', '', text)
 text = re.sub(r'
text = re.sub(r'@\w+', '', text)
 text = re.sub(r'
',' ', text)
 text = re.sub('\s+', ' ', text ).strip()
return text
def find_hashtags(tweet):
return " ".join([match.group(0)[1:] for match in re.finditer(r"
def find_usernames(tweet):
return " ".join([match.group(0)[1:] for match in re.finditer(r"@\w+", tweet)])or 'no'
def find_links(tweet):
return " ".join([match.group(0)[:] for match in re.finditer(r"https?://\S+", tweet)])or 'no'
def preprocess_text(df):
df['clean_text'] = df['text'].apply(lambda x: clean_text(x))
df['hashtags'] = df['text'].apply(lambda x: find_hashtags(x))
df['usernames'] = df['text'].apply(lambda x: find_usernames(x))
df['links'] = df['text'].apply(lambda x: find_links(x))
return df
train_df = preprocess_text(train_df)
 test_df = preprocess_text(test_df)
 print(train_df )
num_reviews = training["review"].size
clean_train_reviews = []
print("Cleaning and parsing the training set movie reviews...
 ")
 clean_train_reviews = []
for i in range(0, num_reviews):
if(( i+1)%10000 == 0):
print("Review %d of %d
" %(i+1, num_reviews))
clean_train_reviews.append(review_to_words(training["review"][i]))
print("Done" )<feature_engineering>
train_df.isnull().sum()
print("Creating the bag of words...
 ")
 vectorizer = CountVectorizer(analyzer = "word", \
tokenizer = None, \
preprocessor = None, \
stop_words = None, \
max_features = 6000)
 train_data_features = vectorizer.fit_transform(clean_train_reviews)
 train_data_features = train_data_features.toarray()
print("Done")
<split>
train_df.fillna(' ')
 test_df.fillna(' ')
 train_df['text_final'] = train_df['clean_text']+' '+ train_df['keyword']
test_df['text_final'] = test_df['clean_text']+' '+ test_df['keyword']
train_df['lowered_text'] = train_df['text_final'].str.lower()
test_df['lowered_text'] = test_df['text_final'].str.lower()
x_train,x_test,y_train,y_test=train_test_split(train_data_features,training["sentiment"],test_size=0.2,random_state=0)
 print("Splitting Done" )<train_model>
def bert_encode(texts, tokenizer, max_len):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )
print("Training the random forest model...")
 forest = RandomForestClassifier(n_estimators = 100)
 forest = forest.fit(x_train, y_train)
 print("Done")
<predict_on_test>
BertTokenizer = bert.bert_tokenization.FullTokenizer
bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_cased_L-12_H-768_A-12/1",trainable=True)
 vocabulary_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
to_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = BertTokenizer(vocabulary_file, to_lower_case)
result = forest.predict(x_test)
 print("Predictions Done" )<compute_test_metric>
train_input = bert_encode(train_df.lowered_text.values.astype(str), tokenizer, max_len=512)
 test_input = bert_encode(test_df.lowered_text.values.astype(str), tokenizer, max_len=512)
 train_labels = train_df.target.values
accuracy=accuracy_score(y_test,result)
 accuracy<compute_test_metric>
def build_model(bert_layer, max_len):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
hidden1 = Dense(128, activation='relu' )(clf_output)
 out = Dense(1, activation='sigmoid' )(hidden1)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=2e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model
cm=confusion_matrix(y_test,result)
 cm<compute_test_metric>
history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=5,
batch_size=16 
)
 predictions1 = model.predict(test_input)
 print(predictions1[0:30] )
f1=f1_score(y_test,result)
 f1<load_from_csv>
embedding = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
 embedded_xtrain = embedding(train_df['clean_text'] ).numpy()
embedded_xtest = embedding(test_df['clean_text'] ).numpy()
target = train_df["target"].to_numpy()
model2 = svm.SVR(kernel='rbf',gamma='auto')
 model2.fit(embedded_xtrain,target)
 predictions2 = model2.predict(embedded_xtest)
 predictions2 = np.mat(predictions2)
 predictions2 = predictions2.T
print(predictions2[0:30])
test = pd.read_csv(".. /input/word2vec-nlp-tutorial/testData.tsv", header=0, delimiter="\t", \
quoting=3)
 print(test.shape)
 num_reviews = len(test["review"])
 clean_test_reviews = []
print("Cleaning and parsing the test set movie reviews...
 ")
 for i in range(0,num_reviews):
if(( i+1)% 5000 == 0):
print("Review %d of %d
" %(i+1, num_reviews))
clean_review = review_to_words(test["review"][i])
 clean_test_reviews.append(clean_review)
 test_data_features = vectorizer.transform(clean_test_reviews)
 test_data_features = test_data_features.toarray()
result = forest.predict(test_data_features)
 output = pd.DataFrame(data={"id":test["id"], "sentiment":result})
 output.to_csv("Bag_of_Words_model.csv", index=False, quoting=3)
 print("Done" )<set_options>
sequence_lenght = 512
warnings.filterwarnings('ignore')
 data_path = '.. /input/'
%matplotlib inline 
<load_from_csv>
USElite2_embedding = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5")
 USElite2_embedded_xtrain = USElite2_embedding(train_df['clean_text'] ).numpy()
USElite2_embedded_xtest = USElite2_embedding(test_df['clean_text'] ).numpy()
USElite2_target = train_df["target"].to_numpy()
USElite2_embedded_xtest.shape
USE_for_m4_xtrain = USElite2_embedded_xtrain
USE_for_m4_xtest = USElite2_embedded_xtest
USE_for_m4_target = USElite2_target
lbl_train = pd.read_csv(data_path+'labeledTrainData.tsv', sep='\t')
 print("Shape: {}".format(lbl_train.shape))
print(lbl_train.columns )<load_from_csv>
USElite2_x_train, USElite2_x_test, USElite2_y_train, USElite2_y_test = train_test_split(
 USElite2_embedded_xtrain,
USElite2_target,
test_size=0.1,
random_state=0,
shuffle=True 
)
 print(USElite2_x_train.shape)
 print(USElite2_y_train.shape)
 print(USElite2_x_test.shape)
 print(USElite2_y_test.shape)
 print(USElite2_x_train )
unlbl_train = pd.read_csv(data_path+'unlabeledTrainData.tsv', sep='\t', error_bad_lines=False)
 print("Shape: {}".format(unlbl_train.shape))
print(unlbl_train.columns )<load_from_csv>
def make_my_model() :
input = keras.layers.Input(shape=(sequence_lenght,1), dtype='float32')
 next_layer = keras.layers.Conv1D(265,kernel_size = 10, activation = "relu",padding="valid",strides = 1 )(input)
 next_layer = keras.layers.MaxPooling1D(pool_size=2 )(next_layer)
 next_layer = keras.layers.Conv1D(64,kernel_size = 5, padding="valid", strides = 1 )(next_layer)
 next_layer = keras.layers.LeakyReLU(alpha=0.1 )(next_layer)
 next_layer = keras.layers.MaxPooling1D(pool_size=3, strides=1 )(next_layer)
 next_layer = keras.layers.Flatten()(next_layer)
 next_layer = keras.layers.Dense(64 )(next_layer)
 next_layer = keras.layers.LeakyReLU(alpha=0.1 )(next_layer)
 output = keras.layers.Dense(1, activation="sigmoid" )(next_layer)
 return keras.Model(inputs=input, outputs=output )
test = pd.read_csv(data_path+'testData.tsv', sep='\t')
 print(f'Shape: {test.shape}')
 print(test.columns )<load_from_csv>
USElite2_x_train = np.reshape(USElite2_x_train,(USElite2_x_train.shape[0], USElite2_x_train.shape[1],1))
USElite2_y_train = np.reshape(USElite2_y_train,(USElite2_y_train.shape[0],1))
USElite2_x_test = np.reshape(USElite2_x_test,(USElite2_x_test.shape[0], USElite2_x_test.shape[1],1))
USElite2_y_test = np.reshape(USElite2_y_test,(USElite2_y_test.shape[0],1))
model3 = make_my_model()
model3.compile("adam", loss = "binary_crossentropy", metrics = ["acc"])
 model3.summary()
model3.fit(
 USElite2_x_train,
USElite2_y_train,
batch_size = 128,
epochs = 15,
validation_data =(USElite2_x_test,USElite2_y_test)
)
 USElite2_embedded_xtest = np.reshape(USElite2_embedded_xtest,(USElite2_embedded_xtest.shape[0],USElite2_embedded_xtest.shape[1],1))
predictions3 = model3.predict(USElite2_embedded_xtest)
 predictions3
print(predictions3[0:30] )
samplesub = pd.read_csv(data_path+'./sampleSubmission.csv')
 samplesub.head(3 )<find_best_params>
model4 = svm.SVR(kernel='rbf', gamma='auto')
 model4.fit(USE_for_m4_xtrain ,USE_for_m4_target)
 predictions4 = model4.predict(USE_for_m4_xtest)
 predictions4 = np.mat(predictions4)
 predictions4 = predictions4.T
print(predictions4[0:30] )
print(f'pos:
{lbl_train.review[np.random.randint(0, 25000)]}')
 print(f'neg:
{test.review[np.random.randint(0, 25000)]}' )<string_transform>
((0.5*predictions1+0.5*predictions2+0.1*predictions3+0.3*predictions4)*0.8 ).round().astype(int )
<split><EOS> 
submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 print(submission)
 submission['target'] =(( 0.5*predictions1+0.5*predictions2+0.1*predictions3+0.3*predictions4)*0.8 ).round().astype(int)
 print(submission)
 submission.to_csv('submission.csv', index=False )