Skip to content

Commit

Permalink
fix BoboTiG#1717: [ES] Better support for contracción template
Browse files Browse the repository at this point in the history
  • Loading branch information
lasconic committed Feb 7, 2023
1 parent d88a10b commit 15d3740
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 14 deletions.
15 changes: 2 additions & 13 deletions wikidict/lang/es/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"antropónimo masculino",
"apellido",
"definición imprecisa",
"definición impropia",
"{{infinitivo",
"{{enclítico",
"f.adj2",
Expand Down Expand Up @@ -137,12 +136,12 @@
"color": "color([p for p in parts if '=' not in p][1] if len(parts) > 1 else '#000000')",
# {{contexto|Educación}}
"contexto": "term(lookup_italic(parts[-1], 'es'))",
# {{contracción|de|ellas|leng=es}}
"contracción": "f\"{italic('Contracción de')} {parts[1]} {italic('y')} {parts[2]}\"",
# {{coord|04|39|N|74|03|O|type:country}}
"coord": "coord(parts[1:])",
# {{datación|xv}}
"datación": 'f"Atestiguado desde el siglo {parts[-1]}"',
# {{definición impropia|Utilizado para especificar...}}
"definición impropia": "italic(parts[1])",
# {{DRAE}}
"DRAE": 'f"«{word}», <i>Diccionario de la lengua española (2001)</i>, 22.ª ed., Madrid: Real Academia Española, Asociación de Academias de la Lengua Española y Espasa."', # noqa
"DRAE2001": 'f"«{word}», <i>Diccionario de la lengua española (2001)</i>, 22.ª ed., Madrid: Real Academia Española, Asociación de Academias de la Lengua Española y Espasa."', # noqa
Expand Down Expand Up @@ -264,9 +263,6 @@ def last_template_handler(
>>> last_template_handler(["default"], "es")
'##opendoublecurly##default##closedoublecurly##'
>>> last_template_handler(["variantes", "adestrador", "nota=poco frecuente"], "es")
'<b>Variante:</b> adestrador (poco frecuente)'
>>> last_template_handler(["en"], "es")
'Inglés'
Expand All @@ -279,7 +275,6 @@ def last_template_handler(
extract_keywords_from,
italic,
lookup_italic,
strong,
)
from ..defaults import last_template_handler as default
from .langs import langs
Expand Down Expand Up @@ -325,12 +320,6 @@ def last_template_handler(
phrase = italic(f'({concat(phrase_a, ", ")})')
return phrase

if tpl == "variantes":
phrase = f"{strong('Variante:')} {parts[0]}"
if data["nota"]:
phrase += f" ({data['nota']})"
return phrase

if lang := langs.get(template[0]):
return capitalize(lang)

Expand Down
93 changes: 92 additions & 1 deletion wikidict/lang/es/template_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,51 @@
extract_keywords_from,
italic,
small,
strong,
subscript,
)
from .langs import lang_to_normalize, langs

articulos: Dict[str, str] = {
"def.f.pl": "las",
"indef.f.pl": "unas",
"def.f.sg": "la",
"indef.f.sg": "una",
"def.m.pl": "los",
"indef.m.pl": "unos",
"def.m.sg": "el",
"def.m.sg.prep": "l",
"indef.m.sg": "un",
}

catgrams: Dict[str, Dict[str, str]] = {
"sustantivo": {
"sg": "sustantivo",
"gen": "m",
"adj_f_sg": "sustantiva",
},
"adjetivo": {
"sg": "adjetivo",
"gen": "m",
"adj_f_sg": "adjetiva",
},
}


def catgram(catgram: str, variation: str = "gen") -> str:
return catgrams[catgram][variation]


def inflect_articulo(
genero: str = "m", tipo: str = "def", numero: str = "sg", prep: bool = False
) -> str:
key = f"{tipo}.{genero}.{numero}"
if prep:
if key != "def.m.sg":
return " "
key += ".prep"
return articulos[key]


def normalizar_nombre(to_normalize: str) -> str:
"""
Expand Down Expand Up @@ -128,6 +169,26 @@ def render_comparativo(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
return phrase


def render_contraccion(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
"""
>>> render_contraccion("contracción", ["de", "ellas"], defaultdict(str, {"leng": "es"}))
'<i>Contracción de</i> de <i>y</i> ellas'
>>> render_contraccion("contracción", ["mi", "hija", "adjetivo", "sustantivo"], defaultdict(str, {"leng": "es"}))
'<i>Contracción del adjetivo</i> mi <i>y el sustantivo</i> hija'
"""
typo1 = data["typo1"] or parts[2] if len(parts) > 2 else ""
typo2 = data["typo2"] or parts[3] if len(parts) > 3 else ""
phrase = "Contracción de"
phrase += f"{inflect_articulo(catgram(typo1), prep=True)} {typo1}" if typo1 else ""
phrase = italic(phrase)
phrase += f" {parts[0]} "
phrase2 = "y"
phrase2 += f" {inflect_articulo(catgram(typo2))} {typo2}" if typo2 else ""
phrase += italic(phrase2)
phrase += f" {parts[1]}"
return phrase


def render_etim(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
"""
>>> render_etim("etim", ["la", "folia"], defaultdict(str))
Expand Down Expand Up @@ -583,7 +644,7 @@ def render_l(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
trans = data["tr"]
glosa = data["glosa-alt"] or data["glosa"]
num = data["núm"] or data["num"]
phrase = parts[2] if len(parts) > 2 else parts[-1]
phrase = data["3"] or data["2"] or (parts[2] if len(parts) > 2 else parts[-1])
if num:
phrase += subscript(num)
if tpl == "l+":
Expand Down Expand Up @@ -700,13 +761,42 @@ def render_variante(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
return f"{italic(capitalize(sentence))} " + render_l("l", [parts[0]], data)


def render_variantes(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
"""
>>> render_variantes("variantes", ["acrótera", "acroteria"], defaultdict(str))
'<b>Variantes:</b> acrótera, acroteria'
>>> render_variantes("variantes", ["moerus"], defaultdict(str, {"nota1":"arcaica", "alt1": "moerǔs"}))
'<b>Variante:</b> moerǔs (arcaica)'
>>> render_variantes("variantes", ["adestrador"], defaultdict(str, {"nota":"poco frecuente"}))
'<b>Variante:</b> adestrador (poco frecuente)'
"""
starter = "Variante" + ("s:" if len(parts) > 1 else ":")
a_phrase: List[str] = []
for i in range(0, 10):
if i == 0:
phrase = (
data["alt"] or data[f"alt{i+1}"] or (parts[i] if len(parts) > i else "")
)
else:
phrase = data[f"alt{i+1}"] or (parts[i] if len(parts) > i else "")
if i == 0 and data["nota"]:
phrase += f" ({data['nota']})"
elif data[f"nota{i+1}"]:
phrase += f" ({data[f'nota{i+1}']})"
if phrase:
a_phrase.append(phrase)

return f"{strong(starter)} {concat(a_phrase, ', ')}"


template_mapping = {
"adjetivo de verbo": render_adjetivo_de_verbo,
"AFI": render_afi,
"aumentativo": render_aumentativo,
"adverbio de adjetivo": render_adverbio_de_adjetivo,
"adverbio de sustantivo": render_adverbio_de_sustantivo,
"comparativo": render_comparativo,
"contracción": render_contraccion,
"diminutivo": render_aumentativo,
"etim": render_etim,
"etimología": render_etimologia,
Expand All @@ -732,6 +822,7 @@ def render_variante(tpl: str, parts: List[str], data: Dict[str, str]) -> str:
"sustantivo de adjetivo": render_sustantivo_de,
"sustantivo de verbo": render_sustantivo_de,
"variante": render_variante,
"variantes": render_variantes,
}


Expand Down
9 changes: 9 additions & 0 deletions wikidict/lang/fr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,15 @@ def last_template_handler(
>>> last_template_handler(["ar-mot", "elHasan_"], "fr")
'<span style="line-height: 0px;"><span style="font-size:larger">الحَسَن</span></span> <small>(elHasan_)</small>'
>>> last_template_handler(["ar-racine/nom", "ar-ktb"], "fr")
"كتب: relatif à l'action d'écrire, relier"
>>> last_template_handler(["ar-sch", "ar-*â*a*a"], "fr")
'زَارَزَ'
>>> last_template_handler(["ar-terme", "mu'ad²ibũ"], "fr")
"مُؤَدِّبٌ (<i>mu'ad²ibũ</i>) /mu.ʔad.di.bun/"
>>> last_template_handler(["nom langue", "gcr"], "fr")
'créole guyanais'
>>> last_template_handler(["langue", "gcr"], "fr")
Expand Down
5 changes: 5 additions & 0 deletions wikidict/lang/fr/ar_pronunciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def toIPA(arabic: str = "", tr: str = "") -> str:
>>> toIPA("فِي الله")
'fi‿l.laːh'
>>> toIPA("")
''
>>> toIPA("ة")
''
"""
translit = ""
if tr:
Expand Down

0 comments on commit 15d3740

Please sign in to comment.