# Importing the libraries and the dataset
import pandas as pd
dataset = pd.read_csv('Restaurant_Reviews.tsv', delimiter = '\t', quoting = 3)
# delimiter= '\t' is used to read it like a tsv file, quoting=3 is used to ignore the double quotes in the dataset

# Cleaning the texts
import re    # re library is used for text cleaning
import nltk
# 'stopwords' contains all the unnecessary words that need to be removed
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
corpus = []
for i in range(0, 1000):
    # everything other than the alphabets are removed and replaced with a space
    review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][i])
    #all the reviews are converted to lower case only
    review = review.lower()    
    # the string is split into a list of words
    review = review.split()
    # PorterStemmer is used to make a list of only the stem words
    ps = PorterStemmer()
    review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
    # the list of words is converted to a string using join method
    review = ' '.join(review)
    # the string is added to the list from which we make the bag of words
    corpus.append(review)

# Creating the Bag of Words model
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features = 1500)
x = cv.fit_transform(corpus).toarray()
y = dataset.iloc[:, 1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.20, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

# making the ann, importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 1500))
# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))  
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(x_train, y_train, batch_size = 10, epochs = 100)

# Predicting the Test set results
y_pred = classifier.predict(x_test)
y_pred = (y_pred > 0.5)

#making a confusion matrix 
from sklearn.metrics import confusion_matrix
confusionMatrix = confusion_matrix(y_test, y_pred)

# printing the final accuracy of the model
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred))

# Importing the libraries and the dataset
import pandas as pd
dataset = pd.read_csv('Restaurant_Reviews.tsv', delimiter = '\t', quoting = 3)
# delimiter= '\t' is used to read it like a tsv file, quoting=3 is used to ignore the double quotes in the dataset

# Cleaning the texts
import re    # re library is used for text cleaning
import nltk
# 'stopwords' contains all the unnecessary words that need to be removed
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
corpus = []
for i in range(0, 1000):
    # everything other than the alphabets are removed and replaced with a space
    review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][i])
    #all the reviews are converted to lower case only
    review = review.lower()    
    # the string is split into a list of words
    review = review.split()
    # PorterStemmer is used to make a list of only the stem words
    ps = PorterStemmer()
    review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
    # the list of words is converted to a string using join method
    review = ' '.join(review)
    # the string is added to the list from which we make the bag of words
    corpus.append(review)

# Creating the Bag of Words model
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features = 1500)
x = cv.fit_transform(corpus).toarray()
y = dataset.iloc[:, 1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.20, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

# making the ann, importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 1500))
# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))  
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(x_train, y_train, batch_size = 10, epochs = 100)

# Predicting the Test set results
y_pred = classifier.predict(x_test)
y_pred = (y_pred > 0.5)

#making a confusion matrix 
from sklearn.metrics import confusion_matrix
confusionMatrix = confusion_matrix(y_test, y_pred)

# printing the final accuracy of the model
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred))

# Importing the libraries and the dataset # delimiter= '\t' is used to read it like a tsv file, quoting=3 is used to ignore the double quotes in the dataset # Cleaning the texts # re library is used for text cleaning # 'stopwords' contains all the unnecessary words that need to be removed # everything other than the alphabets are removed and replaced with a space #all the reviews are converted to lower case only # the string is split into a list of words # PorterStemmer is used to make a list of only the stem words # the list of words is converted to a string using join method # the string is added to the list from which we make the bag of words # Creating the Bag of Words model # Splitting the dataset into the Training set and Test set # Feature Scaling # making the ann, importing the Keras libraries and packages # Initialising the ANN # Adding the input layer and the first hidden layer # Adding the second hidden layer # Adding the output layer # Compiling the ANN # Fitting the ANN to the Training set # Predicting the Test set results #making a confusion matrix # printing the final accuracy of the model