Last active
May 31, 2023 05:11
-
-
Save ilius/8327ae548222eb7285b83dc16ab07cde to your computer and use it in GitHub Desktop.
VahidN/EnglishToPersianDictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
add_prefix = True | |
add_reverse = True | |
import os | |
import sys | |
from os.path import join | |
from json import load | |
inputDirPath = sys.argv[1] | |
outputPath = sys.argv[2] | |
outputFile = open(outputPath, mode="wt", encoding="utf-8") | |
for jsonName in sorted(os.listdir(inputDirPath)): | |
if not jsonName.endswith(".json"): | |
continue | |
jsonPath = join(inputDirPath, jsonName) | |
print("Reading", jsonPath) | |
with open(jsonPath, mode="rb") as _file: | |
data = load(_file) | |
for entry in data["Words"]: | |
term = entry["EnglishWord"] | |
defiParts = [] | |
for meaning in entry["Meanings"]: | |
meaning = meaning.replace("\t", "\\t") | |
meaning = meaning.replace("\n", "\\n") | |
defiParts.append(meaning) | |
defi = "<br><br>".join(defiParts) | |
outputFile.write(term + "\t" + defi + "\n") | |
if add_prefix: | |
words = term.split(" ")[1:] | |
for i in range(len(words)): | |
p_term = " ".join(words[i:]) + ": " + term | |
outputFile.write(p_term + "\t" + defi + "\n") | |
if add_reverse: | |
for meaning in entry["Meanings"]: | |
meaning = meaning.replace("\t", "\\t") | |
meaning = meaning.replace("\n", "\\n") | |
outputFile.write(meaning + "\t" + term + "\n") | |
outputFile.close() | |
print("Finished writing", outputPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment