-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrivercode.py
More file actions
155 lines (115 loc) · 4.65 KB
/
Copy pathdrivercode.py
File metadata and controls
155 lines (115 loc) · 4.65 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
#multinomialNB algorithm is used
#ngram is used to check the text similarity
from flask import Flask,request
from datetime import datetime
from flask import jsonify
from flask_json import FlaskJSON, JsonError, json_response, as_json
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
app = Flask(__name__)
json = FlaskJSON(app)
json.init_app(app)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import operator
import re
#ngram model-- used to obtain the ngrams of the sourcefield data
def ngrams(string, n=3):
string = "".join(string.split())
string = re.sub(r'[,-./_]|\sBD',r'', string)
ngrams = zip(*[string[i:] for i in range(n)])
m = [''.join(ngram) for ngram in ngrams]
m = [n.lower() for n in m]
return m
#function to classify the source field and obtain the confidence
def predictionAndMatching(word,input,df):
ngram_transformer = CountVectorizer(analyzer = ngrams).fit(df['title'])
test = ngram_transformer.transform(word)
prediction= model.predict(test)
probabilities = (model.predict_proba(test))
print(prediction)
return probabilities, prediction
#function to train the model [model learning]
def trainmodel(df):
global ngram_transformer
ngram_transformer = CountVectorizer(analyzer = ngrams).fit(df['title'])
title_ngram = ngram_transformer.transform(df['title'])
print(title_ngram)
Tfidf_transformer = TfidfTransformer().fit(title_ngram)
title_tfidf = Tfidf_transformer.transform(title_ngram)
global model
model = MultinomialNB().fit(title_tfidf, df['category'])
global model
@app.route('/train/format/match', methods=['GET', 'POST'])
def api1():
source = request.json
df ={'title':source['source']['formatFields'], 'category': source['target']['formatFields']}
trainmodel(df)
response ={
"sourceformatName": source['source']['formatName'],
"targetformatName": source['target']['formatName'],
"overallConfidence": 0,
"mappings": [
]
}
maps = []
overallConfidence = 0
for key in df['title']:
wordtoMap=[key]
obtainedMapping = predictionAndMatching(wordtoMap,source,df)
#print(r)
mappings={"sourceField" : key,"targetField" :obtainedMapping[1][0] ,"confidence" :max(obtainedMapping[0][0])*100 }
maps.append(mappings)
overallConfidence = overallConfidence+max(obtainedMapping[0][0])*100
overallConfidence = overallConfidence/len(df['title'])
response['mappings']= maps
response ['overallConfidence'] =overallConfidence
type(response)
return jsonify(response)
@app.route('/train/format/learn', methods=['GET', 'POST'])
def api2():
input = request.json
dict2 = {}
for i in range(0, len(input["mappings"])):
dict2[input["mappings"][i].get('sourceField')] = input["mappings"][i].get('targetField')
df ={'title':list(dict2.keys()), 'category':list( dict2.values())}
trainmodel(df)
response ={
"sourceformatName": input['source']['formatName'],
"targetformatName": input['target']['formatName'],
"message": "Learned the mappings",
}
return jsonify(response)
@app.route('/format/match', methods=['GET', 'POST'])
def api3():
ip = request.json
df ={'title':ip['source']['formatFields'], 'category': ip['target']['formatFields']}
#classify2.csv contains the training data
#this file is to train the model using sample dataset
#this api predicts the targetField of all sourcefield given as input and also the confidence value is obtained
dx = pd.read_csv("D:\classify2.csv")
trainmodel(dx)
response ={
"sourceformatName": ip['source']['formatName'],
"targetformatName": ip['target']['formatName'],
"overallConfidence": 0,
"mappings": [
]
}
maps = []
confidence = 0
for key in df['title']:
wordToPredict=[key]
prediction = predictionAndMatching(wordToPredict,ip,dx)
mappingss={"sourceField" : key,"targetField" :prediction[1][0] ,"confidence" :max(prediction[0][0])*100 }
maps.append(mappingss)
confidence = confidence+max(prediction[0][0])*100
confidence = confidence/len(df['title'])
response['mappings']= maps
response ['overallConfidence'] =confidence
type(response)
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)