Parse a line with URL, JSON, and embedding into document objects. Args: line: Tab-separated line with URL, JSON, and embedding site: Site identifier Returns: List of document objects
(line, site)
| 207 | return [], [] |
| 208 | |
| 209 | def documents_from_csv_line(line, site): |
| 210 | """ |
| 211 | Parse a line with URL, JSON, and embedding into document objects. |
| 212 | |
| 213 | Args: |
| 214 | line: Tab-separated line with URL, JSON, and embedding |
| 215 | site: Site identifier |
| 216 | |
| 217 | Returns: |
| 218 | List of document objects |
| 219 | """ |
| 220 | try: |
| 221 | url, json_data, embedding_str = line.strip().split('\t') |
| 222 | embedding_str = embedding_str.replace("[", "").replace("]", "") |
| 223 | embedding = [float(x) for x in embedding_str.split(',')] |
| 224 | js = json.loads(json_data) |
| 225 | js = trim_schema_json(js, site) |
| 226 | except Exception as e: |
| 227 | print(f"Error processing line: {e!s}") |
| 228 | return [] |
| 229 | |
| 230 | # Skip if trim_schema_json returned None |
| 231 | if js is None: |
| 232 | return [] |
| 233 | |
| 234 | documents = [] |
| 235 | if not isinstance(js, list): |
| 236 | js = [js] |
| 237 | |
| 238 | for i, item in enumerate(js): |
| 239 | # Skip None items in the list |
| 240 | if item is None: |
| 241 | continue |
| 242 | |
| 243 | # No longer filtering by should_include_item - trimming already handles this |
| 244 | item_url = url if i == 0 else f"{url}#{i}" |
| 245 | name = get_item_name(item) |
| 246 | |
| 247 | # Ensure no None values in the document |
| 248 | doc = { |
| 249 | "id": str(int64_hash(item_url)), |
| 250 | "embedding": embedding, |
| 251 | "schema_json": json.dumps(item), |
| 252 | "url": item_url or "", |
| 253 | "name": name or "Unnamed Item", |
| 254 | "site": site or "unknown" |
| 255 | } |
| 256 | |
| 257 | # Additional validation to ensure no None values |
| 258 | for key, value in doc.items(): |
| 259 | if value is None: |
| 260 | print(f"Warning: None value found for field '{key}' in document") |
| 261 | if key == "embedding": |
| 262 | doc[key] = [] |
| 263 | else: |
| 264 | doc[key] = "" |
| 265 | |
| 266 | documents.append(doc) |
no test coverage detected