/
NGrams.py
168 lines (143 loc) · 4.79 KB
/
NGrams.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
162
163
164
165
166
167
168
import nltk
import re
import math
import operator
from nltk import RegexpTokenizer
from collections import Counter
#First input & Tokenize
def startToken(input):
tokenizer = RegexpTokenizer(r'(\w+)')
file = open(input,'r')
text = file.read()
tokens = tokenizer.tokenize(text.lower())
file.close()
return tokens
#Further input & tokenize
def getToken(tokens,input):
tokenizer = RegexpTokenizer(r'(\w+)')
file = open(input,'r')
text = file.read()
tokens += tokenizer.tokenize(text.lower())
file.close()
#Build Unigram table
def getUnigram(tokens):
unigram = {}
unigram_count = 0
for token in tokens:
if token in unigram:
unigram[token] += 1
else:
unigram[token] = 1
unigram_count += 1
return unigram, unigram_count
#Build Bigram table
def getBigram(tokens):
length = len(tokens)
bigram = {}
bigram_count = 0
for x in range(0, length - 1):
if tokens[x] in bigram:
if tokens[x + 1] in bigram[tokens[x]]:
bigram[tokens[x]][tokens[x + 1]] += 1
else:
bigram[tokens[x]][tokens[x + 1]] = 1
bigram_count += 1
else:
bigram[tokens[x]] = {}
bigram[tokens[x]][tokens[x + 1]] = 1
bigram_count += 1
return bigram, bigram_count
#Handle unseen word
def getGT(tokens, unigram_count):
bigram_max = unigram_count * unigram_count
bigram, bigram_count = getBigram(tokens)
bigram_unseen = bigram_max - bigram_count
num_of_gram_of_counts = [bigram_unseen, 0, 0, 0, 0, 0]
for first in bigram:
for second in bigram[first]:
counter = bigram[first][second]
if counter < 6:
num_of_gram_of_counts[counter] += 1
GT_counts = [0, 0, 0, 0, 0]
for x in range(0, 5):
GT_counts[x] = (x + 1) * (num_of_gram_of_counts[x + 1] * 1.0000) / (num_of_gram_of_counts[x] * 1.0000)
return GT_counts
#Calculate Probability
def probability(first, second, unigram, bigram, gt_counts):
counter = 0
if second.lower() in bigram[first.lower()]:
counter = bigram[first.lower()][second.lower()]
if counter < 5:
counter = gt_counts[counter]
return counter / (unigram[first.lower()] * 1.0)
# Calculate perplexity
def compute_perplexity(tester, trainer1, trainer2):
tokens_tester = startToken(tester)
tokens_trainer = startToken(trainer1)
getToken(tokens_trainer, trainer2)
unigram, ucount = getUnigram(tokens_trainer)
bigram, bcount = getBigram(tokens_trainer)
gt_count = getGT(tokens_trainer, ucount)
tokens_tester_count = len(tokens_tester)
unk = 'unknown'
length = len(tokens_tester)
for i in range(0, length, 1):
if tokens_tester[i] not in unigram:
tokens_tester[i] = unk
if unk not in unigram:
unigram[unk] = 1
else:
unigram[unk] += 1
if 'unknown' not in bigram:
bigram[unk] = {}
bigram[unk][unk] = 1
else:
bigram[unk][unk] += 1
total = 0
for x in range(0, length - 1):
prob = probability(tokens_tester[x], tokens_tester[x + 1], unigram, bigram, gt_count)
total += math.log(prob) * (-1)
return math.exp((total / length))
# Generate Poetry
def generatePoetry(trainer1, trainer2, maxwordcount):
token = startToken(trainer1)
getToken(token, trainer2)
unigram, ucount = getUnigram(tokens)
bigram, bcount = getBigram(tokens)
gt_count = getGT(tokens, ucount)
sentences = ''
wordcount = 0
lastWord = ''
lastProb = 0
currentProb = 0
maxProb = 0
while (wordcount < maxwordcount):
if (wordcount == 0):
for word, count in unigram.items():
currentProb = count / len(token)
if maxProb < currentProb:
maxProb = currentProb
lastWord = word
sentences += lastWord
lastProb = maxProb
wordcount += 1
else:
if lastWord not in bigram:
maxProb = 0
for word, count in unigram.items():
currentProb = count / len(token)
if maxProb < currentProb:
maxProb = currentProb
lastWord = word
else:
maxProb = 0
for word in bigram[lastWord]:
currentProb = lastProb * probability(lastWord, word, unigram, bigram, gt_count)
if maxProb < currentProb:
maxProb = currentProb
lastWord = word
sentences += ' '
sentences += lastWord
lastProb = maxProb
wordcount += 1
return sentences