File size: 3,034 Bytes
227f4db
cd85390
 
 
 
 
227f4db
2de3cd4
ff36b01
cd85390
 
b580105
33ecfa0
63a54d0
cd85390
 
 
 
227f4db
 
 
 
 
cd85390
 
 
 
 
 
4bf038a
b580105
 
4bf038a
 
 
 
cd85390
 
 
 
 
 
 
4bf038a
 
 
 
 
 
 
 
c42a8bf
 
cd7ea66
 
 
 
c42a8bf
 
 
 
 
 
00ad83a
 
 
 
 
 
 
 
30739e9
 
 
 
 
 
 
 
7b8d81e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d51055e
 
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
from copy import deepcopy
import json



class Test:
    _zero_state = {"value":-1, "responses":[]}
    names = ["袘袦", "袛小", "袉小", "袗袛", "袝小", "袨楔", "袗袣", "袗楔", "肖小", "袦袙", "袧袚", "袧袩"]

    def __init__(self):
        self.data = self.read_test_data()
        self.descriptions = self.read_txt("images/descriptions.txt")
        self.full_names = self.read_txt("photos/names.txt")
        self.titles = self.read_txt("photos/titles.txt")

    def __len__(self):
        return len(self.data)

    @property
    def zero_state(self):
        state = deepcopy(self._zero_state)
        return state

    @staticmethod
    def read_test_data():
        with open("test.json") as f:
            test_data = json.load(f)
        return test_data

    @staticmethod
    def read_txt(path):
        with open(path) as f:
            lines = f.readlines()
            clean_lines = [line.strip() for line in lines]
        return clean_lines

    def get_question(self, idx):
        question = self.data[idx]["question"]
        return question
    
    def get_answers(self, idx):
        responces = self.data[idx]["responces"]
        answers = [responce["answer"] for responce in responces]
        return answers
    
    def get_description(self, idx):
        description = self.descriptions[idx]
        return description
    
    def get_image_path(self, idx):
        image_path = f"images/{idx}.jpg"
        return image_path
    
    def get_photos_path(self, idx):
        image_path = f"photos/{idx}.jpg"
        return image_path
    
    def convert_to_answer_ids(self, responces):
        answer_ids = []
        for idx, responce in enumerate(responces):
            answers = self.get_answers(idx)
            index = answers.index(responce)
            answer_ids.append(index)
        return answer_ids
    
    def total_name_count(self):
        name_count = {name: 0 for name in self.names}
        for question in self.data:
            for answer in question["responces"]:
                for name in answer["name"]:
                    name_count[name] += 1
        return name_count
    
    def current_name_count(self, answer_ids):
        name_count = {name: 0 for name in self.names}
        for question, answer_idx in zip(self.data, answer_ids):
            answer = question["responces"][answer_idx]
            for name in answer["name"]:
                name_count[name] += 1
        return name_count
    
    def select_best(self, answer_ids):
        total_name_count = self.total_name_count()
        current_name_count = self.current_name_count(answer_ids)

        name_percent = {}

        for name in self.names:
            percent = current_name_count[name] / total_name_count[name]
            name_percent[name] = percent
        
        best_name = self.dict_max(name_percent)
        return best_name

    @staticmethod
    def dict_max(dict):
        key = max(dict, key=dict.get)
        max_key_index = list(dict.keys()).index(key)
        return max_key_index