Aller au contenu

Utilisateur:Lepticed7/Script lettres diacritées

Définition, traduction, prononciation, anagramme et synonyme sur le dictionnaire libre Wiktionnaire.
import pywikibot as pwb

ACCENTUATED_LETTERS = set('ĉĝĥĵŝŭĈĜĤĴŜŬ')


def get(normal, pron, c, o, l):
    return f"""== {{{{langue|eo}}}} ==
=== {{{{S|variante typographique|eo}}}} ===
'''{c}''' {{{{pron|{pron}|eo}}}}
# {{{{eo-sys-{l}|{normal}}}}}.

==== {{{{S|écriture}}}} ====
* {{{{lien|{normal}|eo}}}}
* {{{{lien|{o}|eo}}}}"""


def x_get(normal, x, h, pron):
    return get(normal, pron, x, h, 'x')


def h_get(normal, x, h, pron):
    return get(normal, pron, h, x, 'h')


def has_special_letter(word):
    """
    Checks if a word contains one of the following letters: ĉ, ĝ, ĥ, ĵ, ŝ, ŭ
    """
    return bool(set(word) & ACCENTUATED_LETTERS)


def convert_to_x_system(word):
    """
    Converts a given word with the X-system.
    """
    return word.replace('ĉ', 'cx') \
        .replace('ĝ', 'gx') \
        .replace('ĥ', 'hx') \
        .replace('ĵ', 'jx') \
        .replace('ŝ', 'sx') \
        .replace('ŭ', 'ux') \
        .replace('Ĉ', 'Cx') \
        .replace('Ĝ', 'Gx') \
        .replace('Ĥ', 'Hx') \
        .replace('Ĵ', 'Jx') \
        .replace('Ŝ', 'Sx') \
        .replace('Ŭ', 'Ux')


def convert_to_h_system(word):
    """
    Converts a given word with the H-system.
    """
    return word.replace('ĉ', 'ch') \
        .replace('ĝ', 'gh') \
        .replace('ĥ', 'hh') \
        .replace('ĵ', 'jh') \
        .replace('ŝ', 'sh') \
        .replace('ŭ', 'u') \
        .replace('Ĉ', 'Ch') \
        .replace('Ĝ', 'Gh') \
        .replace('Ĥ', 'Hh') \
        .replace('Ĵ', 'Jh') \
        .replace('Ŝ', 'Sh') \
        .replace('Ŭ', 'U')


site = pwb.Site('fr', 'wiktionary')

for page in pwb.Category(site, 'espéranto').articles(reverse=True):
    normal = page.title()

    if has_special_letter(normal):
        try:
            pron = [t for t in page.raw_extracted_templates if
                    t[0] == 'pron' and (
                            '2' in t[1] and t[1]['2'] == 'eo' or 'lang' in t[1] and t[1]['lang'] == 'eo')][
                0][
                1]['1']
        except IndexError as e:
            pron = ''

        del page

        x = convert_to_x_system(normal)
        h = convert_to_h_system(normal)

        page_x: pwb.Page = pwb.Page(site, x)
        if page_x.isRedirectPage() or not page_x.text:
            page_x.text = x_get(normal, x, h, pron)
            page_x.save()

        page_h = pwb.Page(site, h)
        if page_h.isRedirectPage() or not page_h.text:
            page_h.text = h_get(normal, x, h, pron)
            page_h.save()