Cristobal299 commited on
Commit
c522459
·
1 Parent(s): abf5b37

Agrega script 00: completar diccionario con HebrewStrong.xml

Browse files
Files changed (1) hide show
  1. 00_completar_diccionario.py +114 -0
00_completar_diccionario.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ 00_completar_diccionario.py
4
+ Descarga el lexico completo de Strong hebreo (HebrewStrong.xml, dominio
5
+ publico, proyecto Open Scriptures) y completa dictionary_hebrew_spanish.json
6
+ con las entradas que falten (los ~700 H#### que tu diccionario actual no
7
+ tiene). NO pisa nada que ya tengas: si una clave ya existe (con o sin
8
+ "translation_es"), se deja intacta.
9
+
10
+ Requisitos:
11
+ pip install requests
12
+
13
+ Uso (en la carpeta pdf-reader/, junto a dictionary_hebrew_spanish.json):
14
+ python 00_completar_diccionario.py
15
+
16
+ Corre ANTES de 01_traducir_diccionario.py.
17
+ """
18
+ import json
19
+ import re
20
+ import xml.etree.ElementTree as ET
21
+ from pathlib import Path
22
+
23
+ import requests
24
+
25
+ BASE_DIR = Path(__file__).parent
26
+ DICT_PATH = BASE_DIR / "dictionary_hebrew_spanish.json"
27
+
28
+ URL = "https://raw.githubusercontent.com/openscriptures/HebrewLexicon/master/HebrewStrong.xml"
29
+
30
+
31
+ def descargar_xml():
32
+ print("Descargando HebrewStrong.xml...")
33
+ r = requests.get(URL, timeout=60)
34
+ r.raise_for_status()
35
+ return r.text
36
+
37
+
38
+ def texto_de(el):
39
+ if el is None:
40
+ return ""
41
+ return "".join(el.itertext()).strip()
42
+
43
+
44
+ def parsear(xml_text):
45
+ # El id puede venir como "H1" o (en variantes) sin la H; normalizamos.
46
+ root = ET.fromstring(xml_text)
47
+ entradas = {}
48
+ for entry in root.findall("entry"):
49
+ eid = entry.get("id") or ""
50
+ eid = eid.strip()
51
+ if not eid:
52
+ continue
53
+ if not eid.startswith("H"):
54
+ eid = "H" + eid
55
+
56
+ w = entry.find("w")
57
+ lemma = (w.text or "").strip() if w is not None else ""
58
+ xlit = w.get("xlit", "") if w is not None else ""
59
+
60
+ meaning_el = entry.find("meaning")
61
+ usage_el = entry.find("usage")
62
+ source_el = entry.find("source")
63
+
64
+ meaning = texto_de(meaning_el)
65
+ usage = texto_de(usage_el)
66
+ source = texto_de(source_el)
67
+
68
+ definition_en = meaning if meaning else usage
69
+
70
+ translation = ""
71
+ if meaning_el is not None:
72
+ primer_def = meaning_el.find("def")
73
+ if primer_def is not None and primer_def.text:
74
+ translation = primer_def.text.strip()
75
+ if not translation:
76
+ translation = re.split(r"[;,]", definition_en)[0].strip()
77
+ if not translation:
78
+ translation = usage.rstrip(".").strip()
79
+
80
+ entradas[eid] = {
81
+ "lemma": lemma,
82
+ "translation": translation,
83
+ "transliteration": xlit,
84
+ "root": "",
85
+ "definition_en": definition_en,
86
+ "note": source,
87
+ }
88
+ return entradas
89
+
90
+
91
+ def main():
92
+ with open(DICT_PATH, "r", encoding="utf-8") as f:
93
+ actual = json.load(f)
94
+
95
+ xml_text = descargar_xml()
96
+ completas = parsear(xml_text)
97
+
98
+ agregadas = 0
99
+ for k, v in completas.items():
100
+ if k not in actual:
101
+ actual[k] = v
102
+ agregadas += 1
103
+
104
+ tmp = DICT_PATH.with_suffix(".tmp")
105
+ with open(tmp, "w", encoding="utf-8") as f:
106
+ json.dump(actual, f, ensure_ascii=False, indent=2)
107
+ tmp.replace(DICT_PATH)
108
+
109
+ print(f"Entradas nuevas agregadas: {agregadas}")
110
+ print(f"Total de entradas en el diccionario ahora: {len(actual)}")
111
+
112
+
113
+ if __name__ == "__main__":
114
+ main()