-
Notifications
You must be signed in to change notification settings - Fork 1
/
processtor.py
133 lines (110 loc) · 5.23 KB
/
processtor.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
# -*- coding:utf-8 -*-
# @Time: 2020/1/14 10:41
# @Author: jockwang, [email protected]
import argparse
import numpy as np
import logging
def process(dataset='book',bathpath='data'):
RATING_FILE_NAME = dict({'movie': 'ratings.dat', 'book': 'BX-Book-Ratings.csv', 'news': 'ratings.txt'})
SEP = dict({'movie': '::', 'book': ';', 'news': '\t'})
THRESHOLD = dict({'movie': 4, 'book': 0, 'news': 0})
BASEPATH = bathpath
def read_item_index_to_entity_id_file():
file = BASEPATH + DATASET + '/item_index2entity_id_rehashed.txt'
logging.info('reading item index to entity id file: ' + file + ' ...')
i = 0
for line in open(file, encoding='utf-8').readlines():
item_index = line.strip().split('\t')[0]
satori_id = line.strip().split('\t')[1]
item_index_old2new[item_index] = i
entity_id2index[satori_id] = i
i += 1
def convert_rating():
file = BASEPATH + DATASET + '/' + RATING_FILE_NAME[DATASET]
logging.info('reading rating file ...')
item_set = set(item_index_old2new.values())
user_pos_ratings = dict()
user_neg_ratings = dict()
for line in open(file, encoding='utf-8').readlines()[1:]:
array = line.strip().split(SEP[DATASET])
# remove prefix and suffix quotation marks for BX dataset
if DATASET == 'book':
array = list(map(lambda x: x[1:-1], array)) # 去掉双引号
item_index_old = array[1] # item列
if item_index_old not in item_index_old2new: # the item is not in the final item set
continue
item_index = item_index_old2new[item_index_old]
user_index_old = int(array[0]) # 用户
rating = float(array[2]) # 评分
if rating >= THRESHOLD[DATASET]: # 评分阈值
if user_index_old not in user_pos_ratings: # 积极评分
user_pos_ratings[user_index_old] = set()
user_pos_ratings[user_index_old].add(item_index)
else:
if user_index_old not in user_neg_ratings: # 消极评分
user_neg_ratings[user_index_old] = set()
user_neg_ratings[user_index_old].add(item_index)
logging.info('converting rating file ...')
writer = open(BASEPATH + DATASET + '/ratings_final.txt', 'w', encoding='utf-8')
user_cnt = 0
user_index_old2new = dict()
for user_index_old, pos_item_set in user_pos_ratings.items():
if user_index_old not in user_index_old2new:
user_index_old2new[user_index_old] = user_cnt
user_cnt += 1
user_index = user_index_old2new[user_index_old]
for item in pos_item_set:
writer.write('%d\t%d\t1\n' % (user_index, item))
unwatched_set = item_set - pos_item_set
if user_index_old in user_neg_ratings:
unwatched_set -= user_neg_ratings[user_index_old]
for item in np.random.choice(list(unwatched_set), size=len(pos_item_set), replace=False):
writer.write('%d\t%d\t0\n' % (user_index, item))
writer.close()
logging.info('number of users: %d' % user_cnt)
logging.info('number of items: %d' % len(item_set))
return user_cnt, len(item_set)
def convert_kg():
logging.info('converting kg file ...')
entity_cnt = len(entity_id2index)
relation_cnt = 0
writer = open(BASEPATH + DATASET + '/kg_final.txt', 'w', encoding='utf-8')
files = []
if DATASET == 'movie':
files.append(open(BASEPATH + DATASET + '/kg_part1_rehashed.txt', encoding='utf-8'))
files.append(open(BASEPATH + DATASET + '/kg_part2_rehashed.txt', encoding='utf-8'))
else:
files.append(open(BASEPATH + DATASET + '/kg_rehashed.txt', encoding='utf-8'))
for file in files:
for line in file:
array = line.strip().split('\t')
head_old = array[0]
relation_old = array[1]
tail_old = array[2]
if head_old not in entity_id2index:
entity_id2index[head_old] = entity_cnt
entity_cnt += 1
head = entity_id2index[head_old]
if tail_old not in entity_id2index:
entity_id2index[tail_old] = entity_cnt
entity_cnt += 1
tail = entity_id2index[tail_old]
if relation_old not in relation_id2index:
relation_id2index[relation_old] = relation_cnt
relation_cnt += 1
relation = relation_id2index[relation_old]
writer.write('%d\t%d\t%d\n' % (head, relation, tail))
writer.close()
logging.info('number of entities (containing items): %d' % entity_cnt)
logging.info('number of relations: %d' % relation_cnt)
return entity_cnt
np.random.seed(555)
DATASET = dataset
entity_id2index = dict()
relation_id2index = dict()
item_index_old2new = dict()
read_item_index_to_entity_id_file()
user, item = convert_rating()
all_ = convert_kg()
logging.info('done')
return user, item, all_