|
import re
|
|
import difflib
|
|
import requests
|
|
from huggingface_hub import HfApi
|
|
|
|
def get_readme(repo_id, file_path):
|
|
url = f"https://huggingface.co/datasets/{repo_id}/resolve/main/{file_path}"
|
|
response = requests.get(url)
|
|
response.raise_for_status()
|
|
return response.text
|
|
|
|
def get_chs(readme_content):
|
|
pattern = re.compile(r"\| (.*?) \| (.*?) \| (.*?) \|")
|
|
matches = pattern.findall(readme_content)
|
|
chs_dict = {f"{company}_{eng}": chs for company, eng, chs in matches if chs}
|
|
return chs_dict
|
|
|
|
def get_repo(repo_id, repo_type='dataset'):
|
|
api = HfApi()
|
|
repo_info = api.repo_info(repo_id=repo_id, repo_type=repo_type)
|
|
files = [file.rfilename for file in repo_info.siblings if file.rfilename not in ['.gitattributes', 'README.md']]
|
|
return files
|
|
|
|
def escape_markdown(text):
|
|
escape_chars = "~#"
|
|
return ''.join(f"\\{char}" if char in escape_chars else char for char in text)
|
|
|
|
def gen_markdown(files, chs_dict):
|
|
table_rows = []
|
|
for file in files:
|
|
parts = file.replace('.7z', '').split('_')
|
|
company, eng = parts
|
|
chs = chs_dict.get(f"{company}_{escape_markdown(eng)}", "")
|
|
row = f"| {company} | {escape_markdown(eng)} | {escape_markdown(chs)} |"
|
|
table_rows.append(row)
|
|
|
|
markdown = """| 会社 | ENG | CHS |
|
|
| :----- | :----- | :----- |
|
|
"""
|
|
markdown += "\n".join(table_rows)
|
|
return markdown
|
|
|
|
def gen_diff(original, modified):
|
|
original_lines = original.splitlines(keepends=True)
|
|
modified_lines = modified.splitlines(keepends=True)
|
|
diff = difflib.unified_diff(original_lines, modified_lines, fromfile='original', tofile='modified')
|
|
return ''.join(diff)
|
|
|
|
if __name__ == '__main__':
|
|
repo_id = 'OOPPEENN/Galgame_Dataset'
|
|
readme_content = get_readme(repo_id, 'README.md')
|
|
|
|
|
|
data_list_start = readme_content.find("| 会社 | ENG | CHS |")
|
|
readme_content_data_list = readme_content[data_list_start:]
|
|
|
|
chs_dict = get_chs(readme_content_data_list)
|
|
file_list = get_repo(repo_id, repo_type='dataset')
|
|
markdown_table = gen_markdown(file_list, chs_dict)
|
|
|
|
with open(r"C:\Users\bfloat16\Desktop\output.md", 'w', encoding='utf-8') as file:
|
|
file.write(markdown_table)
|
|
|
|
diff = gen_diff(readme_content_data_list, markdown_table)
|
|
print(diff) |