Jump to content

User:SemperBlottoBot/adjectives

From Wiktionary, the free dictionary
  • itadjs.py
#coding: utf-8
"""
This module reads a file of Italian adjectives and generates the feminine and plural forms.
"""
__version__='$Id: Exp $'

import wikipedia, config
import re, sys, codecs

mysite = wikipedia.getSite()
commenttext = 'Auto-generated Italian adjective forms'
global itadjs, fs, mp, fp, mfp
itadjs = ["---"]
fs = ""
mp = ""
fp = ""
mfp = ""

def doargform(form, ms, type):
    if form == "":
        return
    if type == "fs":
        gender = "f"
        text1 = "{{feminine of|[["
        text2 = "]]}}"
    elif type == "mp":
        gender = "m"
        text1 = "Plural form of [["
        text2 = "]]"
    elif type == "fp":
        gender = "f"
        text1 = "Feminine plural form of [["
        text2 = "]]"
    else:
        gender = "m|f"
        text1 = "Plural form of [["
        text2 = "]]"
    newpage = "==Italian==" + '\n\n' + "===Adjective===" + '\n'
    newpage = newpage + "'''" + form + "'''" + " {{" + gender + "}}" + '\n\n'
    newpage = newpage + "# " + text1 + ms + text2 + '\n' + "[[Category:Italian adjective forms]]"
    page = wikipedia.Page(mysite, form)
    if page.exists():
        old_text = page.get()
        if not re.search(r'==\s*Italian\s*==', old_text):
            contents = old_text + '\n\n----\n'  + newpage + '\n{{rfc-auto}}\n'
            commenttext_add = commenttext + " - appended"
            wikipedia.output(u"Page %s already exists, adding to entry!"%form)
            page.put(contents, comment = commenttext_add, minorEdit = False)
        else:
            wikipedia.output(u"Page %s already exists with Italian section, not adding!"%form)
    else:
        page.put(newpage, comment = commenttext, minorEdit = True) # was False (see above)

def findadjforms(ms):
    global itadjs, fs, mp, fp, mfp
    page = wikipedia.Page(mysite, ms)
    text = page.get()
    x = text.find("{{it-adj|")
    if x == -1:
        print "No template found"
        itadjs.append(ms)
        return ""
    text = text[x+9:]
    x = text.find("}}")
    text = text[:x]
    x = text.find("|")
    if x > 0:
        stem = text[:x]
    else:
        stem = text
    text = text[x+1:]
    x = text.find("|")
    if x < 1: # No extra parameters
        fs = stem + "a"
        mp = stem + "i"
        fp = stem + "e"
        mfp = ""
    else:
        text = text[x+1:]
        x = text.find("|")
        if x < 1: # mfs + mfp
            fs = ""
            mp = ""
            fp = ""
            mfp = stem + text[x+1:]
        else: # All parameters specified
            fs = stem + text[:x]
            text = text[x+1:]
            x = text.find("|")
            mp = stem + text[:x]
            fp = stem + text[x+1:]
            mfp = ""
    doargform(fs, ms, "fs")
    doargform(mp, ms, "mp")
    doargform(fp, ms, "fp")
    doargform(mfp, ms, "mfp")
    return

    
adjs = open('itadjs.txt', 'r')
ms = adjs.readline() # First line strange
while len(ms) > 1:
    ms = adjs.readline()[:-1]
    if len(ms) > 1:
        findadjforms(ms)
adjs.close()
for I in itadjs:
    print I