-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_comments.rb
108 lines (93 loc) · 2.46 KB
/
load_comments.rb
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
require 'json'
require_relative "CommentNode.rb"
@count =0
def loadcomments(file_name,sub,subreddits,comment_nodes,map)
f = File.open(file_name,"r")
str = ""
f.each{|line| str+=str+line}
j=JSON.load(str)
post_1 = j[sub][0]
post_2 = j[sub][1]
post_3 = j[sub][2]
grab_link = Proc.new {
|comment|
@count+=1
if(comment =~ /r\/([a-zA-Z_0-9]+)/)
m=/r\/([a-zA-Z0-9_]+)/.match(comment)
to_sub = m[1].downcase
if(subreddits.include?(to_sub) and to_sub!=sub.downcase )
comment_node= CommentNode.new(comment,sub.downcase,to_sub)
if(map[sub.downcase] == nil)
comment_nodes<<comment_node
map[sub.downcase]= Hash.new
map[sub.downcase][to_sub] = true
elsif(map[sub.downcase][to_sub] == nil)
comment_nodes<<comment_node
map[sub.downcase][to_sub] = true
end
end
end
}
post_1.each{|comment| grab_link.call(comment)}
post_2.each{|comment| grab_link.call(comment)}
post_2.each{|comment| grab_link.call(comment)}
f.close
end
def test_file(file_name, subs)
f = File.open(file_name, "r")
str = ""
f.each{|line| str+=str+line}
j=JSON.load(str)
check_exists = Proc.new{|sub| if j[sub] ==nil then puts sub end}
subs.each{
|sub|
check_exists.call(sub)
}
end
def get_subreddits()
subreddits= []
Dir.foreach('./json_files') do |file_name|
next if file_name == '.' or file_name == '..'
total_file_path = "./json_files/"+file_name
matcher = m=/([a-zA-Z0-9_]+)_json/.match(file_name)
sub_name = m[1].downcase
subreddits<<sub_name
end
return subreddits
end
def load_all()
subreddits = get_subreddits
comment_nodes = []
map = Hash.new
Dir.foreach('./json_files') do |file_name|
next if file_name == '.' or file_name == '..'
total_file_path = "./json_files/"+file_name
matcher = m=/([a-zA-Z0-9_]+)_json/.match(file_name)
sub_name = m[1]
loadcomments(total_file_path, sub_name,subreddits,comment_nodes,map)
end
return comment_nodes
end
def print_contents(comment_nodes)
comment_nodes.each{
|node|
puts "From: #{node.from} To: #{node.to}"
}
end
def print_out_edges(comment_nodes)
visited =[]
comment_nodes.each{
|comment_node|
to = comment_node.to
if(!visited.include?(to))
visited<<to
end
}
subreddits = get_subreddits()
subreddits.each{|sub| if(!visited.include?(sub)) then puts sub end}
end
#test_file("./json_files/pics_json", j_0)
# loadcomments("./json_files/RealGirls_json","bicycling")
comment_nodes = load_all()
#print_out_edges(comment_nodes)
# print_out_edges(comment_nodes)