|
|
import json |
|
|
import csv |
|
|
from pathlib import Path |
|
|
|
|
|
SRC = Path("data/toeic_vocabulary.json") |
|
|
OUT_JSONL = Path("data/toeic_vocabulary_flat.jsonl") |
|
|
OUT_CSV = Path("data/toeic_vocabulary_flat.csv") |
|
|
|
|
|
def main(): |
|
|
data = json.loads(SRC.read_text(encoding="utf-8")) |
|
|
vocab_groups = data.get("vocabulary_by_importance", {}) |
|
|
|
|
|
rows = [] |
|
|
for importance_group, items in vocab_groups.items(): |
|
|
if not isinstance(items, list): |
|
|
continue |
|
|
for item in items: |
|
|
row = { |
|
|
"importance_group": importance_group, |
|
|
"english_word": item.get("english_word"), |
|
|
"chinese_definition": item.get("chinese_definition"), |
|
|
"star_rating": item.get("star_rating"), |
|
|
"category": item.get("category"), |
|
|
"parts_of_speech": "|".join(item.get("parts_of_speech", []) or []), |
|
|
"word_forms": json.dumps(item.get("word_forms", {}), ensure_ascii=False), |
|
|
"examples": json.dumps(item.get("examples", []), ensure_ascii=False), |
|
|
} |
|
|
rows.append(row) |
|
|
|
|
|
|
|
|
with OUT_JSONL.open("w", encoding="utf-8", newline="\n") as f: |
|
|
for r in rows: |
|
|
f.write(json.dumps(r, ensure_ascii=False) + "\n") |
|
|
|
|
|
|
|
|
cols = [ |
|
|
"importance_group", |
|
|
"english_word", |
|
|
"chinese_definition", |
|
|
"star_rating", |
|
|
"category", |
|
|
"parts_of_speech", |
|
|
"word_forms", |
|
|
"examples", |
|
|
] |
|
|
with OUT_CSV.open("w", encoding="utf-8", newline="") as f: |
|
|
writer = csv.DictWriter(f, fieldnames=cols) |
|
|
writer.writeheader() |
|
|
for r in rows: |
|
|
writer.writerow(r) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|
|
|
|