-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPickle_Building_TOPS_RPA_Input.py
222 lines (178 loc) · 9.44 KB
/
Pickle_Building_TOPS_RPA_Input.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 11 17:40:54 2018
@author: velmurugan.m
"""
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 08 17:25:02 2018
@author: velmurugan.m
"""
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 25 11:20:12 2018
@author: velmurugan.m
"""
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
import nltk
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize
from nltk import NaiveBayesClassifier
from nltk.corpus import stopwords
import re
import pickle
from IPython.core.display import display, HTML
#DATA_LOCATION = 'D:\Winstream_data\TOPS\second\FAS - Order Correction response details_Updated_last.xlsx'
DATA_LOCATION = r'C:\Users\velmurugan.m\Desktop\FAS - Order Correction response details_Updated.xlsx'
########################## Form Multi-Label for predicting Next-Action ########
def formDataMultiLabel(sampData, df):
####### Convert Response Category into Labelized Binarizer ################
inpRPAData = (df.inp_Data).astype(str)
inpRPAData = inpRPAData.apply(lambda x: x.split()[0])
lab, lev = pd.factorize(inpRPAData)
print (lev)
return lab
########################## Read Input Data & Format the same ###############
def readFormatData(DATA_LOCATION):
### Read Data from Excel Sheet ###
orgDataFrame = pd.read_excel(DATA_LOCATION, sheet_name='Analysed data').iloc[:1997]
### Extract Data Frame for Response & Response Action ###
respData = orgDataFrame[orgDataFrame.columns[5]]
respActData = orgDataFrame['Manual Action taken']
respSecData = orgDataFrame['2nd Step']
inpData = orgDataFrame["Input Data"]
### Form a new Data Frame with the required fields ###
orgDict = list(zip(respData, respActData, respSecData, inpData))
orgDataFrame = pd.DataFrame(orgDict, columns = ['Resp_Data', 'Resp_Action_Data', 'Secnd_Action_Data', 'inp_Data'])
### Append Label Num IDs for the Target Class Labels ###
orgDataFrame['sec_label_num'] = orgDataFrame.Secnd_Action_Data.map({
'No Action required':8,
'Create the order next service in same location':9,
'Process as Install order':10,
'Process the order with the input address':11,
'Process as TOA':12,
'Check for Billing & Active location steps':13,
'Disconnect the O order & Active the customer':14,
'Order already completed & Check the order':15,
'Process as Move order':16,
'Process the order with the input Unit/LOT #':17,
'Order already cancelled & Check the order':18,
'Process the order with New TN #':19,
'Check Billing & Disconnect the location':20,
'Process the O order first & Process other orders':21,
'Process the C order first & Process other orders':22,
'Check if loc is Active in MIROR, Hold the order':23,
'Check for OC provided':24,
'Process ORCAN Process':25,
'Process Re-establishment process':26
})
orgDataFrame['label_num'] = orgDataFrame.Resp_Action_Data.map({
'Check & work the order':1,
'Check the Billing/Dir/Order for active TN #':2,
'Create the Record in 2nd Drop':3,
'Disconnect & Create Record in the location':4,
'Hold the order until response':5,
'Proceed with Change Process ':6,
'Cancel the order':7
})
'''
lookup_dict = {
'svc' : 'service',
'svr' : 'service',
'placed': 'place',
'tn=' : 'tn',
'called': 'call',
'cus' : 'customer',
'cust' : 'customer',
'actv' : 'active',
'2nd' : 'second',
'addtl' : 'additional',
'addl' : 'additional',
'cncl' : 'cancel',
'cancelled': 'cancel',
'autod' : 'autodialer',
'ialer' : 'dialer',
'auto-dialer': 'autodialer',
'rdy' : 'ready',
'loc' : 'location',
'dup' : 'duplicate'
}
'''
lookup_dict = {
'svc' : 'service',
'svr' : 'service',
'cus' : 'customer',
'cust' : 'customer',
'actv' : 'active',
'2nd' : 'second',
'addtl' : 'additional',
'autod' : 'autodialer',
'ialer' : 'dialer',
'auto-dialer': 'autodialer',
'rdy' : 'ready',
'loc' : 'location',
'dup' : 'duplicate'
}
respData = orgDataFrame.Resp_Data
labelData = orgDataFrame.label_num
secData = orgDataFrame.sec_label_num
newRespData = []
stop_words = set(stopwords.words('english'))
wordnet_lemmatizer = WordNetLemmatizer()
for line in respData:
line = line.replace('/', ' ')
tokens = word_tokenize( (str(line )).lower() )
tokens = tokens[2:]
newVal = map( lambda val: lookup_dict[val] if val in lookup_dict else val, tokens )
remDate = filter( lambda ThisWord: not re.match('^(?:(?:[0-9]{1,2}[:\/,]){1,2}[0-9]{1,4})$', ThisWord), newVal)
remInt = filter( lambda ThisWord: not re.match('^(\d{1,10}|\d{12})$', ThisWord), remDate)
remSplCh = filter( lambda ThisWord: not re.match('[^ a-zA-Z0-9]', ThisWord), remInt)
remDat = filter( lambda ThisWord: not re.match("(u')", ThisWord), remSplCh)
filSen = [w for w in remDat if not w in stop_words]
lemWord = map( wordnet_lemmatizer.lemmatize, filSen)
newLine = " ".join(lemWord)
newRespData.append(newLine)
newRespDataSeries = pd.Series( newRespData )
RPAInpData = formDataMultiLabel(newRespDataSeries, orgDataFrame)
return (newRespDataSeries, labelData, secData, RPAInpData)
########################## TF-IDF Vectorizer Pickle ###########################
def formTFIDFVectPickl(respData):
vect = TfidfVectorizer(min_df=1, max_df=1.0, stop_words='english')
respVect = vect.fit_transform(respData)
#### Create Pickle for Tfidf Vectorizer ####
pickle.dump(vect, open('TfidfVect.pkl', 'wb'))
return respVect
########################## RF Classifier for Manual Action ####################
def RFManAct(rData, lData):
randClass = RandomForestClassifier(n_estimators = 100)
randClass.fit(rData, lData)
#### Create Pickle for RFManAct ####
pickle.dump(randClass, open('RFManAct.pkl', 'wb'))
########################## RF Classifier for Next Step ########################
def RFSeconStep(rData, lData):
randClass = RandomForestClassifier(n_estimators = 100)
randClass.fit(rData, lData)
#### Create Pickle for RFManAct ####
pickle.dump(randClass, open('RFSeconStep.pkl', 'wb'))
########################## RF Classifier for RPA Input Data ###################
def RFInpData(rData, lData):
randClass = RandomForestClassifier(n_estimators = 100)
randClass.fit(rData, lData)
#### Create Pickle for RFManAct ####
pickle.dump(randClass, open('RFInpData.pkl', 'wb'))
########################## Main Function ##########################
def main():
respData, manActData, nxtStepData, RPAInpData = readFormatData(DATA_LOCATION)
### Form Vectorizer Pickle #####
respVectData = formTFIDFVectPickl(respData)
### RF Classifier for Manual Action Taken ###
RFManAct(respVectData, manActData)
### RF Classifier for Second Step ###
RFSeconStep(respVectData, nxtStepData)
### RF Classifier for RPA Input Data ###
RFInpData(respVectData, RPAInpData)
if __name__ == "__main__":
main()