-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
103 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import csv, re, os | ||
import csv | ||
import os | ||
import re | ||
from shutil import copyfile | ||
|
||
|
||
path_syllabs = "./syllabs/" | ||
path_syllabs = "./syllabs/" | ||
syllabs_list = os.listdir(path_syllabs) | ||
|
||
with open('./data/source.csv', 'r', newline='', encoding="UTF8") as csvfile: | ||
with open("./data/source.csv", "r", newline="", encoding="UTF8") as csvfile: | ||
reader = csv.reader(csvfile, delimiter="\t") | ||
for line in reader: | ||
for s in syllabs_list: | ||
if line[2] in s: | ||
copyfile(path_syllabs+s, './media/audio/'+s) | ||
copyfile(path_syllabs + s, "./media/audio/" + s) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import csv | ||
import urllib.request | ||
import os | ||
import urllib.request | ||
|
||
|
||
def downloadAncientImg(): | ||
with open("./data/ancient_img_url.csv", "r", encoding="UTF8") as csvfile: | ||
reader = csv.reader(csvfile, delimiter='\t') | ||
reader = csv.reader(csvfile, delimiter="\t") | ||
img_list = os.listdir("./media") | ||
downloaded_img = [i[0] for i in img_list] | ||
for row in reader: | ||
if row[0] not in downloaded_img: | ||
try: | ||
urllib.request.urlretrieve(row[1], f"./media/{row[0]}_img_ancient.svg") | ||
urllib.request.urlretrieve( | ||
row[1], f"./media/{row[0]}_img_ancient.svg" | ||
) | ||
except: | ||
print(f"can't load IMG {row[0]}") | ||
|
||
|
||
if __name__ == "__main__": | ||
downloadAncientImg() | ||
downloadAncientImg() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,96 @@ | ||
import json, re, csv, urllib, os | ||
import csv | ||
import json | ||
import os | ||
import re | ||
import urllib | ||
import urllib.request | ||
from sys import platform | ||
|
||
if platform == "darwin": | ||
import certifi | ||
|
||
import pinyin | ||
|
||
def getRadicalsData(): | ||
|
||
def getRadicalsData(): | ||
fields = ["string", "altMandarin", "altDefinition"] | ||
url_radicals = f"http://ccdb.hemiola.com/characters/radicals?fields={','.join(fields)}" | ||
url_radicals = ( | ||
f"http://ccdb.hemiola.com/characters/radicals?fields={','.join(fields)}" | ||
) | ||
|
||
if platform == "darwin": | ||
json_file_radicals = urllib.request.urlopen(url_radicals, cafile=certifi.where()).read() | ||
json_file_radicals = urllib.request.urlopen( | ||
url_radicals, cafile=certifi.where() | ||
).read() | ||
else: | ||
json_file_radicals = urllib.request.urlopen(url_radicals).read() | ||
|
||
radicals = json.loads(json_file_radicals) | ||
entries = [] | ||
|
||
for i in range(1,215): | ||
for i in range(1, 215): | ||
same_radicals = list(filter(lambda r: int(r["radical"]) == i, radicals)) | ||
entry = same_radicals.pop(0) | ||
entry["alternativs"] = ", ".join([r["string"] for r in same_radicals]) | ||
tone_number = re.search(r"\d", entry["altMandarin"]).group(0) | ||
entry["MandarinStyled"] = f"<div class=tone{tone_number}>{pinyin.get(entry['string'])}</div>" | ||
entry[ | ||
"MandarinStyled" | ||
] = f"<div class=tone{tone_number}>{pinyin.get(entry['string'])}</div>" | ||
entries.append(entry) | ||
|
||
|
||
char_with_img = [c[0] for c in os.listdir("./media/img")] | ||
|
||
with open('./data/source.csv', 'w', newline='', encoding="UTF8") as csvfile: | ||
writer = csv.writer(csvfile, delimiter='\t', | ||
quotechar='|', quoting=csv.QUOTE_MINIMAL) | ||
with open("./data/source.csv", "w", newline="", encoding="UTF8") as csvfile: | ||
writer = csv.writer( | ||
csvfile, delimiter="\t", quotechar="|", quoting=csv.QUOTE_MINIMAL | ||
) | ||
for d in entries: | ||
sound = f'[sound:cmn-{d["altMandarin"]}.mp3]' | ||
if d['string'] in char_with_img: | ||
if d["string"] in char_with_img: | ||
ancient_img = f"<img src=\"{d['string']}_img_ancient.svg\">" | ||
else: | ||
ancient_img = "" | ||
writer.writerow([d["string"], d["MandarinStyled"], d["altMandarin"], d["altDefinition"], d["radical"], d["alternativs"], sound, ancient_img]) | ||
writer.writerow( | ||
[ | ||
d["string"], | ||
d["MandarinStyled"], | ||
d["altMandarin"], | ||
d["altDefinition"], | ||
d["radical"], | ||
d["alternativs"], | ||
sound, | ||
ancient_img, | ||
] | ||
) | ||
|
||
|
||
def overwriteRadicalData(): | ||
with open('./data/correction.csv', 'r', newline='', encoding="UTF8") as csvfile: | ||
reader = csv.reader(csvfile, delimiter='\t', | ||
quotechar='|', quoting=csv.QUOTE_MINIMAL) | ||
with open("./data/correction.csv", "r", newline="", encoding="UTF8") as csvfile: | ||
reader = csv.reader( | ||
csvfile, delimiter="\t", quotechar="|", quoting=csv.QUOTE_MINIMAL | ||
) | ||
filter_list = list(reader) | ||
with open('./data/source.csv', 'r', newline='', encoding="UTF8") as csvfile: | ||
reader = csv.reader(csvfile, delimiter='\t', | ||
quotechar='|', quoting=csv.QUOTE_MINIMAL) | ||
with open("./data/source.csv", "r", newline="", encoding="UTF8") as csvfile: | ||
reader = csv.reader( | ||
csvfile, delimiter="\t", quotechar="|", quoting=csv.QUOTE_MINIMAL | ||
) | ||
radical_list = list(reader) | ||
for f in filter_list: | ||
item = next(filter(lambda x: x[0]==f[0], radical_list)) | ||
item = next(filter(lambda x: x[0] == f[0], radical_list)) | ||
if len(f) != 9: | ||
print("error!!!") | ||
continue | ||
radical_index = int(item[4])-1 | ||
radical_index = int(item[4]) - 1 | ||
for i in range(8): | ||
if f[i] != '': | ||
if f[i] != "": | ||
radical_list[radical_index][i] = f[i] | ||
with open('./data/source.csv', 'w', newline='', encoding="UTF8") as csvfile: | ||
writer = csv.writer(csvfile, delimiter='\t', | ||
quotechar='|', quoting=csv.QUOTE_MINIMAL) | ||
with open("./data/source.csv", "w", newline="", encoding="UTF8") as csvfile: | ||
writer = csv.writer( | ||
csvfile, delimiter="\t", quotechar="|", quoting=csv.QUOTE_MINIMAL | ||
) | ||
writer.writerows(radical_list) | ||
|
||
|
||
if __name__ == "__main__": | ||
getRadicalsData() | ||
overwriteRadicalData() | ||
overwriteRadicalData() |
Oops, something went wrong.