import numpy as np
import json
import csv

db_json_dir = '/data/chenxiaolin/dataset/db.json'
db_csv_dir = '/data/chenxiaolin/dataset/db.csv'
# 内容中有,的话replace

def load_json(file_name):
    f_json = open(file_name, 'r')
    load_content = json.load(f_json)
    return load_content

def write_csv(file_name, to_write_list):
    f_csv = open(file_name, 'w', encoding='UTF-8-sig', newline='')
    writer = csv.writer(f_csv)
    writer.writerows(to_write_list)


json_content = load_json(db_json_dir)

skip_list = ['url', 'imgs', 'tips']
relation_list = ['venueScore', 'venueAddress', 'subDomains', 'Credit Cards', 'desc', 'Menus', 'venueNeigh', 'terms', 'Parking', 'telephone', 'Dining Options', 'Restroom', 'sub_sightseeing', 'Wi-Fi', 'Music', 'timeFrame', 'Outdoor Seating', 'Wheelchair Accessible', 'Smoking', 'Reservations', 'Drinks', 'venueCat', 'price', 'domain']
# relation_list = ['venueName', 'venueScore', 'venueAddress', 'subDomains', 'Credit Cards', 'desc', 'url', 'Menus', 'venueNeigh', 'terms', 'Parking', 'telephone', 'Dining Options', 'Restroom', 'sub_sightseeing', 'Wi-Fi', 'Music', 'timeFrame', 'Outdoor Seating', 'Wheelchair Accessible', 'Smoking', 'Reservations', 'Drinks', 'venueCat', 'price', 'imgs', 'tips', 'domain'] origin
# print(len(json_content))

to_write_list = []
replace_str = ','

for each_entity in json_content:
    # print(each_entity)
    entity_name = each_entity['venueName']
    if replace_str in entity_name:
        entity_name = entity_name.replace(replace_str, '')

    for each_relation in relation_list:
        each_relation_list = []
        entity_tail = each_entity[each_relation]
        if len(entity_tail) > 0:
            if replace_str in each_relation:
                each_relation = each_relation.replace(replace_str, '')
            if replace_str in entity_tail:  # tail
                entity_tail = entity_tail.replace(',','')
            # list
            if isinstance(entity_tail,list):
                entity_tail = ' '.join(entity_tail)
            each_relation_list = [entity_name, each_relation, entity_tail]
            to_write_list.append(each_relation_list)

# save_csv_file
write_csv(db_csv_dir, to_write_list)
