File size: 1,863 Bytes
1dda194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared benchmark data and image loading."""
import json
from pathlib import Path

DEFAULT_ROOT = Path(__file__).resolve().parent.parent / "data"

QUERY_FILE = "mipr_query.jsonl"
CORPUS_FILE = "mipr_corpus.jsonl"
ANNOTATION_FILE = "mipr_annotation.jsonl"

GRADES = ("L1", "L2", "N1", "N2", "N3")


def data_root(explicit=None):
    return Path(explicit) if explicit else DEFAULT_ROOT


def load_jsonl(path):
    items = []
    with open(path, encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if line:
                items.append(json.loads(line))
    return items


def load_queries(root=None):
    """List of {"idx", "query", "image_url"} in file order."""
    return load_jsonl(data_root(root) / QUERY_FILE)


def load_corpus(root=None):
    """item_id -> corpus record (title, pict_url, treecate_full_path)."""
    return {d["item_id"]: d for d in load_jsonl(data_root(root) / CORPUS_FILE)}


def load_treecate(root=None):
    """item_id -> leaf-extended category path (evaluation unit of SC-R)."""
    return {d["item_id"]: d["treecate_full_path"]
            for d in load_jsonl(data_root(root) / CORPUS_FILE)}


def load_annotation(root=None):
    """qid -> {item_id: final_grade}, all five grades kept."""
    ann = {}
    for d in load_jsonl(data_root(root) / ANNOTATION_FILE):
        ann[str(d["idx"])] = {
            it["item_id"]: it["final_grade"] for it in d["items"]
        }
    return ann


def query_image_path(root, image_url):
    path = data_root(root) / "images" / "query_images" / image_url
    if not path.is_file():
        raise FileNotFoundError(path)
    return str(path.resolve())


def product_image_path(root, pict_url):
    path = data_root(root) / "images" / "product_images" / pict_url
    if not path.is_file():
        raise FileNotFoundError(path)
    return str(path.resolve())