-
Notifications
You must be signed in to change notification settings - Fork 0
Add LXX Morphology downloader and README chat-question template #1
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
Open
editor77
wants to merge
1
commit into
main
Choose a base branch
from
codex/access-gopher-text-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,31 @@ | ||
| # LXX Morphology Downloader | ||
|
|
||
| This repository contains a small utility for downloading the LXX Morphology corpus from the UPenn gopher mirror. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| python scripts/download_lxxmorph.py --output-dir lxxmorph | ||
| ``` | ||
|
|
||
| By default, it pulls from: | ||
|
|
||
| ``` | ||
| https://ccat.sas.upenn.edu/gopher/text/religion/biblical/lxxmorph/ | ||
| ``` | ||
|
|
||
| The downloader defaults to fetching only `*.mlxx` files so the local corpus stays limited to the MLXX data. | ||
| You can override the base URL with `--base-url` or adjust the filename filter with `--include` if the mirror | ||
| changes or you need additional file types. | ||
|
|
||
| ## Chat question template | ||
|
|
||
| Use the template below when you want an analysis run based on the local `*.mlxx` files and the | ||
| `MT-LXX_Parallel_Manual (1).pdf` instructions: | ||
|
|
||
| ``` | ||
| Please analyze <BOOK CHAPTER:VERSE> using the local .mlxx files. Follow the MT-LXX_Parallel_Manual (1).pdf | ||
| step-by-step and return rows in this format: | ||
|
|
||
| Reference,Hebrew (MT),Greek (LXX),CATSS Notation & Analysis | ||
| ``` |
This file contains hidden or 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,126 @@ | ||
| #!/usr/bin/env python3 | ||
| """Download the LXX Morphology corpus directory from UPenn's gopher mirror.""" | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import html.parser | ||
| import os | ||
| import pathlib | ||
| import re | ||
| import urllib.parse | ||
| import urllib.request | ||
|
|
||
| DEFAULT_BASE_URL = "https://ccat.sas.upenn.edu/gopher/text/religion/biblical/lxxmorph/" | ||
|
|
||
|
|
||
| class LinkParser(html.parser.HTMLParser): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.links: list[str] = [] | ||
|
|
||
| def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: | ||
| if tag.lower() != "a": | ||
| return | ||
| for key, value in attrs: | ||
| if key.lower() == "href" and value: | ||
| self.links.append(value) | ||
|
|
||
|
|
||
| def fetch_bytes(url: str, user_agent: str | None = None) -> bytes: | ||
| request = urllib.request.Request(url) | ||
| if user_agent: | ||
| request.add_header("User-Agent", user_agent) | ||
| with urllib.request.urlopen(request) as response: | ||
| return response.read() | ||
|
|
||
|
|
||
| def parse_links(content: bytes, base_url: str) -> list[str]: | ||
| text = content.decode("utf-8", errors="replace") | ||
| if "<a" in text.lower(): | ||
| parser = LinkParser() | ||
| parser.feed(text) | ||
| return [urllib.parse.urljoin(base_url, link) for link in parser.links] | ||
|
|
||
| links: list[str] = [] | ||
| for line in text.splitlines(): | ||
| line = line.strip() | ||
| if not line or line.startswith("#"): | ||
| continue | ||
| if line.startswith("/"): | ||
| links.append(urllib.parse.urljoin(base_url, line)) | ||
| return links | ||
|
|
||
|
|
||
| def ensure_directory(path: pathlib.Path) -> None: | ||
| path.mkdir(parents=True, exist_ok=True) | ||
|
|
||
|
|
||
| def save_file(url: str, output_dir: pathlib.Path, allow_pattern: re.Pattern[str] | None, user_agent: str | None) -> None: | ||
| parsed = urllib.parse.urlparse(url) | ||
| filename = os.path.basename(parsed.path) | ||
| if not filename: | ||
| return | ||
| if allow_pattern and not allow_pattern.search(filename): | ||
| return | ||
| target_path = output_dir / filename | ||
| if target_path.exists(): | ||
| return | ||
| data = fetch_bytes(url, user_agent=user_agent) | ||
| target_path.write_bytes(data) | ||
|
|
||
|
|
||
| def download_recursive( | ||
| base_url: str, | ||
| output_dir: pathlib.Path, | ||
| visited: set[str], | ||
| allow_pattern: re.Pattern[str] | None, | ||
| user_agent: str | None, | ||
| ) -> None: | ||
| if base_url in visited: | ||
| return | ||
| visited.add(base_url) | ||
|
|
||
| content = fetch_bytes(base_url, user_agent=user_agent) | ||
| links = parse_links(content, base_url) | ||
| for link in links: | ||
| if link.endswith("/"): | ||
| subdir_name = os.path.basename(os.path.normpath(urllib.parse.urlparse(link).path)) | ||
| subdir_path = output_dir / subdir_name | ||
| ensure_directory(subdir_path) | ||
| download_recursive(link, subdir_path, visited, allow_pattern, user_agent) | ||
| else: | ||
| save_file(link, output_dir, allow_pattern, user_agent) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description="Download files from the LXX Morphology directory.") | ||
| parser.add_argument( | ||
| "--base-url", | ||
| default=DEFAULT_BASE_URL, | ||
| help="Base URL for the LXX Morphology directory listing.", | ||
| ) | ||
| parser.add_argument( | ||
| "--include", | ||
| default="\\.mlxx$", | ||
| help="Regex pattern of filenames to download (default: only .mlxx files).", | ||
| ) | ||
| parser.add_argument( | ||
| "--output-dir", | ||
| default="lxxmorph", | ||
| help="Directory to store downloaded files.", | ||
| ) | ||
| parser.add_argument( | ||
| "--user-agent", | ||
| default="Mozilla/5.0 (compatible; LXXMorphDownloader/1.0)", | ||
| help="User-Agent header to send with requests.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| output_dir = pathlib.Path(args.output_dir) | ||
| ensure_directory(output_dir) | ||
| allow_pattern = re.compile(args.include) if args.include else None | ||
| download_recursive(args.base_url, output_dir, set(), allow_pattern, args.user_agent) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the directory listing includes a parent link such as
../(common in Apache-style indexes),parse_linkswill return it and the recursion will follow it because there is no guard thatlinkstays under the originalbase_url. That means a run starting at.../lxxmorph/can walk up to.../biblical/and begin downloading unrelated directories (still matching*.mlxx), which is both unexpected and potentially huge. Consider rejecting links that are not prefixed by the initial base or explicitly skipping..links before recursing.Useful? React with 👍 / 👎.