Spaces:
Runtime error
Runtime error
update from https://github.com/ArneBinder/argumentation-structure-identification/pull/529
d868d2e verified Download src/analysis/get_json_field_as_string.py from ArneBinder/ScientificArgumentRecommender: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/ArneBinder/ScientificArgumentRecommender/resolve/main/src/analysis/get_json_field_as_string.py
- Command line
-
hf download hf://spaces/ArneBinder/ScientificArgumentRecommender/src/analysis/get_json_field_as_string.py
-
curl -L -o get_json_field_as_string.py https://huggingface.co/spaces/ArneBinder/ScientificArgumentRecommender/resolve/main/src/analysis/get_json_field_as_string.py
1.41 kB
| import json | |
| def main( | |
| paths: list[str], | |
| field: list[str], | |
| format: str = "plain", | |
| ) -> None: | |
| result = [] | |
| for path in paths: | |
| with open(path, "r") as f: | |
| data = json.load(f) | |
| value = data | |
| for key in field: | |
| value = value.get(key) | |
| if not isinstance(value, list): | |
| value = [value] | |
| result.extend(value) | |
| if format == "plain": | |
| print(",".join(map(str, result))) | |
| elif format == "python": | |
| result_str = str(result) | |
| print(result_str.replace(" ", "")) | |
| else: | |
| raise ValueError(f"Unknown format: {format}") | |
| if __name__ == "__main__": | |
| import argparse | |
| parser = argparse.ArgumentParser( | |
| description="Get a field from one or more JSON files and print to stdout." | |
| ) | |
| parser.add_argument( | |
| "paths", | |
| type=lambda x: x.split(","), | |
| help="Comma-separated list of paths to the JSON files to process.", | |
| ) | |
| parser.add_argument( | |
| "--field", | |
| type=str, | |
| required=True, | |
| nargs="+", | |
| help="Field to extract from the JSON files. Can be a nested field by providing multiple entries.", | |
| ) | |
| parser.add_argument( | |
| "--format", | |
| type=str, | |
| default="plain", | |
| choices=["plain", "python"], | |
| ) | |
| args = parser.parse_args() | |
| kwargs = vars(args) | |
| main(**kwargs) | |