File size: 1,730 Bytes
8850458 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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)
# write jsonl
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")
# write csv
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()
|