Sunday, October 16, 2016

A Python HTML Table Gloss Maker

Since making glosses using the blogger editor by hand is tedious and boring, I suddenly realized I'm doing it often enough anyways to benefit from writing a short script for dealing with it. The code is not particularly beautiful or anything, but I figure it might be useful for some conlangers who want to write glosses in html, and just find doing so by hand tedious. Adding css or whatever to keep it beautiful is up to you. 

Here goes: 
 def Tokenzr(T, symbols):
        for sym in symbols:
                T = T.replace(sym, ' ' + sym)
        return T.split(' ')

def TRow(words):
        tr = " "
        for word in words:
                tr = tr + "" + word + "
"
        return tr + " "

def DefElems(description, elements):
        print(description + ":\n")
        output = []
        for element in elements:
                temp = raw_input("\n" + element + "\n")
                output.append(temp)
        return TRow(output)


T_tb_glossd = raw_input("Text to be glossed:\n")
tokend_T = Tokenzr(T_tb_glossd, [',', '.', '-', '_'])
print("\n")

gloss = "" + TRow(tokend_T) + DefElems("Grammatical glosses", tokend_T) + DefElems("Word for word translation", tokend_T) + ""
print(gloss)
By request, a python3 version, which is better whenever you use non-ascii symbols:
def Tokenzr(T, symbols):
    for sym in symbols:
        T = T.replace(sym, ' ' + sym)
    return T.split(' ')

def TableRow(words):
    tr = " "
    for word in words:
        tr = tr + "" + word + ""
    return tr + " "

def DefineElements(description, elements):
    print(description + ":\n")
    output = list()
    for element in elements:
        temp = input("\n" + element + "\n")
        output.append(temp)
    return TableRow(output)


T_tb_glossd = input("Text to be glossed:\n")
tokend_T = Tokenzr(T_tb_glossd, [',', '.', '-', '_'])
print("\n")
gloss = "" + TableRow(tokend_T) + DefineElements("Grammatical glosses", tokend_T) + DefineElements("Word for word translation", tokend_T) + "
"
print(gloss)

 
The script prints the result out to the console - to me at least that's more convenient than printing to a file. Making a browser plugin seems like overkill for such a trivial task. Just copy-paste and put in a .py file if you want to run it. The ugly abbreviations are just my own conventions.

From now on, I should never be caught making badly layed-out glosses for this blog, at least.

1 comment:

  1. You may want to rewrite that to Python 3 for superior Unicode support, though!

    ReplyDelete