-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfoCollector.py
161 lines (138 loc) · 5.65 KB
/
infoCollector.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
import mysql.connector
import time
import json
from bson import json_util
from kafka import KafkaProducer
from pymongo import MongoClient
import random
from scipy import stats
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from collections import defaultdict
plt.style.use(style='seaborn')
start_time = time.time()
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="tfm"
)
#IA PARAMETERS weighted random choices
#random.choices(population, weights=None, *, cum_weights=None, k=1)
#population: It is is sequence or data structure from which you want to choose data.
#weights or cum_weights: Define the selection probability for each element.
#weights: If a weights sequence is specified, random selections are made according to the relative weights.
#cum_weights: Alternatively, if a cum_weights sequence is given, the random selections are made according to the cumulative weights.
#k: The number of samples you want from a population.
#alpha: wight of the element with most probabilities
#Probability = element_weight/ sum of all weights
alpha = 0.8
#IA PARAMETERS Probability based on number of occurences
#probability = number of occurences of the pattern / total number of occurences
#method to generate n-grams:
#params:
#text-the text for which we have to generate n-grams
#ngram-number of grams to be generated from the text(1,2,3,4 etc., default value=1)
def generate_N_grams(text,ngram=1):
words=[word for word in text.split(" ") if word != ""]
#print("Numbers: ",words)
temp=zip(*[words[i:] for i in range(0,ngram)])
ans=[' '.join(ngram) for ngram in temp]
#print(ans)
return ans
def send_kafka_message(user, prediction):
producer = KafkaProducer(bootstrap_servers=['localhost:9092'],
value_serializer=lambda x:
json.dumps(x).encode('utf-8'))
data = { 'usuario' : user,
'versiones': prediction
}
mensajeIA = "com.master.demo.Entities.MensajeIA"
mensajeIABytes = mensajeIA.encode('utf-8')
lista = [("__TypeId__",mensajeIABytes)]
producer.send('my_topic3', headers = lista, value=data)
producer.flush()
def plot_ngrama(x, y, tipo, usuario):
plt.figure(1,figsize=(8,5))
plt.bar(x,y, color ='green',
width = 0.8)
plt.xlabel("Object versions reviewed by the user "+ usuario, fontsize = 16)
plt.ylabel("Count", fontsize = 16)
plt.title("Top 10 object versions reviewed by user "+usuario+"-"+tipo+" analysis", fontsize = 16)
plt.savefig(tipo+".png")
plt.show()
mycursor = mydb.cursor(dictionary=True)
name = 'LECTURA'
mycursor.execute("SELECT * FROM registro_peticion rp WHERE rp.tipo_peticion=%s", (name,))
myresult = mycursor.fetchall()
data = json.dumps(myresult)
#print(f"json: {json.dumps(myresult)}")
list_users = []
for item in myresult:
#print(item)
if item['usuario'] not in list_users:
list_users.append(item['usuario'])
print("list of users: ", list_users)
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
client = MongoClient('localhost:27017')
collection = client.tfm.registrosPeticion
if len(myresult) > 0:
y = collection.delete_many({})
x = collection.insert_many(myresult) #myresult comes from mysql cursor
#print(len(x.inserted_ids))
#Aqui comienza la parte de procesado para cada usuario y envio de mensaje a cola con el resultado de cada usuario
for usuario in list_users:
print("user: "+ usuario)
texto = ""
for item in myresult:
if item['usuario'] == usuario:
texto = texto + str(item['id_version']) + " "
#NGRAMAS
conteoNgrama=defaultdict(int)
conteoNgramaSinFiltrar = defaultdict(int)
for word in generate_N_grams(texto, 3):
numeros = word.split(' ')
conteoNgramaSinFiltrar[word]+=1
#solo guardamos el ngrama si no tiene valores repetidos
if (len(numeros) - len(set(numeros)) == 0):
conteoNgrama[word]+=1
#focus on more frequently occuring numbers
df_conteo=pd.DataFrame(sorted(conteoNgrama.items(),key=lambda x:x[1],reverse=True))
df_conteo_sin_filtrar=pd.DataFrame(sorted(conteoNgramaSinFiltrar.items(),key=lambda x:x[1],reverse=True))
#print("ngrama limpio: ")
#print(df_conteo.values.tolist())
#print("ngrama sin limpiar: ")
#print(df_conteo_sin_filtrar.values.tolist())
pd1=df_conteo[0][:10]
pd2=df_conteo[1][:10]
#plot_ngrama(pd1,pd2, "trigrama", usuario)
#Probability based on alpha parameter
#GEOMETRIC DISTRIBUTION p(r;p)=p(1−p)r−1
geometrica = stats.geom(alpha) # Distribución
x = np.arange(geometrica.ppf(0.0001),
geometrica.ppf(0.9999))
fmp = geometrica.pmf(x) # Función de Masa de Probabilidad
weights_p = []
for i in range(pd1.size):
if(0 <= i < fmp.size):
weights_p.append(fmp[i])
else:
weights_p.append(0)
print(str(weights_p))
version_mas_probable_alpha = random.choices(pd1, weights=weights_p, k=1)
#Probability based on number of occurences
weights_number = []
total_weight = 0
for i in range(pd1.size):
total_weight = total_weight + int(df_conteo[1][i])
print("total weight " + str(total_weight))
for j in range(pd1.size):
weights_number.append(df_conteo[1][j] / total_weight)
print("pesos basados en numero de apariciones: " + str(weights_number))
version_mas_probable_number = random.choices(pd1, weights=weights_number, k=1)
print("Version mas probable de trigrama con parametro alpha: "+ str(version_mas_probable_alpha[0]))
print("Version mas probable de trigrama con pesos basados en numero de apariciones: "+ str(version_mas_probable_number[0]))
send_kafka_message(usuario, str(version_mas_probable_alpha[0]))
print("--- %s seconds ---" % (time.time() - start_time))