-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_train_test_files.py
95 lines (82 loc) · 2.55 KB
/
create_train_test_files.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
import re
import json
import math
import psycopg2
import psycopg2.extras
import os.path
# connect to database
conn = psycopg2.connect(host="", database="", user="", password="")
#cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur = conn.cursor()
# get recipes
cur.execute("SELECT recipe_id, vector, second_vector, text_vector FROM public.recipe ORDER BY recipe_id")
recipes = cur.fetchall()
recipes_count = len(recipes)
recipes_dict = {}
for recipe_row in recipes:
recipes_dict[recipe_row[0]] = {'vector': recipe_row[1], 'second_vector': recipe_row[2], 'text_vector': recipe_row[3]}
del recipes
print(recipes_count)
# get users
cur.execute("SELECT user_id, vector FROM public.user ORDER BY user_id")
users = cur.fetchall()
users_count = len(users)
users_dict = {}
for user_row in users:
users_dict[user_row[0]] = user_row[1]
del users
print(users_count)
# initialize
f = open("all.txt", "a")
batch_size = 1000
low_index = 1
high_index = batch_size
# iterate
counter = 0
while True:
# get data
cur.execute("SELECT user_id, recipe_id, rate FROM public.review WHERE review_id BETWEEN %s AND %s ORDER BY review_id ASC", (low_index, high_index,))
reviews = cur.fetchall()
# update index
low_index += batch_size
high_index += batch_size
if len(reviews) == 0:
break
# write file
for review_row in reviews:
# get users
cur.execute("SELECT vector, average_rate FROM public.user WHERE user_id = %s", (review_row[0],))
user_row = cur.fetchone()
# get recipes
cur.execute("SELECT vector, second_vector, text_vector, average_rate FROM public.recipe WHERE recipe_id = %s", (review_row[1],))
recipe_row = cur.fetchone()
f.write(
"{}#{}#{}#{}#{}#{}#{}#{}\n".format(
user_row[0],
recipe_row[0],
recipe_row[1],
recipe_row[2],
"{},{}".format(str(round(user_row[1] / 5, 3)), str(round(recipe_row[3] / 5, 3)),),
str(review_row[2]),
str(review_row[0]),
str(review_row[1])
)
)
counter += 1
print(counter)
del reviews
f.close()
num_total_samples = 2975564
num_training_samples = int(num_total_samples * 0.8)
num_testing_samples = num_total_samples - num_training_samples
f = open("all.txt", "r")
f_train = open("train.txt", "a")
f_test = open("test.txt", "a")
counter = 0
for line in f:
counter += 1
if counter <= num_training_samples:
f_train.write(line)
else:
f_test.write(line)
print(counter)