# Part 1 and 2 checks: what train_nb returns and the two sanity checks
import numpy as np
from common import load_split
from nb import train_nb, score_doc_label, classify_nb

train_docs, train_labels, eval_docs, eval_labels = load_split()
model = train_nb(train_docs, train_labels)

print('classes:', model['classes'])
print('vocabulary size:', len(model['vocab']))
for c in model['classes']:
    print(c, 'prior =', round(float(np.exp(model['log_prior'][c])), 4))

print()
print('Sanity check 1 (one-word documents, alpha = 1)')
for word in ['great', 'bad']:
    for c in model['classes']:
        lp = score_doc_label([word], c, model)
        print(' ', word, c, 'log prob =', round(float(lp), 3),
              ' prob =', round(float(np.exp(lp)), 5))

print()
print('Sanity check 2')
doc = ['a', 'top-quality', 'performance']
for c in model['classes']:
    print(' ', doc, c, 'log prob =', round(float(score_doc_label(doc, c, model)), 3))
print('  in vocabulary?', [w in model['vocab'] for w in doc])

print()
print('A few tiny documents')
for doc in [['great'], ['bad'], ['great', 'product'], ['waste', 'of', 'money'],
            ['i', 'love', 'it'], ['not', 'good']]:
    print(' ', doc, '->', classify_nb(doc, model))
