-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
163 lines (115 loc) · 6.46 KB
/
Copy pathpython.py
File metadata and controls
163 lines (115 loc) · 6.46 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pandas as pd
from model.svm_model import SVMModel
from model.naive_bayes_model import NaiveBayesModel
from sklearn.feature_extraction.text import TfidfVectorizer
from model.svm_model import SVMModel
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB
from time import time
from sklearn.feature_selection import SelectPercentile, f_classif
import numpy as np
import mysql.connector
from mysql.connector import Error
import os
import pickle
import common.common as cm
import re
#from bs4 import BeautifulSoup
from nltk.corpus import stopwords
import csv
class TextClassificationPredict(object):
def __init__(self):
self.test = None
def connectMysql():
try:
mySQLconnection = mysql.connector.connect(host='13.251.123.143',port='3306',database='HotelGatewayNew',user='hotelgateway',password='SeechoitlOfwoRwObEaphOabdLVy')
sql_select_Query = "SELECT room_name,master_room_type FROM HotelGatewayNew.room_type_mapping where master_room_type is not null"
cursor = mySQLconnection .cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
cursor.close()
df = pd.read_sql(sql_select_Query,mySQLconnection)
return df
except Error as e :
print ("Error while connecting to MySQL", e)
finally:
#closing database connection.
if(mySQLconnection .is_connected()):
mySQLconnection.close()
print("MySQL connection is closed")
def save_model(filename, clf):
with open(filename, 'wb') as f:
pickle.dump(clf, f)
def readCSV(filename):
df = pd.read_csv(filename)
return df
def clean_text(text):
REPLACE_BY_SPACE_RE = re.compile('[/(){}\[\]\|@,;-]')
BAD_SYMBOLS_RE = re.compile('[^0-9a-z #+_]')
STOPWORDS = set(stopwords.words('english'))
"""
text: a string
return: modified initial string
"""
text = text.lower() # lowercase text
text = REPLACE_BY_SPACE_RE.sub(' ', text) # replace REPLACE_BY_SPACE_RE symbols by space in text
text = BAD_SYMBOLS_RE.sub('', text) # delete symbols which are in BAD_SYMBOLS_RE from text
text = ' '.join(word for word in text.split() if word not in STOPWORDS) # delete stopwors from text
return text
def get_train_data(self):
common = cm.Common()
# train data
url = "people.csv"
#train_data = TextClassificationPredict.connectMysql()
train_data = TextClassificationPredict.readCSV(url)
checkdata = TextClassificationPredict.readCSV("peoplemaster.csv")
print(checkdata)
df_train = pd.DataFrame(train_data)
chectrain = pd.DataFrame(checkdata)
df_train['category_id'] = df_train['master_room_type'].factorize()[0]
train_outcome = pd.crosstab(index=train_data["master_room_type"], # Make a crosstab
columns="count") # Name the count column
df_train['room_name'] = df_train["room_name"].apply(TextClassificationPredict.clean_text)
chectrain['room_name'] = chectrain["room_name"].apply(TextClassificationPredict.clean_text)
dfview = df_train.drop(df_train[ df_train['view'] == "Other" ].index )
dfBedType = df_train.drop(df_train[ df_train['bedType'] == "Other" ].index )
dfBed = df_train.drop(df_train[ df_train['bed'] == "Other" ].index )
target = train_data['master_room_type']
#target = checkdata['master_room_type']
targetview = dfview['view']
targetBedType = dfBedType['bedType']
targetBed = dfBed['bed']
traindata, testdata,labels_train, labels_test = train_test_split(df_train,target, test_size = 0.2, random_state = 10)
traindataview, testdataview,labels_trainview, labels_testview = train_test_split(dfview,targetview, test_size = 0.2, random_state = 10)
traindataBedType, testdataBedType,labels_trainBedType, labels_testBedType = train_test_split(dfBedType,targetBedType, test_size = 0.2, random_state = 10)
traindataBed, testdataBed,labels_trainBed, labels_testBed = train_test_split(dfBed,targetBed, test_size = 0.2, random_state = 10)
#model = NaiveBayesModel()
model = SVMModel()
modelview = SVMModel()
modelBedType = SVMModel()
modelBed = SVMModel()
clf = model.clf.fit(traindata["room_name"], traindata.master_room_type)
clfview = modelview.clf.fit(traindataview["room_name"], traindataview.view)
clfBedType = modelBedType.clf.fit(traindataBedType["room_name"], traindataBedType.bedType)
clfBed = modelBed.clf.fit(traindataBed["room_name"], traindataBed.bed)
predicted = clf.predict(testdata['room_name'].apply(TextClassificationPredict.clean_text))
predictedview = clfview.predict(testdataview['room_name'].apply(TextClassificationPredict.clean_text))
predictedBedType = clfBedType.predict(testdataBedType['room_name'].apply(TextClassificationPredict.clean_text))
predictedBed = clfBed.predict(testdataBed['room_name'].apply(TextClassificationPredict.clean_text))
#print (predicted)
print('accuracy %s' % accuracy_score(predicted, labels_test))
print('accuracyView %s' % accuracy_score(predictedview, labels_testview))
print('accuracyBedType %s' % accuracy_score(predictedBedType, labels_testBedType))
print('accuracyBed %s' % accuracy_score(predictedBed, labels_testBed))
a = clf.predict_proba(testdata["room_name"])
TextClassificationPredict.save_model(os.path.abspath(os.path.dirname(__file__)) + "/x_transformer.pkl", clf)
TextClassificationPredict.save_model(os.path.abspath(os.path.dirname(__file__)) + "/x_transformerView.pkl", clfview)
TextClassificationPredict.save_model(os.path.abspath(os.path.dirname(__file__)) + "/x_transformerBedType.pkl", clfBedType)
TextClassificationPredict.save_model(os.path.abspath(os.path.dirname(__file__)) + "/x_transformerViewBed.pkl", clfBed)
dt = pd.DataFrame(testdata)
dt["predicted"] = predicted
#dt.to_csv("test.csv")
if __name__ == '__main__':
tcp = TextClassificationPredict()
tcp.get_train_data()