Jump to content

User:SemperBlottoBot/nouns

From Wiktionary, the free dictionary
  • itnouns.py
#coding: utf-8
"""
This module reads a file of Italian nouns and generates the plural forms of those that are probably regular.
"""
__version__='$Id: Exp $'

import wikipedia, config
import re, sys, codecs

mysite = wikipedia.getSite()
commenttext = 'Auto-generated Italian plurals'
global gender
global itnouns
itnouns = ["---"]

def doplural(singular, plural):
    global gender
    newpage = "==Italian==" + '\n\n' + "===Noun===" + '\n'
    newpage = newpage + "'''" + plural + "'''" + " {{" + gender + "}}" + '\n\n'
    newpage = newpage + "# {{plural of|[[" + singular + "]]|lang=Italian}}"
    page = wikipedia.Page(mysite, plural)
    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!"%plural)
            page.put(contents, comment = commenttext_add, minorEdit = False)
        else:
            wikipedia.output(u"Page %s already exists with Italian section, not adding!"%plural)
    else:
        page.put(newpage, comment = commenttext, minorEdit = True) # was False (see above)

def findplural(singular):
    global gender
    global itnouns
    page = wikipedia.Page(mysite, singular)
    text = page.get()
    x = text.find("{{it-noun|")
    if x == -1:
        print "No template found"
        itnouns.append(singular)
        return ""
    text = text[x+10:]
    x = text.find("}}")
    text = text[:x]
    x = text.find("|")
    stem = text[:x]
    text = text[x+1:]
    x = text.find("|")
    gender = text[:x]
    text = text[x+1:]
    while text.find("|") > 0:
        x = text.find("|")
        text = text[x+1:]
    plural = stem + text
    if plural[:1] < "a": #  Template generated nouns with multiple words
        plural = plural[1:]
    return plural

verbs = open('itnouns.txt', 'r')
singular = verbs.readline() # First line strange
while len(singular) > 1:
    singular = verbs.readline()[:-1]
    if len(singular) > 1:
         plural = findplural(singular)
         if not plural == "":
            doplural(singular, plural)
verbs.close()
for I in itnouns:
    print I