OOPPEENN commited on
Commit
09ebca4
1 Parent(s): 8c28fc8

Upload .gen_readme.py

Browse files
Files changed (1) hide show
  1. .gen_readme.py +65 -0
.gen_readme.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import difflib
3
+ import requests
4
+ from huggingface_hub import HfApi
5
+
6
+ def get_readme(repo_id, file_path):
7
+ url = f"https://huggingface.co/datasets/{repo_id}/resolve/main/{file_path}"
8
+ response = requests.get(url)
9
+ response.raise_for_status()
10
+ return response.text
11
+
12
+ def get_chs(readme_content):
13
+ pattern = re.compile(r"\| (.*?) \| (.*?) \| (.*?) \|")
14
+ matches = pattern.findall(readme_content)
15
+ chs_dict = {f"{company}_{eng}": chs for company, eng, chs in matches if chs}
16
+ return chs_dict
17
+
18
+ def get_repo(repo_id, repo_type='dataset'):
19
+ api = HfApi()
20
+ repo_info = api.repo_info(repo_id=repo_id, repo_type=repo_type)
21
+ files = [file.rfilename for file in repo_info.siblings if file.rfilename not in ['.gitattributes', 'README.md']]
22
+ return files
23
+
24
+ def escape_markdown(text):
25
+ escape_chars = "~#"
26
+ return ''.join(f"\\{char}" if char in escape_chars else char for char in text)
27
+
28
+ def gen_markdown(files, chs_dict):
29
+ table_rows = []
30
+ for file in files:
31
+ parts = file.replace('.7z', '').split('_')
32
+ company, eng = parts
33
+ chs = chs_dict.get(f"{company}_{escape_markdown(eng)}", "")
34
+ row = f"| {company} | {escape_markdown(eng)} | {escape_markdown(chs)} |"
35
+ table_rows.append(row)
36
+
37
+ markdown = """| 会社 | ENG | CHS |
38
+ | :----- | :----- | :----- |
39
+ """
40
+ markdown += "\n".join(table_rows)
41
+ return markdown
42
+
43
+ def gen_diff(original, modified):
44
+ original_lines = original.splitlines(keepends=True)
45
+ modified_lines = modified.splitlines(keepends=True)
46
+ diff = difflib.unified_diff(original_lines, modified_lines, fromfile='original', tofile='modified')
47
+ return ''.join(diff)
48
+
49
+ if __name__ == '__main__':
50
+ repo_id = 'OOPPEENN/Galgame_Dataset'
51
+ readme_content = get_readme(repo_id, 'README.md')
52
+
53
+ # 获取数据列表部分
54
+ data_list_start = readme_content.find("| 会社 | ENG | CHS |")
55
+ readme_content_data_list = readme_content[data_list_start:]
56
+
57
+ chs_dict = get_chs(readme_content_data_list)
58
+ file_list = get_repo(repo_id, repo_type='dataset')
59
+ markdown_table = gen_markdown(file_list, chs_dict)
60
+
61
+ with open(r"C:\Users\bfloat16\Desktop\output.md", 'w', encoding='utf-8') as file:
62
+ file.write(markdown_table)
63
+
64
+ diff = gen_diff(readme_content_data_list, markdown_table)
65
+ print(diff)