#!/usr/bin/env python
import sys
import argparse
from collections import OrderedDict

from langdet import \
    RandomLanguageDetector, \
    CosineLanguageDetector, \
    BigramCosineLanguageDetector, \
    TrigramCosineLanguageDetector, \
    MultiCosineLanguageDetector, \
    stream_sample

algos = OrderedDict([
    ('random', RandomLanguageDetector),
    ('cosine1', CosineLanguageDetector),
    ('cosine2', BigramCosineLanguageDetector),
    ('cosine3', TrigramCosineLanguageDetector),
    ('multi', MultiCosineLanguageDetector),
    ])

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('train', help='Training set')
    parser.add_argument('test', help='Testing set')
    parser.add_argument('-m', '--model', metavar='MODEL', help='Use a single model to classify', default=None)
    parser.add_argument('-d', '--debug', default=False, action='store_true', help='Debug mode: show mistakes')
    args = parser.parse_args()

    train = list(stream_sample(args.train))
    test = list(stream_sample(args.test))

    if args.model:
        algos = {args.model: algos[args.model]}

    for algo_name, algo_class in algos.iteritems():
        det = algo_class()
        det.train(train)
        precision, recall, mistakes = det.eval(test)

        print algo_name
        print '%s\t%s\t%s' % ('Lang', 'Prec', 'Rec')
        for lang in sorted(precision):
            print '%s\t%.2f\t%.2f' % (lang, precision[lang], recall[lang])
        print

        if args.debug:
            print 'Mistakes:'
            for t, exp, ret in mistakes:
                sys.stdout.write('\t'.join([t.encode('utf8'), exp, ret]) + '\n')

4549a458cdf0af3a1dd422aa3868ad5f3e9959fe
#!/usr/bin/env python
import sys
import argparse
from collections import OrderedDict

from langdet import \
    RandomLanguageDetector, \
    CosineLanguageDetector, \
    BigramCosineLanguageDetector, \
    TrigramCosineLanguageDetector, \
    MultiCosineLanguageDetector, \
    stream_sample

algos = OrderedDict([
    ('random', RandomLanguageDetector),
    ('cosine1', CosineLanguageDetector),
    ('cosine2', BigramCosineLanguageDetector),
    ('cosine3', TrigramCosineLanguageDetector),
    ('multi', MultiCosineLanguageDetector),
    ])

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('train', help='Training set')
    parser.add_argument('test', help='Testing set')
    parser.add_argument('-m', '--model', metavar='MODEL', help='Use a single model to classify', default=None)
    parser.add_argument('-d', '--debug', default=False, action='store_true', help='Debug mode: show mistakes')
    args = parser.parse_args()

    train = list(stream_sample(args.train))
    test = list(stream_sample(args.test))

    if args.model:
        algos = {args.model: algos[args.model]}

    for algo_name, algo_class in algos.iteritems():
        det = algo_class()
        det.train(train)
        precision, recall, mistakes = det.eval(test)

        print algo_name
        print '%s\t%s\t%s' % ('Lang', 'Prec', 'Rec')
        for lang in sorted(precision):
            print '%s\t%.2f\t%.2f' % (lang, precision[lang], recall[lang])
        print

        if args.debug:
            print 'Mistakes:'
            for t, exp, ret in mistakes:
                sys.stdout.write('\t'.join([t.encode('utf8'), exp, ret]) + '\n')
