-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataPrepare.py
95 lines (86 loc) · 3.05 KB
/
dataPrepare.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 4 20:53:51 2018
@author: tianle
"""
def convert2TSV(dataset="/QA1",type='train'):
lines=[]
with open(dataset+'/' + type + '.txt', 'r', encoding='utf-8') as f:
ss=f.readlines()
for s in ss:
item=s.strip().split(' ')
qid=item[0]
term=item[1]
label=item[2]
query1=item[4:]
query=" ".join(query1)
qlen=len(query1)
#print(query)
line= "\t".join((qid,str(qlen),query, term, label))
lines.append(line)
print(line)
filename= dataset+"/original/term-" + type +".tsv"
with open(filename, "w",encoding='utf-8') as f:
print(filename)
f.write("\n".join(lines) )
return filename
def qid(dataset = "/QA1",type = 'train' ):
with open(dataset+'/qeo-'+type+'.txt', 'r', encoding='utf-8') as f:
ss=f.readlines()
number=[]
for s in ss:
item=s.strip().split(" ")
num=len(item)-1
number.append(num)
return number
def convert2TSVtrue(dataset="/QA1",type='train'):
lines=[]
with open(dataset+'/'+type+'.txt', 'r', encoding='utf-8') as f:
ss=f.readlines()
for s in ss:
item=s.strip().split(' ')
qid=item[0]
term=item[1]
label=item[2]
query1=item[4:]
query=" ".join(query1)
qlen=len(query1)
#print(query)
if(label=="1"):
line= "\t".join((qid,str(qlen),query, term, label))
lines.append(line)
filename= dataset+"/original/groundTruth-"+type+".tsv"
with open(filename, "w",encoding='utf-8') as f:
print(filename)
f.write("\n".join(lines) )
return filename
def format_file(dataset="QA1",type1="train",type2='term-'):
filename=dataset+"/original/"+type2+type1+".tsv"
temp_file=dataset+"/qe/"+type2+type1
print(temp_file)
with open(filename,'r',encoding='utf-8') as f, open(temp_file,"w",encoding='utf-8') as out:
for index, line in enumerate(f):
qid,qlen,query,term,label=line.strip().split("\t")
newline="%s %s qid:%s" %(label,qlen,qid)
tokens1=query.split()+term.split()
fill=max(0,20-len(tokens1))
tokens1.extend(['<a>']*fill)
newline+=" "+"_".join( tokens1)+"_"
tokens2 = term.split()
fill = max(0, 20 - len(tokens2))
tokens2.extend([term] * fill)
newline += " " + "_".join(tokens2) + "_"
out.write(newline + "\n")
return temp_file
if __name__ == "__main__":
dataset='../WebAP'
type1='test'
type2='term-'
type22='groundTruth-'
convert2TSV(dataset,type1)
convert2TSVtrue(dataset,type1)
format_file(dataset,type1,type2)
format_file(dataset,type1,type22)
#number=qid(dataset="WebAP")
#print(number)
print(dataset+' dataset was done.')