-
Notifications
You must be signed in to change notification settings - Fork 63
Script for Bavli Chapter Renames #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c87a095
c8e4745
52d683d
7cf716c
65ca574
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import django | ||
| import csv | ||
| import time | ||
|
|
||
| django.setup() | ||
|
|
||
| from sefaria.model import * | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8 recommends not to import everything from a package (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, except for See here |
||
|
|
||
|
|
||
| def retrieve_new_chapter_names(): | ||
| data = {} | ||
| with open("perek_names - final.csv", newline="", encoding="utf-8") as csvfile: | ||
| reader = csv.DictReader(csvfile) | ||
| for row in reader: | ||
| key = f"{row['Masekhet']} {row['#']}" | ||
| data[key] = row['English'] | ||
| return data | ||
|
|
||
| def retrieve_babylonian_talmud_masechtot(): | ||
| return library.get_indexes_in_corpus('Bavli') | ||
|
|
||
| def name_changer(masechet_index, new_names_data): | ||
saengel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| This function iterates through the alt structure nodes for a given Masechet and renames | ||
| the title, then saves the Index. | ||
| :param masechet_index: The Index object for the given Masechet | ||
| :param new_names_data: The data dict for the new names, where the key is the masechet name and chapter number | ||
| and the value is the updated name for that chapter. | ||
| :return: None | ||
| """ | ||
| chap_num = 0 | ||
| for node in masechet_index.alt_structs['Chapters']['nodes']: | ||
| chap_num += 1 | ||
| new_title = new_names_data[f"{masechet} {chap_num}"] | ||
| print(f">> Updating {masechet} {chap_num} to {new_title}") | ||
| chapter_title_list = node["titles"] | ||
| for title in chapter_title_list: | ||
| if title['lang'] == 'en': | ||
| title['text'] = new_title | ||
| masechet_index.save() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| start = time.time() | ||
|
|
||
| # Ingest CSV | ||
| new_names_data = retrieve_new_chapter_names() | ||
|
|
||
| # Retrieve all masechtot | ||
| masechtot = retrieve_babylonian_talmud_masechtot() | ||
|
|
||
| # Run the name-changer on a masechet-by-masechet basis | ||
| for masechet in masechtot: | ||
| masechet_index = Ref(masechet).index | ||
| print(masechet_index) | ||
| name_changer(masechet_index, new_names_data) | ||
|
|
||
| end = time.time() | ||
|
|
||
| print(f"Total run time {end - start}") | ||
|
|
||
| # In this case, the Hebrew title name for Bava Kamma 2 also needed to be updated correcting a typo. | ||
| bk_index = Ref("Bava Kamma 2").index | ||
| bk_index.alt_structs["Chapters"]["nodes"][1]["titles"][1]["text"] = "כיצד הרגל" | ||
| bk_index.save() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you purposely put this import below the
django.setup()? PEP8 recommends having all the imports at the topThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if I recall correctly,
django.setup()is necessary before importing the model, although it lends itself to a clunky import section at the top of the file.