Kaggle competition: Natural Language Processing with Disaster Tweets
stop=set(stopwords.words('english'))
warnings.filterwarnings("ignore")
 cufflinks.go_offline()
cufflinks.set_config_file(world_readable=True, theme='pearl')
<compute_test_metric>
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 if(len(a)==0)&(len(b)==0): return 0.5
c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
Actual_1 = 'Twitter Sentiment Analysis'
Predict_1 = 'Sentiment Analysis'
Actual_2 = 'Twitter Sentiment Analysis'
Predict_2 = 'Analysis'
print("Jaccard score for first set of scentences: {}".format(jaccard(Actual_1,Predict_1)))
 print("Jaccard score for second set of scentences: {}".format(jaccard(Actual_2,Predict_2)) )<load_from_csv>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
ori_train=pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv')
 ori_test=pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 train = ori_train
test = ori_test
ori_train = ori_train.fillna("")
 ori_test = ori_test.fillna("")
 train.head(10)
 print("There are {} rows and {} columns in train file".format(train.shape[0],train.shape[1]))
print("There are {} rows and {} columns in test file".format(test.shape[0],test.shape[1]))<define_variables>
import tokenization
train = ori_train
test = ori_test<count_values>
def bert_encode(texts, tokenizer, max_len=512):
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 )
def count_values(df,feature):
total=df.loc[:,feature].value_counts(dropna=False)
 percent=round(df.loc[:,feature].value_counts(dropna=False,normalize=True)*100,2)
 return pd.concat([total,percent],axis=1,keys=['Total','Percent'] )<set_options>
def build_model(bert_layer, max_len=512):
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, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 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
def hover(hover_color="
return dict(selector="tr:hover",
props=[("background-color", "%s" % hover_color)] )<feature_engineering>
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )
def find_link(string):
url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +', string)
 return "".join(url)
 train['target_url']=train['selected_text'].apply(lambda x: find_link(x))
df2=pd.DataFrame(train.loc[train['target_url']!=""]['sentiment'].value_counts() ).reset_index()
df2.rename(columns={"index": "sentiment", "sentiment": "url_count"},inplace = True)
 styles = [
hover() ,
dict(selector="th", props=[("font-size", "150%"), 
("text-align", "center")]),
dict(selector="caption", props=[("caption-side", "bottom")])
 ]
html =(df2.style.set_table_styles(styles)
.set_caption("Hover to highlight."))
html<string_transform>
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )
def create_corpus_text(target):
corpus=[]
for x in train[train['sentiment']==target]['text'].str.split() :
for i in x:
corpus.append(i)
 return corpus<categorify>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
corpus=create_corpus_text("positive")
 dic=defaultdict(int)
 for word in corpus:
if word in stop:
dic[word]+=1
top_0=sorted(dic.items() , key=lambda x:x[1],reverse=True)[:20]
corpus=create_corpus_text("negative")
 dic=defaultdict(int)
 for word in corpus:
if word in stop:
dic[word]+=1
top_1=sorted(dic.items() , key=lambda x:x[1],reverse=True)[:20]
corpus=create_corpus_text("neutral")
 dic=defaultdict(int)
 for word in corpus:
if word in stop:
dic[word]+=1
top_2=sorted(dic.items() , key=lambda x:x[1],reverse=True)[:20]<string_transform>
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values
def create_corpus_selected_text(target):
corpus=[]
for x in train[train['sentiment']==target]['selected_text'].str.split() :
for i in x:
corpus.append(i)
 return corpus<import_modules>
callback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=20,
batch_size=8,
callbacks=[callback] 
)
 model.save('model_bert.h5' )
print('TF version',tf.__version__ )<define_variables>
prediction= model.predict(test_input )
MAX_LEN = 96
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 EPOCHS = 3
BATCH_SIZE = 32
PAD_ID = 1
SEED = 88888
LABEL_SMOOTHING = 0.1
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
train = ori_train
train.head()<define_variables>
submission['target'] = prediction.round().astype(int)
 submission.to_csv('submission.csv', index=False )
<categorify><EOS> 
train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=2,
batch_size=8 
)
 model.save('model_bert.h5' )