import json # Open the file and read line by line with open('/home/yiyangai/stephenqs/datasets/wikihow/WikihowText_data.json', 'r') as file: unique_first_level_categories = set() for line in file: try: # Attempt to parse each line as a JSON object item = json.loads(line.strip()) if item['category_hierarchy'] != []: first_category = item['category_hierarchy'][0] unique_first_level_categories.add(first_category) except json.JSONDecodeError as e: print(f"Skipping line due to JSON error: {e}") # Save the unique first-level categories to a text file with open('unique_categories.txt', 'w') as output_file: for category in sorted(unique_first_level_categories): output_file.write(f"{category}\n") print(f"Unique first-level categories have been saved to 'unique_categories.txt'") #import json # ## Open the file and read lines #with open('/home/yiyangai/stephenqs/datasets/wikihow/WikihowText_data.json', 'r') as file: # data = [] # for line in file: # data.append(json.loads(line)) # ## Now 'data' is a list of dictionaries #unique_categories = set() #for item in data: # hierarchy = tuple(item['category_hierarchy']) # unique_categories.add(hierarchy) # if len(unique_categories) >= 100: # break # #for hierarchy in unique_categories: # print(" > ".join(hierarchy))