-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
188 lines (149 loc) · 4.29 KB
/
main2.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from string import ascii_letters
from collections import Counter
from pprint import pprint
import numpy as np
from nltk import ngrams
import codecs
import itertools
import operator
def generation2(list):
result = {}
result2= {}
liste = decoupe(list)
for s in liste:
if result.has_key(s):
result[s] = result[s]+ 1
else:
result[s] = 1
for s in result:
result2[s] = result[s]/float(len(result))
return result2;
def sum2(list):
tmp = 0
for y in list:
tmp = tmp + list[y]
return tmp
def generation(list):
result ={}
final = {}
liste = decoupe(list)
for s in liste:
a = (s[0])
c = s[1]
if result.has_key(a):
if result[a].has_key(s[1]):
result[a][c] = result[a][c] +1
else:
result[a][c] = 1
else:
result[a]= {}
result[a][c] = 1
for y in result:
ma_value = sum2(result[y])
for ee in result[y]:
result[y][ee]= result[y][ee]/float(ma_value)
return result
'''
def decoupe(l):
strings = ""
for e in l:
strings = strings + e
print(list(ngrams(strings.split(),2)))
return 0
'''
def decoupe(l):
result = []
taille = len(l)
for i in range(1,taille):
result.append((l[i-1],l[i]))
return result
def sample_from_discrete_distrib(distrib):
words, probas = list(zip(*distrib.items()))
s = np.random.choice(words, p = probas)
return s
def getList(file):
result = {}
count =0
motss = []
with codecs.open(file,"r",encoding="utf8") as my_file:
for line in my_file:
line= line.strip() # remove the \n*
mots = line.split(" ")
for s in mots:
motss.append(s)
count +=1
if(result.has_key(s)):
result[s]+=1
else:
result[s]= 1
for s in result:
result[s] = result[s]/float(count)
return (result,motss)
def proba(phrase, probas):
s = phrase.split()
count = 1.0
for e in s:
if(probas.has_key(e)):
count *= probas[e]
return count
def generationMots(list):
mon_dico = list
ma_liste = ["there"]
while(ma_liste[len(ma_liste)-1] != "."):
taille = len(ma_liste)
cle =(ma_liste[taille-1])
ma_hash = mon_dico[cle]
eee = sample_from_discrete_distrib(ma_hash)
ma_liste.append(eee)
#pprint(ma_liste)
return ma_liste
def decodes(liste):
buffer = ""
for i in range(2,len(liste)):
buffer = buffer + liste[i] + " "
if(i==2):
buffer = buffer.title()
print buffer
(probas,texte) = getList("newsco.en")
print(proba("i want that house",probas))
#print(list(decoupe("Stephe is an asshole")))
def summ(en):
count = 0.0
for s in en:
count += en[s]
return count
def probasBigram(texte,model,uni):
mots = texte.split()
red = uni[mots[0]]
h = mots[0]
for s in range(1,len(mots)):
print(mots[s])
if(model.has_key(h)):
if(model[h].has_key(mots[s])):
red *= model[h][mots[s]]
else:
model[h][mots[s]] = 1.0/summ(model[h])
red *= model[h][mots[s]]
else:
model[h] ={}
model[h][mots[s]] = 1.0
red *= model[h][mots[s]]
h = mots[s]
return red
bigramm = (generation(texte))
#print(probasBigram("there is a house mslls msls",bigramm,probas))
def sortes(ma_list):
result = sorted(ma_list.items(), key=operator.itemgetter(1),reverse= True)
return result
#print(bigramm["there"]["is"])
#print(probas["there"])
#generationMots(bigramm)
#decodes(generationMots(listeFinal))
def test_permutation(phrase):
list_permuter = list(itertools.permutations(phrase.split(" ")))
score = {}
print(len(list_permuter))
for phr in list_permuter:
score[phr] = probasBigram(" ".join(phr),bigramm,probas)
pprint(sortes(score))
test_permutation("i like apple")