-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreateFeatureVector.py
More file actions
70 lines (58 loc) · 2.09 KB
/
createFeatureVector.py
File metadata and controls
70 lines (58 loc) · 2.09 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
import json
import pymongo
import getopt
import sys
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 getUsers(id_experiment, type, db):
collection = 'users'
return db[collection].find({'id_experiment':id_experiment, 'type':type})
def createFeatures(id_experiment, users, db, N):
for user in users:
types = findTypes(id_experiment, user['id_user'], db, N)
storeTypes(types, id_experiment, user['id_user'], db)
#storeTypes(types)
def findTypes(id_experiment,id_user, db, N):
collection = 'tweets'
if N != None:
tweets = db[collection].find({'id_experiment':id_experiment, 'id_user':id_user},{'annotation.types':1,'_id':0}).sort([('create_at',1)]).limit(N)
else:
tweets = db[collection].find({'id_experiment':id_experiment, 'id_user':id_user},{'annotation.types':1,'_id':0})
features = {}
for t in tweets:
if 'annotation' in t:
for i in t['annotation']:
for type in i['types']:
if type in features:
features[type] += 1
else:
features[type] = 1
# print(features)
return features
def storeTypes(types, id_experiment,id_user, db):
collection='users'
db[collection].update({'id_experiment':id_experiment,'id_user':id_user}, {'$set':{'features': types}})
def main():
try:
db = loginMongo()
except:
print('error login Mongo')
try:
opts, args = getopt.getopt(sys.argv[1:], 'n:s:e: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)
createFeatures(id_experiment, users, db, N)
if __name__ == "__main__":
main()