-
Notifications
You must be signed in to change notification settings - Fork 1
/
genStairCorpus.py
173 lines (161 loc) · 6.77 KB
/
genStairCorpus.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author : HenrySky
# Date : 2016/1/17
# Function : transform from paper adjalist data to start training corpus.
# Parameter : window size win
# adjalist file fin
# output corpus file fout
import os
import sys
import numpy as np
import argparse
import scipy.sparse
import random
from math import log
decayRate = 0
#Algorithm : http://stackoverflow.com/questions/2140787/select-k-random-elements-from-a-list-whose-elements-have-weights
def weighted_sample(dic, n):
if len(dic) == 0:
return -1
total = sum(dic.values())
items = sorted(dic.items(), key=lambda d: d[1], reverse=True)
i = 0
v, w = items[0]
while n:
x = total * (1 - random.random() ** (1.0/n))
total -= x
while x > w:
x -= w
i += 1
v, w = items[i]
w -= x
yield v
n -= 1
def extractData(filename):
lookupDict = {}
idxList = []
citingList = []
citedList = []
countList = []
with open(filename, "r") as f:
index = 0
while True:
line = f.readline()
if not line:
break
docs = line.strip().split()
if len(docs) > 1:
print("Corrupted data. {} error >1.".format(index))
if docs[0] in lookupDict.keys():
print("Corrupted data. {} line error existed.".format(index))
lookupDict[docs[0]] = index
idxList.append(docs[0])
f.readline()
f.readline()
index += 1
with open(filename, "r") as f:
index = 0
while True:
line = f.readline()
if not line:
break
citingLine = [lookupDict[z] for z in f.readline().strip().split()]
citedLine = [lookupDict[z] for z in f.readline().strip().split()]
citedList.append(citedLine)
citingList.append(citingLine)
countList.append(len(citingLine)+len(citedLine))
index += 1
return idxList, citedList, citingList, countList
def outputStairData(idxList, citedList, citingList, countList, fout, win, iterate):
#print(lookupDic, adjaList)
global decayRate
consideredDocs = set()
learningWeights = np.zeros((len(countList)), dtype=int)
contextWeights = []
# iterList = [iterate * 3, int(iterate * 2.5), int(iterate * 2)]
oneProbability = np.ones((len(countList)), dtype=float)
for i in range(len(idxList)):
contextWeights.append({i: 1})
with open(fout, "w") as f:
for i in range(len(idxList)):
if i % 100 == 0:
print("\r%%%.2f"%(100.0*i/len(idxList)), end="")
#distant = np.zeros(len(idxList))
# drop later
buildSet = {i}
froutiers = {i}
for w in range(win):
newFrontier = set()
for froutier in froutiers:
if contextWeights[i][froutier]/(countList[froutier]+1) < 1e-6:
continue
for k in citingList[froutier]:
if k in contextWeights[i].keys():
# it doesn't matter if first window is decayed, for the relative not the absolute value is considered.
contextWeights[i][k] += decayRate * contextWeights[i][
froutier]/countList[froutier]
# contextWeights[i][k] += contextWeights[i][froutier] * decayRate
else:
#distant[k] = w + 1
contextWeights[i][k] = decayRate * contextWeights[i][
froutier]/countList[froutier]
# contextWeights[i][k] = contextWeights[i][froutier] * decayRate
for k in citedList[froutier]:
if k in contextWeights[i].keys():
contextWeights[i][k] += decayRate * contextWeights[i][
froutier]/countList[froutier]
# contextWeights[i][k] += contextWeights[i][froutier] * decayRate
else:
#distant[k] = w + 1
contextWeights[i][k] = decayRate * contextWeights[i][
froutier]/countList[froutier]
# contextWeights[i][k] = contextWeights[i][froutier] * decayRate
newFrontier = newFrontier.union(citedList[froutier])
newFrontier = newFrontier.union(citingList[froutier])
# print("newFroutier : ", newFrontier)
froutiers = newFrontier.difference(buildSet)
if len(froutiers) == 0:
continue
buildSet = buildSet.union(froutiers)
# consideredDocs.add(i)
for idx in weighted_sample(contextWeights[i], iterate):
f.write(idxList[i] #+ " " + str(countList[con])
# + " " + str(1)
+ " " + idxList[idx] + "\n")
'''
for con in buildSet:
if con == i:
pass
f.write(idxList[i] + " " + str(countList[con])
+ " " + str(1)
+ " " + idxList[con] + "\n")
else:
temp = contextWeights[i].pop(con)
for j in range(temp):
f.write(idxList[i] #+ " " + str(countList[con])
#+ " " + str(tempi)
+ " " + idxList[con] + "\n")
'''
def process(win, fin, fout, iterate):
print("Extract adjalist...");
idxList, citedList, citingList, countList = extractData(fin)
print("Output corpus...");
outputStairData(idxList, citedList, citingList, countList, fout, win, iterate)
def parseArg():
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--window", help="the step window of citation linkage context.",
type=int, default=3)
parser.add_argument("-fin", "--input", help="Input adjalist filename.",
required=True)
parser.add_argument("-fout", "--output", help="Output corpus filename.",
required=True)
parser.add_argument("-decay", "--decay", help="Decay factor for further nodes.", type=float,
default=1.0)
parser.add_argument("-iter", "--iterate", help="times starting from each node.", type=int, default=200)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parseArg()
decayRate = args.decay
process(args.window, args.input, args.output, args.iterate)