forked from signal-ai/Signal-1M-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-to-trec.py
50 lines (46 loc) · 1.59 KB
/
convert-to-trec.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
# -- coding: utf-8 --
import json
import sys
import getopt
def usage():
print ("usage: convert-to-trec.py -i inputfile -o outputfile \n\n"+
" inputfile: the original JSONL file of the dataset\n\n"+
"Copyright (c) 2015 by Singal Media Ltd.")
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
if not inputfile:
usage()
sys.exit()
if not outputfile:
usage()
sys.exit()
with open(inputfile) as fin, open(outputfile, 'w') as fout:
for line in fin:
news_article = json.loads(line)
trecdoc = u"<DOC>\n"
trecdoc += u"<DOCNO>{}</DOCNO>\n".format(news_article["id"])
trecdoc += u"<SOURCE>{}</SOURCE>\n".format(news_article["source"])
trecdoc += u"<MEDIATYPE>{}</MEDIATYPE>\n".format(news_article["media-type"])
trecdoc += u"<PUBLISHED>{}</PUBLISHED>\n".format(news_article["published"])
trecdoc += u"{}\n".format(news_article["content"])
trecdoc += u"</DOC>\n"
try: # py27
fout.write(trecdoc.encode('utf-8'))
except TypeError: # py34
fout.write(trecdoc)
if __name__ == "__main__":
main(sys.argv[1:])