# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import numpy as np
import pandas as pd
from os import path
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
from textblob import TextBlob
from sklearn import model_selection, preprocessing, linear_model, naive_bayes, metrics
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn import decomposition, ensemble

import pandas, xgboost, numpy, textblob, string
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix, accuracy_score, mean_squared_error, r2_score, roc_auc_score, roc_curve, classification_report
from sklearn.model_selection import train_test_split

from warnings import filterwarnings
filterwarnings('ignore')

train  =pd.read_csv("../input/nlp-getting-started/train.csv")
train.head()
train.isnull().sum()
#Dropping unncessary columns
train.drop(["keyword","location"],axis = 1,inplace = True)

"""
# Cleaning  & Preprocessing
"""
train["text"] = train["text"].apply(lambda x : " ".join(x.lower() for x in x.split()))
train.head()
"""
## Dropping Numbers
"""
train["text"] = train["text"].str.replace("\d","")
train.head()
"""
## Dropping punctuation marks
"""
train["text"] = train["text"].str.replace("[^\w\s]","")
train.head()
import nltk 
nltk.download("stopwords")
"""
## Stopwords

**Stopwords** : Words that are filtered out by Web search engines and other enterprise searching and indexing platforms. Stop words are natural language words which have very little meaning, such as "and", "the", "a", "an", and similar words.
"""
from nltk.corpus import stopwords
sw = stopwords.words("english")
sw.append("u")
sw.append("im")
train["text"] = train["text"].apply(lambda x: " ".join(x for x in x.split() if x not in sw))
"""
## Lemmatization

**Lemmatization :** In linguistics, it is the process of grouping together the different inflected forms of a word so they can be analyzed as a single item. Putting an example to the definition, “computers” is an inflected form of “computer”, the same logic as “dogs” being an inflected form of “dog”.
"""
from textblob import Word
nltk.download("wordnet")
train["text"] = train["text"].apply(lambda x: " ".join([Word(word).lemmatize() for word in x.split()]))
train.head()
#regex
train["text"] = train["text"].str.replace('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',"")
train["text"] = train["text"].str.replace(r"We're", "We are")
train["text"] = train["text"].str.replace(r"That's", "That is")
train["text"] = train["text"].str.replace(r"won't", "will not")
train["text"] = train["text"].str.replace(r"they're", "they are")
train["text"] = train["text"].str.replace(r"Can't", "Cannot")
train["text"] = train["text"].str.replace(r"wasn't", "was not")
train["text"] = train["text"].str.replace(r"don\x89Ûªt", "do not")
train["text"] = train["text"].str.replace(r"aren't", "are not")
train["text"] = train["text"].str.replace(r"isn't", "is not")
train["text"] = train["text"].str.replace(r"You're", "You are")
train["text"] = train["text"].str.replace(r"I'M", "I am")
train["text"] = train["text"].str.replace(r"shouldn't", "should not")
train["text"] = train["text"].str.replace(r"wouldn't", "would not")
train["text"] = train["text"].str.replace(r"i'm", "I am")
train["text"] = train["text"].str.replace(r"We've", "We have")
train["text"] = train["text"].str.replace(r"Didn't", "Did not")
train["text"] = train["text"].str.replace(r"it's", "it is")
train["text"] = train["text"].str.replace(r"can't", "cannot")
train["text"] = train["text"].str.replace(r"don't", "do not")
train["text"] = train["text"].str.replace(r"you're", "you are")
train["text"] = train["text"].str.replace(r"I've", "I have")
train["text"] = train["text"].str.replace(r"Don't", "do not")
train["text"] = train["text"].str.replace(r"I'll", "I will")
train["text"] = train["text"].str.replace(r"Let's", "Let us")
train["text"] = train["text"].str.replace(r"Could've", "Could have")
train["text"] = train["text"].str.replace(r"youve", "you have")
train["text"] = train["text"].str.replace(r"It's", "It is")
from sklearn.model_selection import train_test_split
"""
## Visualisation
"""
freq = (train["text"][0:1000]).apply(lambda x: pd.value_counts(x.split(" "))).sum(axis = 0).reset_index()
freq.columns = ["words","tf"]
x = freq[freq["tf"] > 30].sort_values(by = "tf" ,ascending = False)
x.plot.bar(x = "words", y = "tf",color = "pink");
#WordCloud for the first 5 row
for i in range (0,5):
    text = train["text"][i]
    wordcloud = WordCloud().generate(text)
    plt.imshow(wordcloud, interpolation = "bilinear")
    plt.axis("off")
    plt.show()
    print("***********************************************")
from nltk.tokenize import sent_tokenize, word_tokenize 
import warnings 
  
warnings.filterwarnings(action = 'ignore') 
  
import gensim 
from gensim.models import Word2Vec 
import numpy as np
import pandas as pd
from os import path
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
x_train, x_test, y_train, y_test = train_test_split(train["text"], train["target"],
                                                    test_size = 0.3,
                                                    random_state = 18)
"""
# Prediction
"""
"""
## Count Vector


![count%20vector.png](attachment:count%20vector.png)

By word frequency we indicate the number of times each token occurs in a text. When talking about word frequency, we distinguish between types and tokens. Types are the distinct words in a corpus, whereas tokens are the words, including repeats. Let's see how this works in practice.
"""
vectorizer = CountVectorizer()
vectorizer.fit(x_train)
x_train_count = vectorizer.transform(x_train)
x_test_count = vectorizer.transform(x_test)
"""
## Logistic Regression
"""
loj = linear_model.LogisticRegression()
loj_model = loj.fit(x_train_count, y_train)
y_pred = loj_model.predict(x_test_count)


accuracy_score(y_test,y_pred)
"""
## Naive Bayes
"""
x_train_count
nb = naive_bayes.MultinomialNB()
nb_model = nb.fit(x_train_count,y_train)
y_pred = nb_model.predict(x_test_count)
accuracy_score(y_test,y_pred)
"""
## RF
"""
rf = ensemble.RandomForestClassifier()
rf_model = rf.fit(x_train_count,y_train)
y_pred = rf_model.predict(x_test_count)
accuracy_score(y_test,y_pred)
"""
## XGBOOST
"""
xgb = xgboost.XGBClassifier()
xgb_model = xgb.fit(x_train_count,y_train)
y_pred = xgb_model.predict(x_test_count)
accuracy_score(y_test,y_pred)
"""
## TF-IDF Word Level

![tf%20idf.png](attachment:tf%20idf.png)



**Term-frequency-inverse document frequency (TF-IDF)** is another way to judge the topic of an article by the words it contains. With TF-IDF, words are given weight – TF-IDF measures relevance, not frequency. That is, wordcounts are replaced with TF-IDF scores across the whole dataset.



"""
tf_idf_word_vectorizer = TfidfVectorizer()

tf_idf_word_vectorizer.fit(x_train)
x_train_tf_idf_word = tf_idf_word_vectorizer.transform(x_train)
x_test_tf_idf_word = tf_idf_word_vectorizer.transform(x_test)
"""
## Logistic Regression
"""
loj = linear_model.LogisticRegression()
loj_model = loj.fit(x_train_tf_idf_word, y_train)
y_pred = loj_model.predict(x_test_tf_idf_word)
accuracy_score(y_test,y_pred)
"""
## Naive Bayes
"""
nb = naive_bayes.MultinomialNB()
nb_model = nb.fit(x_train_tf_idf_word,y_train)
y_pred = nb.predict(x_test_tf_idf_word)
accuracy_score(y_test,y_pred)
"""
## For the test

"""
test = pd.read_csv("../input/nlp-getting-started/test.csv")
test.drop(["keyword","location"],axis = 1,inplace = True)
def prep(test):
    
    
    test["text"] = test["text"].apply(lambda x : " ".join(x.lower() for x in x.split()))
    
    test["text"] = test["text"].str.replace("\d","")
    
    test["text"] = test["text"].str.replace("[^\w\s]","")
    
    test["text"] = test["text"].apply(lambda x: " ".join(x for x in x.split() if x not in sw))
    
    test["text"] = test["text"].apply(lambda x: " ".join([Word(word).lemmatize() for word in x.split()]))
    
    test["text"] = test["text"].str.replace('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',"")
    
    test["text"] = test["text"].str.replace(r'(((http)(s)?|www(.)?)(://)?\S+)',"")
    
    test["text"] = test["text"].str.replace(r"\x89ÛÓ", "")
    test["text"] = test["text"].str.replace(r"he's", "he is")
    test["text"] = test["text"].str.replace(r"there's", "there is")
    test["text"] = test["text"].str.replace(r"We're", "We are")
    test["text"] = test["text"].str.replace(r"That's", "That is")
    test["text"] = test["text"].str.replace(r"won't", "will not")
    test["text"] = test["text"].str.replace(r"they're", "they are")
    test["text"] = test["text"].str.replace(r"Can't", "Cannot")
    test["text"] = test["text"].str.replace(r"wasn't", "was not")
    test["text"] = test["text"].str.replace(r"don\x89Ûªt", "do not")
    test["text"] = test["text"].str.replace(r"aren't", "are not")
    test["text"] = test["text"].str.replace(r"isn't", "is not")
    test["text"] = test["text"].str.replace(r"You're", "You are")
    test["text"] = test["text"].str.replace(r"I'M", "I am")
    test["text"] = test["text"].str.replace(r"shouldn't", "should not")
    test["text"] = test["text"].str.replace(r"wouldn't", "would not")
    test["text"] = test["text"].str.replace(r"i'm", "I am")
    test["text"] = test["text"].str.replace(r"We've", "We have")
    test["text"] = test["text"].str.replace(r"Didn't", "Did not")
    test["text"] = test["text"].str.replace(r"it's", "it is")
    test["text"] = test["text"].str.replace(r"can't", "cannot")
    test["text"] = test["text"].str.replace(r"don't", "do not")
    test["text"] = test["text"].str.replace(r"you're", "you are")
    test["text"] = test["text"].str.replace(r"I've", "I have")
    test["text"] = test["text"].str.replace(r"Don't", "do not")
    test["text"] = test["text"].str.replace(r"I'll", "I will")
    test["text"] = test["text"].str.replace(r"Let's", "Let us")
    test["text"] = test["text"].str.replace(r"Could've", "Could have")
    test["text"] = test["text"].str.replace(r"youve", "you have")
    test["text"] = test["text"].str.replace(r"It's", "It is")
    

    
    return test
  


df = prep(test)

test_x = df["text"]
vectorizer = CountVectorizer()
vectorizer.fit(test_x)
vectorizer.transform(test_x)
 vectorizer.transform(test_x)
x_test_co= vectorizer.transform(test_x)
x_train_count = vectorizer.transform(x_train)
nb = naive_bayes.MultinomialNB()
nb_model = nb.fit(x_train_count,y_train)
y_pred = nb_model.predict(x_test_co)
y_pred
dictt = {}
dictt['id'] = test.id
dictt['target'] = y_pred
submission = pd.DataFrame(dictt)
submission
#submission.to_csv("submission3.csv" , index = None)
"""
# Credits


https://www.webopedia.com/TERM/S/stop_words.html

https://www.twinword.com/blog/what-is-lemmatization/

https://port.sas.ac.uk/mod/book/view.php?id=583&chapterid=381#:~:text=2.3%20Word%20count,-After%20tokenising%20a&text=By%20word%20frequency%20we%20indicate,how%20this%20works%20in%20practice.

https://wiki.pathmind.com/bagofwords-tf-idf

https://medium.com/deep-math-machine-learning-ai/chapter-9-1-nlp-word-vectors-d51bff9628c1
"""