heziiiii commited on
Commit
62f95c5
·
verified ·
1 Parent(s): 353cfe0

Upload tag_char_info.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tag_char_info.py +95 -0
tag_char_info.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import json
3
+ from tqdm import tqdm
4
+ from concurrent.futures import ProcessPoolExecutor
5
+ from collections import Counter
6
+
7
+ if __name__ == "__main__":
8
+ # 文件路径
9
+ danbooru_parquets_path = "/mnt/data/Booru-parquets/danbooru.parquet"
10
+ danbooru_parquets_path_add = "/mnt/data/danbooru_newest-all/table.parquet"
11
+
12
+ # 读取 Parquet 文件
13
+ df1 = pd.read_parquet(danbooru_parquets_path)
14
+ df2 = pd.read_parquet(danbooru_parquets_path_add)
15
+ df = pd.concat([df1, df2], ignore_index=True)
16
+ print(df.columns)
17
+ print(df.head())
18
+
19
+ # 获取所有 tag_string_character的list
20
+ tag_string_character = df['tag_string_character'].unique()
21
+
22
+ CHARACTER_TAG_LIST = []
23
+ for tag_list in tqdm(tag_string_character):
24
+ if tag_list is None:
25
+ continue # 跳过 None 值
26
+ for tag in tag_list.split(" "):
27
+ CHARACTER_TAG_LIST.append(tag)
28
+ CHARACTER_TAG_LIST = list(set(CHARACTER_TAG_LIST))
29
+ CHARACTER_TAG_LIST.remove("")
30
+ print(len(CHARACTER_TAG_LIST))
31
+
32
+ def map_function(tag):
33
+ """映射函数:获取每个角色的 ID 和相关文本的词频"""
34
+ # 使用布尔���引来选择包含特定 tag 的行
35
+ tag_data = df[df['tag_string_character'].str.contains(tag, na=False, regex=False)]
36
+
37
+ tag_id = tag_data['id'].unique()
38
+ word_counts = Counter()
39
+
40
+ # 假设有一列包含文本,如 'description',替换为实际列名
41
+ text_column = 'tag_string_general' # 替换为实际列名
42
+ for text in tag_data[text_column]:
43
+ if isinstance(text, str): # 确保文本不是 None
44
+ words = text.split(" ") # 根据需要进行分词
45
+ word_counts.update(words)
46
+
47
+ return tag, tag_id, word_counts
48
+
49
+ def reduce_function(results):
50
+ """归约函数:合并所有结果"""
51
+ tag_string_character_id = {}
52
+
53
+
54
+ for tag, ids, word_counts in results:
55
+ tag_string_character_id[tag] = {}
56
+ tag_string_character_id[tag]["ids"] = ids.tolist() # 将 NumPy 数组转换为列表
57
+ tag_string_character_id[tag]["word_counts"] = dict(word_counts) # 将词频转换为字典
58
+
59
+ return tag_string_character_id
60
+
61
+ with open("character_tag_list.txt", "w") as f:
62
+ f.write("\n".join(CHARACTER_TAG_LIST))
63
+ # CHARACTER_TAG_LIST = CHARACTER_TAG_LIST[:60]
64
+ # print(CHARACTER_TAG_LIST)
65
+ # 使用多进程并行处理
66
+ with ProcessPoolExecutor(max_workers=32) as executor:
67
+ # 映射阶段
68
+ mapped_results = list(tqdm(executor.map(map_function, CHARACTER_TAG_LIST), total=len(CHARACTER_TAG_LIST)))
69
+
70
+ # 归约阶段
71
+ tag_string_character_id = reduce_function(mapped_results)
72
+
73
+ # 展示前五个艺术家的 ID 和词频
74
+ # print(list(tag_string_character_id.keys())[:5])
75
+ # print(list(tag_string_character_id.values())[:5])
76
+
77
+
78
+ # 保存 ID,词频 为 JSON
79
+ with open("character_id_with_word_counts.json", "w") as f:
80
+ json.dump(tag_string_character_id, f)
81
+
82
+
83
+
84
+ # 保存艺术家 ID 和词频为 Parquet
85
+ character_data = []
86
+ for tag, data in tag_string_character_id.items():
87
+ character_data.append({
88
+ 'tag': tag,
89
+ 'count':len(data["ids"]),
90
+ 'id': data["ids"],
91
+ 'word_counts': data["word_counts"]
92
+ })
93
+
94
+ artist_df = pd.DataFrame(character_data)
95
+ artist_df.to_parquet("char_id_with_word_counts.parquet")