| 1124 | |
| 1125 | |
| 1126 | class FusionSeqRecDataset(BaseDataset): |
| 1127 | def __init__(self, train_file, item_file, index_file, tokenizer, max_len=2048, sample=-1, test=False, seed=0, category="", dedup=False): |
| 1128 | """ |
| 1129 | Fusion dataset combining sequence recommendation with item features. |
| 1130 | Uses semantic IDs for user history, outputs item titles or descriptions. |
| 1131 | |
| 1132 | Args: |
| 1133 | train_file: Path to CSV file with sequence data |
| 1134 | item_file: Path to .item.json file with item features |
| 1135 | index_file: Path to .index.json file with item indices |
| 1136 | tokenizer: Tokenizer for encoding text |
| 1137 | max_len: Maximum sequence length |
| 1138 | sample: Number of samples to use (-1 for all) |
| 1139 | test: Whether this is test mode |
| 1140 | seed: Random seed |
| 1141 | category: Category name for prompts |
| 1142 | dedup: Whether to filter duplicate items |
| 1143 | """ |
| 1144 | BaseDataset.__init__(self, tokenizer, max_len, test, category, dedup, seed) |
| 1145 | |
| 1146 | # Initialize CSV part |
| 1147 | self.data = pd.read_csv(train_file) |
| 1148 | if sample > 0: |
| 1149 | self.data = self.data.sample(sample, random_state=seed) |
| 1150 | |
| 1151 | # Initialize JSON part |
| 1152 | with open(item_file, 'r') as f: |
| 1153 | self.item_feat = json.load(f) |
| 1154 | with open(index_file, 'r') as f: |
| 1155 | self.indices = json.load(f) |
| 1156 | |
| 1157 | # Build sid2title and sid2description mappings |
| 1158 | self.sid2title = {} |
| 1159 | self.sid2description = {} |
| 1160 | |
| 1161 | for item_id, sids in self.indices.items(): |
| 1162 | if item_id in self.item_feat: |
| 1163 | title = self.item_feat[item_id]['title'] |
| 1164 | description = self.item_feat[item_id]['description'] |
| 1165 | |
| 1166 | # Process description according to requirements: |
| 1167 | # 1. If description is empty, use title |
| 1168 | # 2. If description is a list, select the longest one |
| 1169 | # 3. If the longest in list is also empty, use title |
| 1170 | processed_description = self._process_description(description, title) |
| 1171 | |
| 1172 | # Concatenate all three semantic IDs as the key |
| 1173 | if len(sids) >= 3: |
| 1174 | combined_sid = sids[0] + sids[1] + sids[2] |
| 1175 | self.sid2title[combined_sid] = title |
| 1176 | self.sid2description[combined_sid] = processed_description |
| 1177 | # print("self.sid2title: ", self.sid2title) |
| 1178 | # print("self.sid2description: ", self.sid2description) |
| 1179 | self.get_inputs() |
| 1180 | |
| 1181 | def _process_description(self, description, title): |
| 1182 | """ |
| 1183 | Process description according to the requirements: |
no outgoing calls