natural language toolkit interface to tadm classifier c 20012023 nltk project joseph frazee jfrazeemail utexas edu url https www nltk org for license information see license txt generate an input file for tadm based on the given corpus of classified tokens type traintoks listtupledict str param traintoks training data represented as a list of pairs the first member of which is a feature dictionary and the second of which is a classification label type encoding tadmeventmaxentfeatureencoding param encoding a feature encoding used to convert featuresets into feature vectors type stream stream param stream the stream to which the tadm input file should be written see the following for a file format description https sf netforumforum php threadid1391502forumid473054 https sf netforumforum php threadid1675097forumid473054 given the stdout output generated by tadm when training a model return a numpy array containing the corresponding weight vector call the tadm binary with the given arguments call tadm via a subprocess check the return code natural language toolkit interface to tadm classifier c 2001 2023 nltk project joseph frazee jfrazee mail utexas edu url https www nltk org for license information see license txt generate an input file for tadm based on the given corpus of classified tokens type train_toks list tuple dict str param train_toks training data represented as a list of pairs the first member of which is a feature dictionary and the second of which is a classification label type encoding tadmeventmaxentfeatureencoding param encoding a feature encoding used to convert featuresets into feature vectors type stream stream param stream the stream to which the tadm input file should be written see the following for a file format description https sf net forum forum php thread_id 1391502 forum_id 473054 https sf net forum forum php thread_id 1675097 forum_id 473054 given the stdout output generated by tadm when training a model return a numpy array containing the corresponding weight vector call the tadm binary with the given arguments call tadm via a subprocess check the return code
import subprocess
import sys

from nltk.internals import find_binary

try:
    import numpy
except ImportError:
    pass

_tadm_bin = None


def config_tadm(bin=None):
    global _tadm_bin
    _tadm_bin = find_binary(
        "tadm", bin, env_vars=["TADM"], binary_names=["tadm"], url="http://tadm.sf.net"
    )


def write_tadm_file(train_toks, encoding, stream):
    
    
    
    
    
    labels = encoding.labels()
    for featureset, label in train_toks:
        length_line = "%d\n" % len(labels)
        stream.write(length_line)
        for known_label in labels:
            v = encoding.encode(featureset, known_label)
            line = "%d %d %s\n" % (
                int(label == known_label),
                len(v),
                " ".join("%d %d" % u for u in v),
            )
            stream.write(line)


def parse_tadm_weights(paramfile):
    
    weights = []
    for line in paramfile:
        weights.append(float(line.strip()))
    return numpy.array(weights, "d")


def call_tadm(args):
    
    if isinstance(args, str):
        raise TypeError("args should be a list of strings")
    if _tadm_bin is None:
        config_tadm()

    
    cmd = [_tadm_bin] + args
    p = subprocess.Popen(cmd, stdout=sys.stdout)
    (stdout, stderr) = p.communicate()

    
    if p.returncode != 0:
        print()
        print(stderr)
        raise OSError("tadm command failed!")


def names_demo():
    from nltk.classify.maxent import TadmMaxentClassifier
    from nltk.classify.util import names_demo

    classifier = names_demo(TadmMaxentClassifier.train)


def encoding_demo():
    import sys

    from nltk.classify.maxent import TadmEventMaxentFeatureEncoding

    tokens = [
        ({"f0": 1, "f1": 1, "f3": 1}, "A"),
        ({"f0": 1, "f2": 1, "f4": 1}, "B"),
        ({"f0": 2, "f2": 1, "f3": 1, "f4": 1}, "A"),
    ]
    encoding = TadmEventMaxentFeatureEncoding.train(tokens)
    write_tadm_file(tokens, encoding, sys.stdout)
    print()
    for i in range(encoding.length()):
        print("%s --> %d" % (encoding.describe(i), i))
    print()


if __name__ == "__main__":
    encoding_demo()
    names_demo()