-
Notifications
You must be signed in to change notification settings - Fork 2
/
walk.py
91 lines (81 loc) · 2.51 KB
/
walk.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
# -*- coding: UTF-8 -*-
from numpy import *
import operator
import sys;
import re
from os import listdir
#random
import random
from random import shuffle
#word2vec
from gensim import corpora, models, similarities
from gensim import utils, matutils
from gensim.models import word2vec
from gensim import *
#
def loadGraphDict(fileName):
dataDict = {}
fx = open(fileName)
for line in fx.readlines()
lineArr = re.split(' |,',line.strip())
lenth=len(lineArr)
if(lenth<2):
break;
if(lineArr[0] not in dataDict.keys()):
dataDict[str(lineArr[0])]=[str(lineArr[1])]
if(lineArr[1] not in dataDict[str(lineArr[0])]):
dataDict[str(lineArr[0])].append(str(lineArr[1]))
if(lineArr[1] not in dataDict.keys()):
dataDict[str(lineArr[1])]=[str(lineArr[0])]
if(lineArr[0] not in dataDict[str(lineArr[1])]):
dataDict[str(lineArr[1])].append(str(lineArr[0]))
return dataDict
#
def walk(graphDict,num_paths,walkfile):
walkList=[]
f1=open(walkfile,'a+')
for j in range(int(num_paths)):
nodes=list(graphDict.keys())
random.Random(0).shuffle(nodes)
for keyStartNode in nodes:
walkList.append(keyStartNode)
nowLength=1
RandomWalk(graphDict,walkList,keyStartNode,nowLength)
for e in walkList:
f1.write('%s '%(e))
f1.write('\n');
#print 'walkList=',walkList
walkList=[]
f1.close()
return walkList
#2.1
def RandomWalk(graphDict,walkList,nowNode,nowLength,walkLength=40):
if(nowLength>=walkLength):
return 0
nowNode=random.Random().choice(graphDict[nowNode])
walkList.append(nowNode)
RandomWalk(graphDict,walkList,nowNode,nowLength+1)
#START main function
def start(father,numpaths):
#father="data/"
xDict=loadGraphDict('data/ytb.txt')
#print "xDict=",xDict
yDict=loadGraphDict('data/y2.txt')
#print "yDict=",yDict
while(numpaths!='stop'):
#
try:
numpaths=int(numpaths)
except EXception:
break;
walkListX=walk(xDict,numpaths,walkfile=father+'walkListX.txt')
walkListY=walk(yDict,numpaths,walkfile=father+'walkListY.txt')
print "Please give the num of paths"
numpaths = raw_input()
if __name__=="__main__":
nLen = len(sys.argv);
for i in range(0, nLen):
print("argv %d:%s" %(i, sys.argv[i]));
father=str(sys.argv[1])
numpaths=int(sys.argv[2])
start(str(father),int(numpaths))