-
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.
Use hanzipy instead of a javascript library
This removes the javascript dependency. All python now
- Loading branch information
Showing
4 changed files
with
371 additions
and
65 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import csv | ||
|
||
from hanzipy_decomposer import HanziDecomposer | ||
|
||
|
||
def add_examples_to_source(): | ||
inst = HanziDecomposer() | ||
# Read radicals from a CSV file | ||
with open("./data/source.csv", mode="r", encoding="UTF-8") as file: | ||
reader = csv.reader(file, delimiter="\t") | ||
data = [row for row in reader] | ||
|
||
# Process each radical | ||
radical_characters = [] | ||
for row in data: | ||
try: | ||
characters = inst.get_characters_with_component(row[0]) | ||
# Limit to first 5 characters | ||
if characters is not None: | ||
characters = characters[:5] if len(characters) > 5 else characters | ||
radical_characters.append(", ".join(characters)) | ||
else: | ||
raise KeyError | ||
except KeyError: | ||
radical_characters.append("") | ||
|
||
# Write the updated data to a new CSV file | ||
with open( | ||
"./data/source_examples.csv", mode="w", encoding="UTF-8", newline="" | ||
) as file: | ||
combined_data = [ | ||
row + [rad_chars] for row, rad_chars in zip(data, radical_characters) | ||
] | ||
writer = csv.writer( | ||
file, delimiter="\t", quotechar="|", quoting=csv.QUOTE_MINIMAL | ||
) | ||
writer.writerows(combined_data) | ||
|
||
print("Finished!") | ||
|
||
|
||
if __name__ == "__main__": | ||
add_examples_to_source() |
Oops, something went wrong.