-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngram.py
More file actions
223 lines (201 loc) · 9 KB
/
ngram.py
File metadata and controls
223 lines (201 loc) · 9 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import numpy as np
import random
class NGramModel:
def __init__(self, dictionary, rawWordCounts, isSmoothed):
self.isSmoothed = isSmoothed
self.dictionary = dictionary
self.dictionaryLength = len(dictionary);
self.rawWordCounts = rawWordCounts;
def getDictionary(self):
return self.dictionary
def getRawWordCounts(self):
return self.rawWordCounts
def toString(self):
print("\n--------------toString()-start-------------")
print("Dictionary length is : ", len(self.dictionary))
print("nGram Model is ", self.__class__.__name__)
print("--------------toString()-end---------------\n")
class UniGramModel(NGramModel):
def __init__(self, dictionary, rawWordCounts, isSmoothed):
super(UniGramModel,self).__init__(dictionary, rawWordCounts, isSmoothed);
self.countTable = self.rawWordCounts;
def calculateProbabilityTable(self):
N = self.dictionaryLength;
# TODO : isSmoothed
self.probabilyTable = (self.countTable+1)/(self.countTable.sum()+ N);
return self.probabilyTable;
def getLogProbabilityOfSentence(self, sentence):
tokens = sentence.split(" ");
logProbability = 0.0;
logProbabilityTable = np.log2(self.probabilyTable);
for token in tokens:
if token in self.dictionary:
logProbability = logProbability + logProbabilityTable[self.dictionary.index(token)]
return logProbability;
def generateEmail(self):
sentence = "<s>";
for i in range(30):
dictionaryIndex = self.getRandom();
sentence = sentence + " " + self.dictionary[dictionaryIndex ];
if self.dictionary[dictionaryIndex] == "</s>":
break;
return sentence;
def getRandom(self):
randomFlaot = random.uniform(0.0, 1.0);
probabilityCounter = 0.0;
for i in range(self.dictionaryLength):
probabilityCounter = probabilityCounter + self.probabilyTable[i];
if probabilityCounter > randomFlaot :
return i;
return -1;
class BiGramModel(NGramModel):
def __init__(self, dictionary, rawWordCounts, isSmoothed):
super(BiGramModel,self).__init__(dictionary, rawWordCounts, isSmoothed)
#self.probabilities = self._BiGramModel__calculateProbabilities()
def generateEmail(self, uniDictionary):
sentence = "<s>";
prefix = sentence;
for i in range(30):
dictionaryIndex = self.getRandom(len(uniDictionary), uniDictionary.index(prefix));
if dictionaryIndex != -1:
sentence = sentence + " " + uniDictionary[dictionaryIndex];
prefix = uniDictionary[dictionaryIndex];
if uniDictionary[dictionaryIndex] == "</s>":
break;
return sentence;
def getRandom(self, uniDictionaryLength, prefixIndex):
randomFlaot = random.uniform(0.0, 1.0);
temp = self.probabilityTable.sum(axis=1);
tempCoefficient = 1/temp[prefixIndex];
probabilityCounter = 0.0;
for i in range(uniDictionaryLength):
probabilityCounter = probabilityCounter + (self.probabilityTable[prefixIndex][i] * tempCoefficient);
if probabilityCounter > randomFlaot :
return i;
return -1;
def getLogProbabilityOfSentence(self, sentence, uniGramDictionary, prefix):
tokens = sentence.split(" ");
logProbability = 0.0;
logProbabilityTable = np.log2(self.probabilityTable);
for token in tokens:
if (token in uniGramDictionary) and (token in uniGramDictionary):
logProbability = logProbability + logProbabilityTable[uniGramDictionary.index(prefix) ,uniGramDictionary.index(token)]
prefix = token;
return logProbability;
def calculateProbabilityTable(self, uniDictionary, uniCountTable):
dictionaryLength = len(uniDictionary);
biGramWordCounts = np.zeros((dictionaryLength, dictionaryLength)).astype(np.float64);
biGramWordProbabilities = np.zeros((dictionaryLength, dictionaryLength)).astype(np.float64);
# print("Calculating biGram Probabilities...");
for i in range(self.dictionaryLength):
tokens = self.dictionary[i].split(" ");
isTokensTrue = True;
isTokensTrue = [(isTokensTrue and (token in uniDictionary)) for token in tokens];
if isTokensTrue :
index1 = uniDictionary.index(tokens[0]);
index2 = uniDictionary.index(tokens[1]);
biGramWordCounts[index1, index2] = self.rawWordCounts[i];
if self.isSmoothed:
temp = uniCountTable+dictionaryLength;
biGramWordProbabilities = (biGramWordCounts+1) / temp[:,None];
else:
biGramWordProbabilities = (biGramWordCounts) / uniCountTable[:,None];
self.countTable = biGramWordCounts;
self.probabilityTable = biGramWordProbabilities;
return self.probabilityTable;
class CorpusModel:
def __init__(self, datasetFileName):
self.datasetFileName = datasetFileName
with open(self.datasetFileName, encoding='utf-8') as file:
lines = file.readlines()
self.corpus = ["<s> " + line.strip() + " </s>" for line in lines]
#def getUnigramCountsAndDictionary():
def getDataBetween(self, startingIndex, endingIndex):
return self.corpus[startingIndex:endingIndex];
def getCorpus(self):
return self.corpus
def getTrainData(self):
maxIndex = 6*len(self.corpus)/10
return self.corpus[0:int(round(maxIndex))];
def getTestData(self):
maxIndex = 6*len(self.corpus)/10
return self.corpus[int(round(maxIndex)):];
def tokenize(s):
illegalTokens = ['!', '"', '$', '&', '(', ')', ',', '.', '-' ,'--', '/']
tempTokens = s.split(" ")
tokens = []
for token in tempTokens:
if token.strip() not in illegalTokens:
tokens.append(token.strip())
return tokens
class CountVectorizer:
def __init__(self, analyzer, ngram):
self.ngram = ngram;
self.analyzer = analyzer;
def fit_transform(self, data):
counts = [];
dictionary = [];
for sentence in data:
sentence = sentence.lower();
if "." in sentence:
sentence = sentence.replace(".", "");
if "," in sentence:
sentence = sentence.replace(".", "");
if "\"" in sentence:
sentence = sentence.replace(".", "");
tokens = self.analyzer(sentence);
for i in range(len(tokens)):
if i == (len(tokens)-(self.ngram-1)):
break;
gramWord = "";
if self.ngram == 1:
gramWord = tokens[i];
elif self.ngram == 2:
gramWord = tokens[i] + " " + tokens[i+1];
elif self.ngram == 3:
gramWord = tokens[i] + " " + tokens[i+1] + " " + tokens[i+2];
else:
#unhandled
exit(-1);
if gramWord not in dictionary:
dictionary.append(gramWord);
counts.append(1.0);
else:
gramWordIndex = dictionary.index(gramWord);
counts[gramWordIndex ] = counts[gramWordIndex ] + 1.0;
self.dictionary = dictionary;
self.counts = counts;
return counts;
def get_feature_names(self):
return self.dictionary;
def getCount(self):
return self.counts;
def getUnigramCountsAndDictionary(data):
counts = [];
dictionary = [];
for sentence in data:
tokens = tokenize(sentence);
for token in tokens:
if token not in dictionary:
dictionary.append(token);
counts.append(1.0);
else:
tokenIndex = dictionary.index(token);
counts[tokenIndex] = counts[tokenIndex] + 1.0;
return {"dictionary":dictionary, "counts":counts};
def getBigramCountsAndDictionary(data):
counts = [];
dictionary = [];
for sentence in data:
tokens = tokenize(sentence);
for i in range(len(tokens)):
if i == len(tokens)-1 :
break;
bigramWord = tokens[i] + " " + tokens[i+1];
if bigramWord not in dictionary:
dictionary.append(bigramWord);
counts.append(1.0);
else:
bigramWordIndex = dictionary.index(bigramWord);
counts[bigramWordIndex] = counts[bigramWordIndex] + 1.0;
return {"dictionary":dictionary, "counts":counts};