-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-scrapervlinux.py
287 lines (240 loc) · 7.8 KB
/
web-scrapervlinux.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from bs4 import BeautifulSoup
import numpy as np
import re
import requests
import os.path
import sys, getopt
regex = re.compile(r'(?:<a.*?>\s*(.*?)\s*</a>)|(\w+)', re.IGNORECASE)
def main(argv):
#print(str)
XWING = False
CROSSHATCHING = False
ALTERN_PAIRS = False
help_str = 'script.py -d <difficulty> -o <outdir> -n <number> -s <box-size>'
try:
opts, args = getopt.getopt(argv,"hd:o:n:s:",["difficulty=", "outdir=", "number=","size="])
except getopt.GetoptError:
print (help_str)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print (help_str)
sys.exit()
elif opt in ("-d", "--difficulty"):
difficulty = arg
elif opt in ("-o", "--outdir"):
outdir = arg
if not os.path.exists(outdir):
os.makedirs(outdir)
elif opt in ("-n", "--number"):
N = int(arg)
elif opt in ("-s", "--block-size"):
size = int(arg)
assert (int(difficulty) >=0 and int(difficulty) <= 9), "difficulty ranges between 0 and 9!"
assert (N >= 0), "the number of sudokus must be non negative"
s_list = []
i=0
dim = size**2
while i <N:
if(dim == 9):
page = requests.get('http://www.menneske.no/sudoku/eng/random.html?diff='+difficulty)
else:
page = requests.get('http://www.menneske.no/sudoku/'+str(size)+'/eng/random.html?diff='+difficulty)
#soup = BeautifulSoup(page.text)
soup = BeautifulSoup(page.text, "lxml")
s_id=str(soup.body).split("number: ")
if len(s_id) < 2:
print("Sudoku not found")
break
s_id = s_id[1][0:7].split('<')[0]
table = soup.find('div', class_='grid')
if s_id in s_list:
print(s_id," is a copy\n")
else:
i+=1
print("Importing Sudoku # ",i," on ",N," ----- ID: ",s_id)
s_list.append(s_id)
sudoku = np.zeros((dim, dim))
encoding=""
num_clauses = calculate_standard_num_clauses(size)
num_variables = (size*size)**3
encoding = add_full_cnf(encoding,size)
r=0
for row in table.find_all('tr'):
c=0
for col in row.find_all('td'):
x=(str(col).split('>',1)[1].split('<')[0])
dum = x
if not dum.strip():
x = str(0)
#x = x.replace(" ","0")
#print(i,r,c)
sudoku[r][c]=int(x)
if x!="0":
encoding+= str(r+1)+'.'+str(c+1)+'.'+x+" 0\n"
num_clauses +=1
#for d in range(1,10):
#if str(d) != x: #print negative clauses
#encoding += '-'+str(r+1)+str(c+1)+str(d)+" 0\n"
c+=1
r+=1
dimac_first_line = 'p cnf {0} {1}\n'.format(num_variables, num_clauses)
tmp = str(soup.body).split("Solution methods: ")
tech = []
if len(tmp) > 1:
tmp =regex.findall(tmp[1].split("<br/>")[0])
for t in tmp:
if t[0] == '':
tech.append(t[1])
else:
tech.append(t[0])
#print("Printing Sudoku # ",i," on ",N," ----- ID: ",s_id)
#print(sudoku)
#print(encoding)
#print(tech)
#print(s_id)
# with open(os.path.join(outdir, s_id+"_sudoku.txt"), "w") as myfile:
# for r in range(size*size):
# for c in range(size*size):
# myfile.write(str(int(sudoku[r][c]))+" ")
# myfile.write("\n")
# myfile.close()
# with open(os.path.join(outdir, s_id+"_encoded.txt"), "w") as myfile:
# myfile.write(encoding)
# myfile.close()
with open(os.path.join(outdir, "dif"+str(difficulty)+"size"+str(size)+"_dimac"+s_id+".txt"), "w") as f:
encoding = convert_encoding_to_dimac(encoding,size)
encoding = dimac_first_line + encoding
f.write(encoding)
f.close()
# with open(os.path.join(outdir, s_id + '_info.txt'), 'w') as f:
# for t in tech:
# f.write(t + '\n')
# f.close()
print(" Done")
print('Finished!')
def calculate_standard_num_clauses(size):
n = size*size
return 2*(n**4)-2*(n**3)+4*(n**2)
def convert_chron(x,y,z, size):
dim = size*size
result = (dim*dim)*(x-1)+(dim*(y-1))+z
return result
def convert_encoding_to_dimac(encoding,size):
result =''
lines = encoding.splitlines()
for line in lines:
literals = line.split()
for literal in literals:
if literal[0] != '0': #End of line char
if literal[0] == '-': #is negative
x,y,z = literal[1:].split('.')
new_lit = convert_chron(int(x),int(y),int(z),size)*-1
result += str(new_lit)+' '
else:
x,y,z = literal.split('.')
new_lit = convert_chron(int(x),int(y),int(z),size)
result += str(new_lit)+' '
result+='0\n'
return result
def add_full_cnf(string,size):
string = col_at_least_one(string,size)
string = row_at_least_one(string,size)
string = val_at_least_one(string,size)
string = block_at_least_one(string,size)
string = row_at_most_one(string,size)
string = col_at_most_one(string,size)
string = val_at_most_one(string,size)
string = block_at_most_one(string,size)
return string
#print(string)
#print( len(string.split('\n')))
return string
def val_at_least_one(string,size):
size_sq = size*size
for r in range(1,size_sq+1): #at least one per cell
for c in range(1,size_sq+1):
line = ''
for v in range(1,size_sq+1):
line+= str(r)+'.'+str(c)+'.'+str(v)+' '
string += line + '0\n'
return string
def val_at_most_one(string,size):
size_sq = size*size
for r in range(1,size_sq+1): #at most one per cell
for c in range(1,size_sq+1):
for v1 in range(1,size_sq+1):
for v2 in range(v1+1,size_sq+1):
line = '-'+str(r)+'.'+str(c)+'.'+str(v1) +' ' +'-'+str(r)+'.'+str(c)+'.'+str(v2)
string += line + ' 0\n'
return string
def col_at_least_one(string,size):
size_sq = size*size
for r in range(1,size_sq+1): #at least one per column
for c in range(1,size_sq+1):
line = ''
for v in range(1,size_sq+1):
line+= str(r)+'.'+str(v)+'.'+str(c)+' '
string += line + '0\n'
return string
def col_at_most_one(string,size):
size_sq = size*size
for r in range(1,size_sq+1): #at most one per column
for c in range(1,size_sq+1):
for v1 in range(1,size_sq+1):
for v2 in range(v1+1,size_sq+1):
line = '-'+str(r)+'.'+str(v1)+'.'+str(c) +' ' +'-'+str(r)+'.'+str(v2)+'.'+str(c)
string += line + ' 0\n'
return string
def row_at_least_one(string,size):
size_sq = size*size
for r in range(1,size_sq+1): #at least one per row
for c in range(1,size_sq+1):
line = ''
for v in range(1,size_sq+1):
line+= str(v)+'.'+str(c)+'.'+str(r)+' '
string += line + '0\n'
return string
def row_at_most_one(string, size):
size_sq = size*size
for r in range(1,size_sq+1): #at most one per row
for c in range(1,size_sq+1):
for v1 in range(1,size_sq+1):
for v2 in range(v1+1,size_sq+1):
line = '-'+str(v1)+'.'+str(c)+'.'+str(r) +' ' +'-'+str(v2)+'.'+str(c)+'.'+str(r)
string += line + ' 0\n'
return string
def block_at_least_one(string, size):
size_sq = size*size
for v in range(1,size_sq+1): #at least one per block
for r_i in range(0,size_sq, size): #0,3,6
for c_i in range(0, size_sq, size): #0,3,6
line = ''
for r in range(1,size+1): #1,2,3
for c in range(1,size+1): #1,2,3
line+= str(r+r_i)+'.'+str(c+c_i)+'.'+str(v)+' '
string += line+ '0\n'
return string
def block_at_most_one(string, size):
size_sq = size*size
for v in range(1,size_sq+1): #at most one per block
for r_i in range(0,size_sq, size): #0,3,6
for c_i in range(0, size_sq, size): #0,3,6
for r in range(1,size+1): #1,2,3
for c in range(1,size+1): #1,2,3
gen = block_generator(size,r_i,c_i)
while True:
tuple = next(gen,None)
if not tuple: #generator empty
break
r_2,c_2 = tuple
if(r_2*100+c_2) > ((r_i+r)*100+(c+c_i)):
line = '-'+str(r_i+r)+'.'+str(c_i+c)+'.'+str(v)+' -'+str(r_2)+'.'+str(c_2)+'.'+str(v)
string += line+ ' 0 \n'
return string
def block_generator(size, r_i, c_i):
for r in range(1, size+1):
for c in range(1, size+1):
yield r+r_i, c+c_i
if __name__ == "__main__":
main(sys.argv[1:])