|
|
import json
|
|
|
import argparse
|
|
|
|
|
|
def parse_args():
|
|
|
parser = argparse.ArgumentParser()
|
|
|
parser.add_argument('--review_data_path', type=str, default='./data')
|
|
|
parser.add_argument('--meta_data_path', type=str, default='./data')
|
|
|
parser.add_argument('--save_dir', type=str, default='./data/dataset')
|
|
|
parser.add_argument('--dataset', type=str, default='dataset')
|
|
|
return parser.parse_args()
|
|
|
|
|
|
args = parse_args()
|
|
|
|
|
|
import os
|
|
|
if not os.path.exists(args.save_dir + '/' + args.dataset):
|
|
|
os.mkdir(args.save_dir + '/' + args.dataset)
|
|
|
|
|
|
|
|
|
''' Extract interaction sequence '''
|
|
|
inters = {}
|
|
|
with open(args.review_data_path, 'r', encoding = 'utf-8') as file:
|
|
|
|
|
|
for line in file:
|
|
|
element = json.loads(line)
|
|
|
if element['reviewerID'] in inters:
|
|
|
inters[element['reviewerID']].append({'time': element['unixReviewTime'], 'item': element['asin']})
|
|
|
else:
|
|
|
inters[element['reviewerID']] = [{'time': element['unixReviewTime'], 'item': element['asin']}]
|
|
|
|
|
|
filtered_inters = {key: value for key, value in inters.items() if len(value) > 4}
|
|
|
final_inters = {}
|
|
|
for key, value in filtered_inters.items():
|
|
|
|
|
|
value.sort(key = lambda x: x['time'])
|
|
|
final_inters[key] = [x['item'] for x in value]
|
|
|
|
|
|
with open(args.save_dir + '/' + args.dataset + '/' + args.dataset + '.inters.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(final_inters, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
''' Extract user review '''
|
|
|
reviews = {}
|
|
|
with open(args.review_data_path, 'r', encoding = 'utf-8') as file:
|
|
|
|
|
|
for line in file:
|
|
|
element = json.loads(line)
|
|
|
if len(element.get('reviewText', '')) > 0:
|
|
|
reviews[element['reviewerID']] = {element['asin']: element['reviewText']}
|
|
|
else:
|
|
|
continue
|
|
|
with open(args.save_dir + '/' + args.dataset + '/' + args.dataset + '.reviews.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(reviews, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
''' Extract item features '''
|
|
|
features = {}
|
|
|
with open(args.meta_data_path, 'r', encoding = 'utf-8') as file:
|
|
|
|
|
|
for line in file:
|
|
|
element = json.loads(line)
|
|
|
if len(element.get('title', '')) > 0 and len(element.get('description', '')) > 0 and len(element.get('imageURL', '')) > 0 and len(element.get('imageURLHighRes', '')) > 0:
|
|
|
features[element['asin']] = {
|
|
|
'title': element['title'],
|
|
|
'description': element['description'],
|
|
|
'image': element['imageURL'],
|
|
|
'imageH': element['imageURLHighRes']}
|
|
|
else:
|
|
|
continue
|
|
|
with open(args.save_dir + '/' + args.dataset + '/' + args.dataset + '.features.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(features, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
''' Filter out modality deficiency '''
|
|
|
with open(args.save_dir + '/' + args.dataset + '/' + args.dataset + '.inters.json', 'r', encoding = 'utf-8') as file:
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.inters.json', 'r', encoding = 'utf-8') as file:
|
|
|
inters = json.load(file)
|
|
|
with open(args.save_dir + '/' + args.dataset + '/' + args.dataset + '.features.json', 'r', encoding = 'utf-8') as file:
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.features.json', 'r', encoding = 'utf-8') as file:
|
|
|
features = json.load(file)
|
|
|
|
|
|
full_inters = {}
|
|
|
number = 1
|
|
|
for key, value in inters.items():
|
|
|
full_value = []
|
|
|
for item in value:
|
|
|
if features.get(item, None) is None:
|
|
|
continue
|
|
|
elif len(features[item]['title']) == 0:
|
|
|
continue
|
|
|
elif len(' '.join(features[item]['description'])[:-1]) == 0:
|
|
|
continue
|
|
|
else:
|
|
|
try:
|
|
|
open_image = Image.open(requests.get(features[item]['imageH'][0], stream = True).raw)
|
|
|
except PIL.UnidentifiedImageError:
|
|
|
print('Caught UnidentifiedImageError.')
|
|
|
continue
|
|
|
except Exception as e:
|
|
|
print('Caught Exception.')
|
|
|
continue
|
|
|
full_value.append(item)
|
|
|
if len(full_value) > 4:
|
|
|
print('Interaction', number)
|
|
|
full_inters[key] = full_value
|
|
|
number += 1
|
|
|
else:
|
|
|
continue
|
|
|
|
|
|
with open(args.save_dir + '/' + args.dataset + '/' + args.dataset + '.inters.full.json', 'w', encoding = 'utf-8') as f:
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.inters.complete.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(full_inters, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
both, text, image, num = 0, 0, 0, 0
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/meta_Toys_and_Games.json', 'r', encoding = 'utf-8') as file:
|
|
|
for line in file:
|
|
|
element = json.loads(line)
|
|
|
text_image = text + image
|
|
|
num += 1
|
|
|
if len(element.get('title', '')) + len(element.get('description', '')) == 0:
|
|
|
text += 1
|
|
|
if len(element.get('imageURL', '')) + len(element.get('imageURLHighRes', '')) == 0:
|
|
|
image += 1
|
|
|
both = both + (text + image - text_image) // 2
|
|
|
|
|
|
print('item:', num)
|
|
|
print('both:', both)
|
|
|
print('text:', text)
|
|
|
print('image', image)
|
|
|
|
|
|
for value in full_inters.values():
|
|
|
all_items += value
|
|
|
|
|
|
items = list(set(all_items))
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.items.txt', 'w') as f:
|
|
|
for item in items:
|
|
|
f.write(item + '\n')
|
|
|
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.items.txt', 'r') as f:
|
|
|
items = f.read()
|
|
|
items = items.split('\n')
|
|
|
|
|
|
item_order_mapping = {}
|
|
|
item_order_reverse = {}
|
|
|
item_order = 0
|
|
|
for item in items:
|
|
|
item_order_mapping[item] = str(item_order)
|
|
|
item_order_reverse[str(item_order)] = item
|
|
|
item_order += 1
|
|
|
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.item_order_mapping.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(item_order_mapping, f, ensure_ascii = False, indent = 4)
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.item_order_reverse.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(item_order_reverse, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
import json
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.inters.full.json', 'r', encoding = 'utf-8') as file:
|
|
|
inters = json.load(file)
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.features.json', 'r', encoding = 'utf-8') as file:
|
|
|
features = json.load(file)
|
|
|
|
|
|
all_items = []
|
|
|
for value in inters.values():
|
|
|
all_items += value
|
|
|
items = list(set(all_items))
|
|
|
|
|
|
full_features = {}
|
|
|
for item in items:
|
|
|
full_features[item] = features[item]
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.features.complete.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(full_features, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.features.complete.json', 'r', encoding = 'utf-8') as f:
|
|
|
features = json.load(f)
|
|
|
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.item_order_mapping.json', 'r', encoding = 'utf-8') as f:
|
|
|
item_order_mapping = json.load(f)
|
|
|
|
|
|
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.inters.complete.json', 'r', encoding = 'utf-8') as f:
|
|
|
inters = json.load(f)
|
|
|
|
|
|
inter_reorder = {}
|
|
|
index = 0
|
|
|
for inter in inters:
|
|
|
inter_reorder[str(index)] = []
|
|
|
for item in inters[inter]:
|
|
|
inter_reorder[str(index)].append(int(item_order_mapping[item]))
|
|
|
index += 1
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.inters.numerical.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(inter_reorder, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
feature_reorder = {}
|
|
|
for item in features:
|
|
|
feature_reorder[item_order_mapping[item]] = features[item]
|
|
|
with open('/datain/v-yinju/LLMBased_Multimodal_RS/Data/Musical_Instruments/Musical_Instruments.features.numerical.json', 'w', encoding = 'utf-8') as f:
|
|
|
json.dump(feature_reorder, f, ensure_ascii = False, indent = 4)
|
|
|
|
|
|
|
|
|
with open('/datain/v-yinju/rqvae-zzx/data/Instruments/Instruments.inter.json', 'r', encoding = 'utf-8') as f:
|
|
|
inters = json.load(f)
|
|
|
with open('/datain/v-yinju/rqvae-zzx/data/Instruments/Instruments.index.json', 'r', encoding = 'utf-8') as f:
|
|
|
indices = json.load(f)
|
|
|
with open('/datain/v-yinju/rqvae-zzx/data/Instruments/Instruments.item.json', 'r', encoding = 'utf-8') as f:
|
|
|
features = json.load(f)
|
|
|
|
|
|
|
|
|
for uid, inter in inters:
|
|
|
remapped_inter = []
|
|
|
for item in inter:
|
|
|
if str(item) in indices:
|
|
|
remapped_inter.append(''.join(indices[str(item)]))
|
|
|
if len(remapped_inter) >= 5:
|
|
|
remapped_inters[uid] = remapped_inter
|
|
|
|
|
|
from datasets import load_dataset, Dataset
|
|
|
amazon = load_dataset("pandas", data_files= '/datain/v-yinju/rqvae-zzx/data/Instruments/Instruments.inter.test.pkl')['train']
|
|
|
SYSTEM_PROMPT = """
|
|
|
Below is an instruction that describes a task. Write a response that appropriately completes the request. Respond in the following format:
|
|
|
<reasoning>
|
|
|
...
|
|
|
</reasoning>
|
|
|
<answer>
|
|
|
...
|
|
|
</answer>
|
|
|
"""
|
|
|
|
|
|
Amazon = amazon.map(lambda x: {
|
|
|
'prompt': [
|
|
|
{'role': 'system', 'content': SYSTEM_PROMPT},
|
|
|
{'role': 'user', 'content': x['instruction']}
|
|
|
],
|
|
|
'answer': x['response']
|
|
|
}) |