Tags: patch

The Moby Thesaurus dictionary package would have much
greater utility as a dictionary per se if it were inverted;
that is, if every word currently used as part of the
"definition" were instead a term defined (using the current
defined terms as the words in the definition).  The attached
extended conv.py includes the inverted thesaurus in the same
dict file.

import sys, dictdlib

# Conversion script by John Goerzen

itextfile = open("introduction.txt", "rt")


dw = dictdlib.DictWriter("moby-thesaurus",
                         'http://www.ibiblio.org/pub/docs/books/gutenberg/etext02/mthes10.zip',
                         'Moby Thesaurus II by Grady Ward, 1.0',
                         itextfile.read(-1))

imt = { }

while 1:
    line = sys.stdin.readline()
    if not line:
        break
    line.strip()
    base, d = line.split(',', 1)
    defstr = ""
    defline = "   "
    count = 0
    for defword in d.split(','):
        if defword not in imt:
            imt[defword] = [ ]
        imt[defword].append(base)
        count += 1
        if len(defline) + len(defword) + 3 > 70:
            defstr += defline + "," + "\n"
            defline = "   " + defword
        elif defline == "   ":
            defline += defword
        else:
            defline += ", " + defword
    if defline != "   ":
        defstr += defline + "\n"
    dw.writeentry("%s Moby Thesaurus words for \"%s\":\n" % (count, base) + \
                  defstr, [base])

for base in imt.keys():
    defstr = ""
    defline = "   "
    count = 0
    for defword in imt[base]:
        count += 1
        if len(defline) + len(defword) + 3 > 70:
            defstr += defline + "," + "\n"
            defline = "   " + defword
        elif defline == "   ":
            defline += defword
        else:
            defline += ", " + defword
    if defline != "   ":
        defstr += defline + "\n"
    dw.writeentry("%s Moby Thesaurus terms refer to \"%s\":\n" % (count, base) \
                  + defstr, [base])
dw.finish()

Reply via email to