-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_queries.py
executable file
·57 lines (49 loc) · 1.55 KB
/
filter_queries.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
#!/usr/bin/python3.4
import re
path = "data/raw/"
filename = path + "user-ct-test-collection-0"
out = open("data/aol_queries.txt","w")
substrings = ["http", "www.", ".com", ".net", ".org", ".edu"]
queries = []
#iterate over the lines of the first 9 files, get queries that don't contain substrings and make them contain only alpharithmetic characters
for i in range(1,10):
files = open(filename + str(i)+".txt","r")
lines = files.readlines()
files.close()
for j in range(0,len(lines)):
query = lines[j].split("\t")[1]
flag = 0
for string in substrings:
try:
query.index(string)
flag = 1
except ValueError:
continue
if flag == 0:
query = re.sub("[^a-zA-Z0-9 ]", '', query)
query = " ".join(query.split())
if (len(query) != 0):
queries.append(query+"\n")
#do the same for the tenth file
files = open(path + "user-ct-test-collection-10.txt","r")
lines = files.readlines()
files.close()
for j in range(0,len(lines)):
query = lines[j].split("\t")[1]
flag = 0
for string in substrings:
try:
query.index(string)
flag = 1
except ValueError:
continue
if flag == 0:
query = re.sub("[^a-zA-Z0-9 ]", '', query)
query = " ".join(query.split())
if (len(query) != 0):
queries.append(query+"\n")
#remove duplicates by using a set
queries_set = set(queries)
for i in queries_set:
out.write(i)
out.close()