Standardize string representations of lists, adding quotes around elements if missing, and safely evaluate to Python list. Returns original item if parsing fails.
(item: str)
| 12 | return predictions |
| 13 | |
| 14 | def fix_list_format(item: str) -> Any: |
| 15 | """ |
| 16 | Standardize string representations of lists, adding quotes around elements if missing, |
| 17 | and safely evaluate to Python list. Returns original item if parsing fails. |
| 18 | """ |
| 19 | if not isinstance(item, str): |
| 20 | return item |
| 21 | match = re.match(r"^\[(.*)\]$", item.strip()) |
| 22 | if not match: |
| 23 | return item |
| 24 | content = match.group(1) |
| 25 | corrected = re.sub(r"(?<!['\w])(\w[^,]*?)(?!['\w])", r"'\1'", content) |
| 26 | try: |
| 27 | return ast.literal_eval(f"[{corrected}]") |
| 28 | except (SyntaxError, ValueError): |
| 29 | return item |
| 30 | |
| 31 | |
| 32 | def parse_to_list(text: str) -> Optional[List[str]]: |
no outgoing calls
no test coverage detected