paranormal-or-skeptic/predict.py
2020-03-21 19:36:14 +01:00

30 lines
891 B
Python

import sys
import pickle
import math
from normalize import normalize
model = pickle.load(open("model.pkl", "rb"))
pskeptic, vocabulary_size,skeptick_words_total, paranormal_words_total, skeptic_count,paranormal_count = model
for line in sys.stdin:
document = line.rstrip()
terms = normalize(document)
log_prob_skeptic = math.log(pskeptic)
log_prob_paranormal = math.log(1-pskeptic)
for term in terms:
if term not in skeptic_count:
skeptic_count[term] = 0
if term not in paranormal_count:
paranormal_count[term] = 0
log_prob_skeptic += math.log((skeptic_count[term]+1)/(skeptick_words_total + vocabulary_size))
log_prob_paranormal += math.log((paranormal_count[term]+1)/(paranormal_words_total + vocabulary_size))
if log_prob_skeptic > log_prob_paranormal:
print("S")
else:
print("P")