forked from quickwit-oss/search-benchmark-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_queries.py
46 lines (43 loc) · 1.21 KB
/
format_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
import fileinput
import json
import re
import json
import random
LETTERS_ONLY = re.compile("^[a-z ]+$")
PTN = re.compile("\s+")
def generate_queries(words):
tags = ["num_token_%i" % len(words)]
if len(words) > 1:
intersection = " ".join( ("+" + word) for word in words)
yield {
"query": intersection,
"tags": tags + ["intersection", "global"]
}
phrase_query = "\"%s\"" % " ".join(words)
if random.random() < 0.1: # only 10% of queries
yield {
"query": phrase_query,
"tags": tags + ["phrase"]
}
else:
yield {
"query": phrase_query,
"tags": tags
}
union_query = " ".join(words)
yield {
"query": union_query,
"tags": tags + ["union", "global"]
}
for line in fileinput.input():
(count, query) = PTN.split(line.decode("utf-8").strip(), 1)
count = int(count)
if not LETTERS_ONLY.match(query):
continue
words = PTN.split(query)
for q in generate_queries(words):
try:
qdoc = json.dumps(q).encode("utf-8")
print qdoc
except:
pass