This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngram.py
161 lines (141 loc) · 6.08 KB
/
ngram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/python
import math
import re
import string
# import matplotlib.pyplot as plt
from decimal import Decimal
from corpus import Corpus
from collections import Counter
from functools import reduce
from loguru import logger
class NGram:
"""
NGram is similar to an abstract factory.
It is used to create Unigrams and Bigrams
"""
@staticmethod
def create(degree=1, delta=0.5, name="", vocabulary=26):
if degree == 1:
return Unigram(delta, name, vocabulary)
else:
return Bigram(delta, name, vocabulary)
class Unigram:
def __init__(self, delta, name, vocabulary):
self.delta = delta
self.name = name
self.vocabulary = vocabulary
self.corpus = [] # train corpus
self.tokenized = [] # tokenized corpus
self.count = [] # Counter
self.model = dict({el:0 for el in string.ascii_lowercase})
def train(self, training_corpus):
self.corpus = Corpus(training_corpus)
self.tokenized = self.corpus.clean_and_tokenize()
self.count = Counter([(self.tokenized[i]) for i in range(0,len(self.tokenized)-1)])
self.print_model(self.count)
def predict(self, test_corpus):
test = Corpus(test_corpus).sanitize()
count = 1
for line in test:
count += 1
tokens = []
logTotal = 0
logger.debug(' '.join(line)+'\n\n')
logger.debug('UNIGRAM MODEL trained on: {}\n\n'.format(self.corpus.name))
tokens.append(Corpus.tokenize(line))
for line in tokens:
for c in line:
logger.debug('UNIGRAM: {}\n'.format(c))
f = self.model[c]
logTotal += math.log10(f)
logger.debug('MODEL PROBABILITY: P({}) = {:.4e} ==> log prob of sequence so far: {:.4e}\n'.format(c, f, logTotal))
def print_model(self, ngram):
outfile = open("output/{}.txt".format(self.name), "w", encoding="utf8")
for letter in string.ascii_lowercase:
letter_frequency = ngram.get(letter)
if letter_frequency is not None:
letter_frequency += letter_frequency + self.delta
else:
letter_frequency = self.delta
total_characters = len(self.tokenized) + self.delta * self.vocabulary
self.model[letter] = Decimal(letter_frequency/total_characters)
outfile.write("P({}) = {:.4e}\n".format(letter, self.model[letter]))
outfile.close()
# self.print_histogram()
def get_model(self):
return self.model
# def print_histogram(self):
# plt.bar(range(len(self.model)), self.model.values(), align='center')
# plt.xticks(range(len(self.model)), self.model.keys())
# plt.title("frequency distribution of "+self.name)
# plt.xlabel("letters")
# plt.ylabel("frequency")
# #plt.show()
# plt.savefig('output/'+self.name+'.png')
# plt.close()
class Bigram:
def __init__(self, delta, name, vocabulary):
self.delta = delta
self.name = name
self.vocabulary = vocabulary
self.corpus = [] # train corpus
self.tokenized = [] # tokenized corpus
self.count = [] # Counter
self.pairs = [(a,b) for a in string.ascii_lowercase for b in string.ascii_lowercase]
self.model = dict({el:0 for el in self.pairs})
def train(self, training_corpus):
self.corpus = Corpus(training_corpus)
self.tokenized = self.corpus.clean_and_tokenize()
self.unigram = Counter([(self.tokenized[i]) for i in range(0,len(self.tokenized)-1)])
self.count = Counter([(self.tokenized[i],self.tokenized[i+1]) for i in range(0,len(self.tokenized)-1)])
self.print_model(self.count)
def predict(self, test_corpus):
test = Corpus(test_corpus).sanitize()
count = 1
for line in test:
tokens = []
logTotal = 0
logger.debug(' '.join(line)+'\n\n')
logger.debug('BIGRAM MODEL: trained on: {}\n\n'.format(self.corpus.name))
tokens.append(Corpus.tokenize(line))
tmp = tokens[0]
count += 1
pairs = [a+b for a,b in zip(tmp,tmp[1:])]
for p in pairs:
logger.debug('BIGRAM: {}{}\n'.format(p[0],p[1]))
f = self.model[(p[1],p[0])]
logTotal += math.log10(f)
logger.debug('MODEL PROBABILITY: P({}|{}) = {:.4e} ==> log prob of sequence so far: {:.4e}\n'.format(p[1], p[0], f, logTotal))
def print_model(self, ngram):
character_count = dict({el:0 for el in string.ascii_lowercase})
for c in character_count:
letter_frequency = self.unigram.get(c)
if letter_frequency is not None:
character_count[c] = letter_frequency + self.vocabulary * self.delta
else:
character_count[c] = self.vocabulary
outfile = open("output/{}.txt".format(self.name), "w", encoding="utf8")
for pair in self.pairs:
pair_frequency = ngram.get(pair)
if pair_frequency is not None:
pair_frequency += pair_frequency + self.delta
else:
pair_frequency = self.delta
self.model[pair] = Decimal(pair_frequency/character_count[pair[0]])
outfile.write("P({}|{}) = {:.4e}\n".format(pair[1], pair[0], self.model[pair]))
outfile.close()
# self.print_histogram()
def get_model(self):
return self.model
# def print_histogram(self):
# most_common = {}
# for letter, count in self.count.most_common(40):
# most_common[letter[0]+letter[1]] = count
# plt.bar(range(len(most_common)), most_common.values(), align='center')
# plt.xticks(range(len(most_common)), most_common.keys(), rotation='vertical')
# plt.title("Most common letter pairs in "+self.name)
# plt.xlabel("letter pairs")
# plt.ylabel("count")
# #plt.show()
# plt.savefig('output/'+self.name+'.png')
# plt.close()