(train_file, test_file) = sys.argv[1:3]

print "Learning model..."
solver = Solver()
train_data = read_data(train_file)
solver.train(train_data)

print "Loading test data..."
test_data = read_data(test_file)

print "Testing classifiers..."
scorer = Score()
Algorithms = ("Simplified", "HMM VE", "HMM MAP")
Algorithm_labels = [ str(i+1) + ". " + Algorithms[i] for i in range(0, len(Algorithms) ) ]
for (s, gt) in test_data:
    outputs = {"0. Ground truth" : gt}
        
    # run all algorithms on the sentence
    for (algo, label) in zip(Algorithms, Algorithm_labels):
        outputs[label] = solver.solve( algo, s) 

    posteriors = { o: solver.posterior( s, outputs[o] ) for o in outputs }
    
    Score.print_results(s, outputs, posteriors)
        
    scorer.score(outputs)
    scorer.print_scores()
    
    print "----"
    end_time=time.time()
    # print (end_time-start_time)
Algorithm_labels = [
    str(i + 1) + ". " + Algorithms[i] for i in range(0, len(Algorithms))
]
for (s, gt) in test_data:
    emission = solver.emission_probability(s, train_data, tags_list)
    outputs = {"0. Ground truth": gt}

    # run all algorithms on the sentence
    maxx = []
    for (algo, label) in zip(Algorithms, Algorithm_labels):
        outputs[label], proba = solver.solve(algo, s, prob_all, tags_list,
                                             start_p, transition, emission)
        maxx.append(proba)

    # calculate posteriors for each output under each model
    count = 0

    posteriors = {
        o: {
            a: solver.posterior(a, maxx[0], maxx[1], maxx[2])
            for a in Algorithms
        }
        for o in outputs
    }
    Score.print_results(s, outputs, posteriors, Algorithms)

    scorer.score(outputs, gt)
    scorer.print_scores()

    print("----")
示例#3
0
(train_file, test_file) = sys.argv[1:3]

print "Learning model..."
solver = Solver()
train_data = read_data(train_file)
solver.train(train_data)

print "Loading test data..."
test_data = read_data(test_file)

print "Testing classifiers..."
scorer = Score()
Algorithms = ("Naive", "Sampler", "Max marginal", "MAP", "Best")
for (s, gt) in test_data:
    outputs = {"0. Ground truth" : [[gt,], []]}

    # run all algorithms on the sentence
    for i in range(0, len(Algorithms)):
        outputs[ str(i+1) + ". " + Algorithms[i] ] = solver.solve(Algorithms[i], s)

    # compute posteriors for each solution
    posteriors = { algo: [ solver.posterior(s, output)  for output in outputs[algo][0] ] for algo in outputs }
    

    Score.print_results(s, outputs, posteriors)

    scorer.score(outputs)
    scorer.print_scores()

    print "----"