| |
| """Parser for avesta.org Avestan dictionary page. |
| |
| Extracts Avestan words with their transliterations and English glosses |
| from the online dictionary at https://www.avesta.org/avdict/avdict.htm |
| |
| The dictionary page is a large HTML file with entries formatted as: |
| <b>word</b> (grammar info) gloss text |
| |
| Uses only stdlib (urllib, html.parser, re). |
| """ |
|
|
| from __future__ import annotations |
|
|
| import logging |
| import re |
| import urllib.request |
| import urllib.error |
| from html.parser import HTMLParser |
| from typing import Any |
|
|
| logger = logging.getLogger(__name__) |
|
|
|
|
| class AvestaParser(HTMLParser): |
| """Parse the avesta.org dictionary HTML to extract entries. |
| |
| The dictionary uses a fairly simple format: |
| - Bold (<b>) tags mark headwords |
| - Following text contains grammatical info and gloss |
| - Entries are separated by <br> or <p> tags |
| """ |
|
|
| def __init__(self) -> None: |
| super().__init__() |
| self.in_bold = False |
| self.bold_text = "" |
| self.after_bold_text = "" |
| self.collecting_gloss = False |
| self.entries: list[dict] = [] |
|
|
| def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: |
| if tag in ("b", "strong"): |
| |
| self._flush_entry() |
| self.in_bold = True |
| self.bold_text = "" |
| elif tag in ("br", "p", "hr") and self.collecting_gloss: |
| self._flush_entry() |
|
|
| def handle_endtag(self, tag: str) -> None: |
| if tag in ("b", "strong") and self.in_bold: |
| self.in_bold = False |
| self.collecting_gloss = True |
| self.after_bold_text = "" |
|
|
| def handle_data(self, data: str) -> None: |
| if self.in_bold: |
| self.bold_text += data |
| elif self.collecting_gloss: |
| self.after_bold_text += data |
|
|
| def _flush_entry(self) -> None: |
| """Process the accumulated bold text + gloss text into an entry.""" |
| if not self.bold_text.strip(): |
| self.collecting_gloss = False |
| return |
|
|
| word = self.bold_text.strip() |
| gloss_raw = self.after_bold_text.strip() |
|
|
| |
| word = re.sub(r"[.,;:]+$", "", word).strip() |
| if not word or len(word) > 80: |
| self.collecting_gloss = False |
| return |
|
|
| |
| if word.isupper() and len(word) > 3: |
| self.collecting_gloss = False |
| return |
|
|
| |
| |
| gloss = gloss_raw |
| |
| gloss = re.sub(r"^\s*\([^)]{0,30}\)\s*", "", gloss) |
| |
| gloss = re.sub(r"^[-–—\s]+", "", gloss) |
| |
| gloss = re.split(r"[.;]", gloss)[0].strip() |
| |
| gloss = re.sub(r"\([^)]*\)", "", gloss).strip() |
|
|
| if gloss and len(gloss) < 200: |
| self.entries.append({ |
| "word": word, |
| "transliteration": word, |
| "gloss": gloss, |
| }) |
|
|
| self.collecting_gloss = False |
|
|
|
|
| def _fallback_regex_parse(html: str) -> list[dict]: |
| """Fallback regex-based parsing for the Avestan dictionary.""" |
| entries: list[dict] = [] |
|
|
| |
| pattern = re.compile( |
| r"<b>([^<]{1,60})</b>" |
| r"\s*(?:\([^)]{0,30}\))?\s*" |
| r"([\w][\w\s,'-]{3,120}?)(?=[.<]|\n\n)", |
| re.IGNORECASE, |
| ) |
|
|
| for m in pattern.finditer(html): |
| word = m.group(1).strip() |
| gloss = m.group(2).strip() |
| |
| gloss = re.sub(r"[,;:\s]+$", "", gloss) |
| if word and gloss and not word.isupper(): |
| entries.append({ |
| "word": word, |
| "transliteration": word, |
| "gloss": gloss, |
| }) |
|
|
| return entries |
|
|
|
|
| def parse(url: str, **kwargs: Any) -> list[dict]: |
| """Download and parse the avesta.org dictionary page. |
| |
| Args: |
| url: URL to the Avestan dictionary, typically: |
| https://www.avesta.org/avdict/avdict.htm |
| |
| Returns: |
| List of dicts with keys: word, transliteration, gloss. |
| Returns empty list if URL is unreachable. |
| """ |
| logger.info("Avesta: downloading %s", url) |
| try: |
| req = urllib.request.Request(url, headers={"User-Agent": "PhaiPhon/1.0"}) |
| with urllib.request.urlopen(req, timeout=30) as resp: |
| html = resp.read().decode("utf-8", errors="replace") |
| except (urllib.error.URLError, urllib.error.HTTPError, OSError) as exc: |
| logger.warning("Avesta: failed to download %s: %s", url, exc) |
| return [] |
|
|
| |
| parser = AvestaParser() |
| parser.feed(html) |
| parser._flush_entry() |
| entries = parser.entries |
|
|
| |
| if not entries: |
| logger.info("Avesta: structured parsing found 0 entries, trying regex fallback") |
| entries = _fallback_regex_parse(html) |
|
|
| |
| seen: set[str] = set() |
| unique: list[dict] = [] |
| for e in entries: |
| if e["word"] not in seen: |
| seen.add(e["word"]) |
| unique.append(e) |
|
|
| logger.info("Avesta: extracted %d entries from %s", len(unique), url) |
| return unique |
|
|
|
|
| if __name__ == "__main__": |
| import sys |
| logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
|
|
| test_url = ( |
| sys.argv[1] if len(sys.argv) > 1 |
| else "https://www.avesta.org/avdict/avdict.htm" |
| ) |
| results = parse(test_url) |
| print(f"\nExtracted {len(results)} entries:") |
| for entry in results[:15]: |
| print(f" {entry['word']:30s} {entry['gloss']}") |
| if len(results) > 15: |
| print(f" ... and {len(results) - 15} more") |
|
|