File size: 2,018 Bytes
dfe35ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import jsonschema
from jsonschema import validate
import os
import pandas as pd
import sys

project_root_path = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)

if project_root_path not in sys.path:
    sys.path.insert(0, project_root_path)

from chinatravel.environment.tools import Attractions


class AttractionsOODTag(Attractions):
    def __init__(
        self,
        base_path: str = os.path.dirname(__file__) + "/eval_annotation/attractions/",
        en_version=False,
    ):
        super().__init__(en_version=en_version)
        city_list = [
            "beijing",
            "shanghai",
            "nanjing",
            "suzhou",
            "hangzhou",
            "shenzhen",
            "chengdu",
            "wuhan",
            "guangzhou",
            "chongqing",
        ]
        curdir = os.path.dirname(os.path.realpath(__file__))
        self.ood_tag = {}
        for city in city_list:
            self.ood_tag[city] = pd.read_csv(
                os.path.join(curdir, f"{base_path}/{city}/attractions_tag.csv")
            )
        city_cn_list = [
            "北京",
            "上海",
            "南京",
            "苏州",
            "杭州",
            "深圳",
            "成都",
            "武汉",
            "广州",
            "重庆",
        ]
        for i, city in enumerate(city_list):
            self.ood_tag[city_cn_list[i]] = self.ood_tag.pop(city)
        for city in city_cn_list:
            self.data[city] = pd.merge(
                self.data[city], self.ood_tag[city], on=["id", "name"], how="left"
            )
            # print(self.data[city])
        del self.ood_tag


def load_json_file(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        return json.load(f)


def validate_json(json_data, schema):
    try:
        validate(instance=json_data, schema=schema)
        return True
    except jsonschema.exceptions.ValidationError as e:
        return False