-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreateInstanceVector.py
More file actions
103 lines (89 loc) · 3.34 KB
/
createInstanceVector.py
File metadata and controls
103 lines (89 loc) · 3.34 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
import json
import pymongo
import getopt
import sys
import mysql.connector
from mysql.connector import errors
def loginMongo():
fileKeys = open('adressMongo.json').read()
keys = json.loads(fileKeys)
client = pymongo.MongoClient(keys['adress_local'],int(keys['port_local']))
return client[keys['name_db']]
def loginMySql():
fileKeys = open('adressMySQL.json').read()
keys = json.loads(fileKeys)
try:
client = mysql.connector.connect(user=keys["user"], password=keys["password"], host=keys["host"], database = keys["database"])
cursor = client.cursor()
except mysql.connector.errors.ProgrammingError:
print("crea il db")
return client, cursor
def getUsers(id_experiment, type, db):
collection = 'users'
return db[collection].find({'id_experiment':id_experiment, 'type':type})
def getExpertTypes(id_experiment, cursor):
command = ("SELECT type FROM expert_types WHERE id_experiment='"+id_experiment+"'")
cursor.execute(command)
expert_types = []
for c in cursor:
expert_types.append(c[0])
return expert_types
def createFeatures(id_experiment, users, expert_types, db, N):
for user in users:
instances = findInstances(id_experiment, user['id_user'], expert_types, db, N)
storeInstances(instances, id_experiment, user['id_user'], db)
#storeTypes(types)
def findInstances(id_experiment,id_user, expert_types, db, N):
collection = 'tweets'
if N != None:
tweets = db[collection].find({'id_experiment':id_experiment, 'id_user':id_user},{'annotation.types':1,'_id':0, 'annotation.uri':1}).sort([('create_at',-1)]).limit(N)
else:
tweets = db[collection].find({'id_experiment':id_experiment, 'id_user':id_user},{'annotation.types':1,'_id':0, 'annotation.uri':1})
instances = {}
# print(expert_types)
for t in tweets:
if 'annotation' in t:
for i in t['annotation']:
for type in i['types']:
type = type.split('ontology/')[1]
print(type)
if type in expert_types:
inst = i['uri'].split('/')[-1].replace('.','')
if inst in instances:
instances[inst] += 1
else:
instances[inst] = 1
break
return instances
def storeInstances(instances, id_experiment,id_user, db):
collection='users'
db[collection].update({'id_experiment':id_experiment,'id_user':id_user}, {'$set':{'instances': instances}})
def main():
try:
db = loginMongo()
except:
print('error login Mongo')
try:
dbSQL, cursor = loginMySql()
except:
print('error login Mongo')
try:
opts, args = getopt.getopt(sys.argv[1:], 'x:')
except getopt.GetoptError as err:
# print help information and exit:
print('err') # will print something like "option -a not recognized"
# usage()
sys.exit(2)
id_experiment = args[0]
type = args[1]
N = None
if len(args)>2:
N = int(args[2])
users = getUsers(id_experiment, type, db)
expert_types = getExpertTypes(id_experiment, cursor)
createFeatures(id_experiment, users, expert_types, db, N)
dbSQL.commit()
cursor.close()
dbSQL.close()
if __name__ == "__main__":
main()