File size: 1,065 Bytes
4c895ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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'] != []:
                if len(item['category_hierarchy'])>=2:
                       if item['category_hierarchy'][0]=='Finance and Business':
                            first_category = item['category_hierarchy'][1]
                            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_2.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'")