<jupyter_start><jupyter_text>IMPORTING NECESSARY MODULES<jupyter_code>import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt  #For Visualisation
%matplotlib inline
import seaborn as sns  #For Visualisation
from bs4 import BeautifulSoup  # For Text Parsing<jupyter_output><empty_output><jupyter_text>IMPORTING DATASET<jupyter_code>data = pd.read_csv('Reviews.csv')
data<jupyter_output><empty_output><jupyter_text>DATA PREPROCESSING & VISUALISATION<jupyter_code>data.isnull().sum()
data=data.dropna()
data.isnull().sum()
data.shape
score_unique = data['Score'].unique()
print(score_unique)
#   0-> NEGATIVE REVIEW
#   1-> NEUTRAL REVIEW
#   2-> POSTIVE REVIEW
a=[]
for i in data['Score']:
    if i <3:                              
        a.append(0)
    if i==3:
        a.append(1)
    if i>3:
        a.append(2)
r_0, r_1, r_2 = 0, 0, 0
for i in a:
    if i == 0:
        r_0 += 1
    elif i == 1:
        r_1 += 1
    else:
        r_2 += 1

print('Negative Reviews:',r_0)
print('Neutral Reviews:',r_1)
print('Positive Reviews:',r_2)
sns.countplot(a)
plt.xlabel('Reviews', color = 'red')
plt.ylabel('Count', color = 'red')
plt.xticks([0,1,2],['Negative','Neutral','Positive'])
plt.title('COUNT PLOT', color = 'r')
plt.show()
data['sentiment']=a
#data
final_dataset = data[['Text','sentiment']]
#final_dataset
data_p=final_dataset[data['sentiment']==2]
data_n=final_dataset[data['sentiment']==0]

len(data_p), len(data_n)
datap = data_p.iloc[np.random.randint(1,443766,5000), :]
datan = data_n.iloc[np.random.randint(1, 82007,5000), :]

len(datan), len(datap)
data = pd.concat([datap,datan])
len(data)
c=[]
for i in data['sentiment']:
    if i==0:
        c.append(0)
    if i==2:
        c.append(1)
data['sentiment']=c
sns.countplot(data['sentiment'])
plt.show()
def strip_html(text):
    soup = BeautifulSoup(text, "html.parser")
    return soup.get_text()
data['review'] = data['Text'].apply(strip_html)

data=data.drop('Text',axis=1)

data.head()<jupyter_output><empty_output><jupyter_text>MODEL BUILDING<jupyter_code># !pip install nltk
import nltk  #Natural Language Processing Toolkit
def punc_clean(text):
    import string as st
    a=[w for w in text if w not in st.punctuation]
    return ''.join(a)
data['review'] = data['review'].apply(punc_clean)

data.head(2)
def remove_stopword(text):
    stopword=nltk.corpus.stopwords.words('english')
    stopword.remove('not')
    a=[w for w in nltk.word_tokenize(text) if w not in stopword]
    return ' '.join(a)
#data['review'] = data['review'].apply(remove_stopword)
from sklearn.feature_extraction.text import TfidfVectorizer

vectr = TfidfVectorizer(ngram_range=(1,2),min_df=1)
vectr.fit(data['review'])

vect_X = vectr.transform(data['review'])
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()

clf=model.fit(vect_X,data['sentiment'])

clf.score(vect_X,data['sentiment'])*100<jupyter_output><empty_output><jupyter_text>PREDICTION<jupyter_code>clf.predict(vectr.transform(['''I love ice cream''']))
clf.predict(vectr.transform(['''I hate icecream''']))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1603415559772296, 'fraction_numerical': 0.020556609740670462, 'mean_word_length': 5.177734375, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '606925', 'license': 'MIT', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1233, 'n_words': 233, 'path': 'AI and ML/Sentiment-Analysis/sentimental-analysis.ipynb', 'repo_name': 'srini047/24hr-Beginner-Projects', 'size': '3162'}