-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
63 lines (47 loc) · 1.5 KB
/
query.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
"""
process the DRC query data
"""
import os
import json
import sys
import re
import argparse
import codecs
from collections import Counter
PATH_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)),"path.json")
class DRCQuery(object):
def __init__(self,query_path=None):
if query_path:
self._query_path = query_path
else:
path_data = json.load(open(PATH_FILE))
self._query_path = path_data["query"]
self._original_query_data = json.load(open(self._query_path))
self._queries = {}
for single_query in self._original_query_data:
qid = str(single_query["qid"])
query_string = single_query["query"].lower()
self._queries[qid] = query_string
@property
def queries(self):
return self._queries
def count_top_disaster(self,top=5):
words = {}
for qid in self._queries:
for w in re.findall("\w+",self._queries[qid]):
if w not in words:
words[w] = 0
words[w] += 1
c = Counter(words)
print c.most_common(top)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--query_path","-qp")
parser.add_argument("--top","-t",type=int,default=5)
args=parser.parse_args()
drc_queries = DRCQuery(args.query_path)
print drc_queries.queries
print "-"*20
drc_queries.count_top_disaster(args.top)
if __name__=="__main__":
main()