-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.py
78 lines (61 loc) · 2.11 KB
/
temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
# Part 1 - Data Preprocessing
# Importing the libraries
import pandas as pd
import json
import csv
import re
import nltk
input_data = pd.read_csv("/home/shweta/Desktop/project/Electronics_5.csv")
#dataset = {"reviewText": input_data["reviewText"] }
#dataset1 = pd.DataFrame(input_data,columns=['reviewText'], ['overall'] )
dataset1 = {"reviewText": input_data["reviewText"], "overall": input_data["overall"] }
dataset = pd.DataFrame(data = dataset1)
dataset=dataset.fillna("Product was okay and it works")
print(dataset.isnull().sum())
"""data['Sentiment']"""
dataset = dataset[dataset["overall"] != '3']
dataset["Sentiment"] = dataset["overall"].apply(lambda rating : 1 if rating > '3' else 0)
y=dataset["Sentiment"].values
print("works")
# Cleaning the texts
import re
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
corpus = []
#for i in range(1, 1689188):
#for index, row in dataset.iterrows():
for i in range(1,27000):
#review = re.su1b("[^a-zA-Z]", " ", row['reviewText'])
review = re.sub("[^a-zA-Z]", " ", dataset['reviewText'][i])
review = review.lower()
review = review.split()
ps = PorterStemmer()
review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
review = ' '.join(review)
corpus.append(review)
# Creating the Bag of Words model
"""from sklearn.feature_extraction.text import TfidfVectorizer
vector = TfidfVectorizer(max_features = 1500)
X = vector.fit_transform(corpus).toarray()
Y = dataset.iloc[:, 1].values
"""
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import tenserflow as tf
tk = Tokenizer(lower = True)
tk.fit_on_texts(corpus)
X_seq = tk.texts_to_sequences(corpus)
X_pad = pad_sequences(X_seq, maxlen=100, padding='post')
print("hello")
print("Done")
print("Done")
#split into train and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_pad, y, test_size = 0.25, random_state = 1)