paranormal-or-skeptic/code.py

76 lines
3.0 KiB
Python
Raw Permalink Normal View History

2020-03-22 14:21:40 +01:00
from collections import defaultdict
import math
import pickle
2020-05-02 17:01:57 +02:00
import re
2020-03-22 14:21:40 +01:00
2020-03-28 20:40:28 +01:00
open_file=('test-A/out.tsv')
#---------------TRAIN START
#Prawdopodobienstwo wylosowania dokumentu
2020-03-22 14:21:40 +01:00
def calc_class_logprob(expected_path):
paranormal_classcount=0
skeptic_classcount=0
2020-03-28 20:40:28 +01:00
with open(expected_path,encoding='utf-8') as f:
2020-03-22 14:21:40 +01:00
for line in f:
2020-05-02 19:16:25 +02:00
if '1' in line:
2020-03-22 14:21:40 +01:00
paranormal_classcount += 1
2020-05-02 19:16:25 +02:00
if '0' in line:
2020-03-22 14:21:40 +01:00
skeptic_classcount += 1
paranormal_prob = paranormal_classcount / (paranormal_classcount + skeptic_classcount)
skeptic_prob = skeptic_classcount / (paranormal_classcount + skeptic_classcount)
return math.log(paranormal_prob), math.log(skeptic_prob)
def calc_word_count(in_path, expected_path):
word_counts = {'paranormal':defaultdict(int), 'skeptic': defaultdict(int)}
2020-03-28 20:40:28 +01:00
with open(in_path,encoding='utf-8') as in_file, open(expected_path,encoding='utf-8') as expected_file:
2020-03-22 14:21:40 +01:00
for line, exp in zip(in_file, expected_file):
2020-05-02 18:49:56 +02:00
class_ = exp.rstrip('\n').replace(' ','')
2020-03-22 14:21:40 +01:00
text, timestamp = line.rstrip('\n').split('\t')
2020-05-02 18:40:08 +02:00
text = text.lower()
2020-05-02 18:16:25 +02:00
text = re.sub(r'\\n+', " ", text)
text = re.sub(r'http\S+', " ", text)
2020-05-02 19:26:03 +02:00
text = re.sub(r'\/[a-z]\/', " ", text)
text = re.sub(r'[^a-z]', " ", text)
text = re.sub(r'\s{2,}', " ", text)
text = re.sub(r'\W\w{1,3}\W|\A\w{1,3}\W', " ", text)
text = re.sub(r'\W\w{1,3}\W|\A\w{1,3}\W', " ", text)
text = re.sub(r'\W\w{1,3}\W|\A\w{1,3}\W', " ", text)
text = re.sub(r'^\s', "", text)
2020-05-02 18:40:08 +02:00
tokens = text.split(' ')
2020-03-22 14:21:40 +01:00
for token in tokens:
2020-05-02 19:16:25 +02:00
if class_ == '1':
2020-03-22 14:21:40 +01:00
word_counts['paranormal'][token] += 1
2020-05-02 19:16:25 +02:00
elif class_ == '0':
2020-03-22 14:21:40 +01:00
word_counts['skeptic'][token] += 1
return word_counts
def calc_word_logprobs(word_counts):
total_skeptic = sum(word_counts['skeptic'].values()) + len(word_counts['skeptic'].keys())
total_paranormal = sum(word_counts['paranormal'].values()) + len(word_counts['paranormal'].keys())
word_logprobs= {'paranormal': {}, 'skeptic': {}}
for class_ in word_counts.keys(): # sceptic paranormal
for token, tokens in word_counts[class_].items():
if class_ == 'skeptic':
word_prob = (tokens+1)/total_skeptic
else:
word_prob = (tokens+1)/total_paranormal
word_logprobs[class_][token] = math.log(word_prob)
return word_logprobs
2020-03-28 20:40:28 +01:00
#--------------- TRAIN END
2020-03-22 14:21:40 +01:00
def main():
2020-03-28 20:40:28 +01:00
paranomal_class_logprob, skeptic_class_logprob = calc_class_logprob("train/expected.tsv")
word_counts=calc_word_count("train/in.tsv","train/expected.tsv")
2020-05-02 18:49:56 +02:00
word_counts['paranormal'][''] = 0
word_counts['skeptic'][''] = 0
2020-03-22 14:21:40 +01:00
word_logprobs = calc_word_logprobs(word_counts)
pickle.dump([paranomal_class_logprob, skeptic_class_logprob, word_logprobs], open('naive_base_model.pkl','wb'))
main()