<jupyter_start><jupyter_text>Unit 3  Chapter 3  ---  Naive Bayes Classifiers ---Naive Bayes Classifiers are **Bayesian** classifiers that make an assumption that the features are **naive** i.e. the features(here words) are independent and have no relation between each other. For example, consider the sentence,'He is my friend. His name is John.'A naive classifier sees no relation between, **He**, **His**, & **John** (even though there is). ---  Bag of words Before we work on any algorithm, we need to convert our text data to numbers.To convert text into numbers, we need a way to represent each word in our data.In the **bag of words** model, we use frequency as a way of representing text as numbers.There are new & better ways to convert our text data into numbers, like **word2vec**.We will first take a look at the ```bag of words``` model. --- A bag of words model, as the name suggests, stores words into an unordered **set**.We use **set** because duplicate words are not allowed, as we are storing their frequency as well.  Bag of words in Python> [Code Courtesy](https://www.geeksforgeeks.org/bag-of-words-bow-model-in-nlp/) - Let's take a sample paragraph > Beans. I was trying to explain to somebody as we were flying in, that’s corn. That’s beans. And they were very impressed at my agricultural knowledge. Please give it up for Amaury once again for that outstanding introduction. I have a bunch of good friends here today, including somebody who I served with, who is one of the finest senators in the country, and we’re lucky to have him, your Senator, <NAME> is here. I also noticed, by the way, former Governor Edgar here, who I haven’t seen in a long time, and somehow he has not aged and I have. And it’s great to see you, Governor. I want to thank President Killeen and everybody at the U of I System for making it possible for me to be here today. And I am deeply honored at the Paul Douglas Award that is being given to me. He is somebody who set the path for so much outstanding public service here in Illinois. Now, I want to start by addressing the elephant in the room. I know people are still wondering why I didn’t speak at the commencement.<jupyter_code>dataset = 'Beans. I was trying to explain to somebody as we were flying in, that’s corn. That’s beans. And they were very impressed at my agricultural knowledge. Please give it up for Amaury once again for that outstanding introduction. I have a bunch of good friends here today, including somebody who I served with, who is one of the finest senators in the country, and we’re lucky to have him, your Senator, <NAME> is here. I also noticed, by the way, former Governor Edgar here, who I haven’t seen in a long time, and somehow he has not aged and I have. And it’s great to see you, Governor. I want to thank President Killeen and everybody at the U of I System for making it possible for me to be here today. And I am deeply honored at the Paul Douglas Award that is being given to me. He is somebody who set the path for so much outstanding public service here in Illinois. Now, I want to start by addressing the elephant in the room. I know people are still wondering why I didn’t speak at the commencement.'
dataset<jupyter_output><empty_output><jupyter_text>Basic preprocessing > We will first break the sentences, then convert text to lower case, and remove punctuation and non words<jupyter_code>from nltk.tokenize import sent_tokenize
import re 
sentences = sent_tokenize(dataset)
sentences
sentences[4]
# Now the dataset has been broken into sentences.<jupyter_output><empty_output><jupyter_text>> Converting to lower case.But why is it neccessary to convert to lower case?- If we don't convert to lower case then, any two words like ```Going``` and ```going``` will be treated differently, resulting in a large feature space.<jupyter_code>lower_cased_data = [sentence.lower() for sentence in sentences]
lower_cased_data[4]<jupyter_output><empty_output><jupyter_text>> Removing non-word characters<jupyter_code>cleaned_data = [re.sub(r'\W+',' ',sentence) for sentence in lower_cased_data]
cleaned_data[4]<jupyter_output><empty_output><jupyter_text>> Removing punctuations if any<jupyter_code>cleaned_data = [re.sub(r'\s+',' ',sentence) for sentence in cleaned_data]
cleaned_data[4]<jupyter_output><empty_output><jupyter_text>> Creating bag of words<jupyter_code>word2count = {} 
for data in cleaned_data: 
    words = nltk.word_tokenize(data) 
    for word in words: 
        if word not in word2count.keys(): 
            word2count[word] = 1
        else: 
            word2count[word] += 1<jupyter_output><empty_output><jupyter_text>Now we have a bag of words that has stored frequency with it<jupyter_code>word2count<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06968421052631579, 'fraction_numerical': 0.0029473684210526317, 'mean_word_length': 4.602594339622642, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '357091', 'license': 'Apache-2.0', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1272, 'n_words': 692, 'path': 'Unit 3 - Naives Bayes and Sentiment Classification/.ipynb_checkpoints/Chapter 3 | Bag of Words-checkpoint.ipynb', 'repo_name': 'samacker77/NLP-Starter-Bundle', 'size': '4760'}