File size: 2,056 Bytes
8dc315b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import os
import sys
os.environ["PYTHONHASHSEED"] = "10"


def clean(ifilename, ofilename, err_ofilename):
    line_cnt, err_cnt = 0, 0
    with open(ifilename) as ifile:
        with open(ofilename, "w") as ofile:
            with open(err_ofilename, "w") as ofile_err:
                for line in ifile:
                    line_cnt += 1
                    line_split = line.split(",")
                    if len(line_split) != 4:
                        err_cnt += 1
                        ofile_err.write("{}".format(line))
                        print("line {} err {} err rate {:.4f}".format(line_cnt, line_split, err_cnt/line_cnt))
                        continue
                    if '"' in line_split[2]:
                        err_cnt += 1
                        # if 'monobook/null?"{' not in line_split[2]:
                        #     print("line {} found quotation in url {} - err rate {:.4f}".format(line_cnt, line_split[2], err_cnt/line_cnt))
                        ofile_err.write("{}".format(line))
                        continue
                    ofile.write("{},{}\n".format(int(float(line_split[1])), line_split[2]))
                    if line_cnt % 200000000 == 0:
                        print("line {} err {}".format(line_cnt, err_cnt/line_cnt))

def conv(ifilename, ofilename):
    ifile = open(ifilename, "r")
    ofile = open(ofilename, "w")

    start_ts = -1
    last_ts = 0
    for line in ifile:
        line_split = line.split(",")
        if len(line_split) != 2:
            print(line_split)
            continue
        if start_ts == -1:
            start_ts = int(line_split[0])
        
        ts = int(line_split[0]) - start_ts 
        if ts < last_ts:
            ts = last_ts
        last_ts = ts
        ofile.write("{},{}\n".format(ts, hash(line_split[1])))

    ifile.close()
    ofile.close()


if __name__ == "__main__":
    # clean("wiki.all.csv.sort.clean", "wiki.all.csv.sort.clean.2", "wiki.all.csv.sort.clean.err")
    conv("wiki.all.csv.sort.clean.2", "wiki.all.csv.sort.clean.hash")