<jupyter_start><jupyter_text>**Importing the packages**<jupyter_code>from transformers import AutoTokenizer as at
from transformers import AutoModelForSequenceClassification  as amsc
import torch
import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>**Initiating the pre-trained [BERT] model**<jupyter_code>tokenizer = at.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')   #Loading the pre-trained NLP model 
model = amsc.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')<jupyter_output><empty_output><jupyter_text>**Analysing , encoding and calculating sentiment. [PIPELINE]**<jupyter_code>token = tokenizer.encode("I love your service  ",return_tensors='pt') #An example review is passed "I love your service".
                                                                      #return_tensor='pt' will return tensors instead of list of python integers
result = model(token)  #passing our tokens to our model 
result.logits   #This is a vector of raw predictions that a classification model generates.
ratings = int(torch.argmax(result.logits)+1)   #using pytorch to normalize the vector of raw predictions
print(ratings)<jupyter_output><empty_output><jupyter_text>**Collecting online reviews from websites [WEB SCRAPING] using beautiful soup or you can use freely available datasets**<jupyter_code>r = requests.get('https://www.yelp.com/biz/social-brew-cafe-pyrmont')   #I am doing webscraping 
soup = BeautifulSoup(r.text , 'html.parser')
regex = re.compile('.*comment.*')
results = soup.find_all('p',{'class':regex})
results     #Awesome we got all the reviews but they have some unwanted things with it .we need to clean it .
reviews = [result.text for result in results]
reviews   #clear view of data(reviews)<jupyter_output><empty_output><jupyter_text>**Loading reviews into the data frame**<jupyter_code>data_frame = pd.DataFrame(np.array(reviews),columns =['Reviews'])    #adding the cleansed data into a dataframe
data_frame            #see now ! . This looks amazing and structured .
data_frame['Reviews'].iloc[0] #Just viewing<jupyter_output><empty_output><jupyter_text>**Creating a function using the pipeline**<jupyter_code>def sentiment_analyzer(Reviews):
  token = tokenizer.encode(Reviews,return_tensors='pt')             #A function to do the work of our pipeline
  result = model(token)            
  return int(torch.argmax(result.logits)+1)<jupyter_output><empty_output><jupyter_text>**Calling the function  and computing the sentiment score of the customer**<jupyter_code>sentiment_analyzer(data_frame['Reviews'].iloc[0])  #An illustration for single review.<jupyter_output><empty_output><jupyter_text>**Looping through all the reviews and adding the sentiment scores to the data frame**<jupyter_code>data_frame['Sentiment_Score'] = data_frame['Reviews'].apply(lambda x: sentiment_analyzer(x[:1000])) 
data_frame #final<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1000660501981506, 'fraction_numerical': 0.00297225891677675, 'mean_word_length': 5.62800875273523, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '439590', 'license': 'MIT', 'n_tokens_mistral': 931, 'n_tokens_neox': 912, 'n_words': 248, 'path': 'Sentiment_Analysis_Using_BERT __.ipynb', 'repo_name': 'Gladrin22/Sentiment_Analysis_BERT', 'size': '3028'}