86d4cb139d09640fdf7b49d68fce90fe42e01c9b
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / annotation_converters / ncf_converter.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 """
16
17
18 from ..representation import HitRatioAnnotation
19 from ..utils import read_txt, get_path
20 from ..config import PathField, NumberField
21
22 from .format_converter import BaseFormatConverter, BaseFormatConverterConfig
23
24
25 class NCFDatasetConverterConfig(BaseFormatConverterConfig):
26     raiting_file = PathField()
27     negative_file = PathField()
28     users_max_number = NumberField(optional=True)
29
30
31 class NCFConverter(BaseFormatConverter):
32     __provider__ = "ncf_converter"
33
34     _config_validator_type = NCFDatasetConverterConfig
35
36     def configure(self):
37         self.raiting_file = self.config['raiting_file']
38         self.negative_file = self.config['negative_file']
39         if 'users_max_number' in self.config:
40             self.users_max_number = self.config['users_max_number']
41         else:
42             self.users_max_number = -1
43
44     def convert(self):
45         annotations = []
46         users = []
47
48         for file_row in read_txt(self.raiting_file):
49             user_id, item_id, _ = file_row.split()
50             users.append(user_id)
51             identifier = ['u:'+user_id, 'i:' + item_id]
52             annotations.append(HitRatioAnnotation(identifier))
53             if self.users_max_number > 0 and len(users) >= self.users_max_number:
54                 break;
55
56         item_numbers = 1
57
58         items_neg = []
59         with get_path(self.negative_file).open() as content:
60             for file_row in content:
61                 items = file_row.split()
62                 items_neg.append(items)
63                 if self.users_max_number > 0 and len(items_neg) >= self.users_max_number:
64                     break;
65
66         if items_neg:
67             iterations = len(items_neg[0])
68             item_numbers += iterations
69             for i in range(iterations):
70                 for user in users:
71                     item = items_neg[int(user)][i]
72                     identifier = ['u:' + user, 'i:' + item]
73                     annotations.append(HitRatioAnnotation(identifier, False))
74
75         return annotations, {'users_number': len(users), 'item_numbers': item_numbers}