forked from WardCunningham/search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneo-batch.rb
87 lines (72 loc) · 1.86 KB
/
neo-batch.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
# export graph as neo4j batch input files, nodes.csv & relations.csv
# ruby neo-batch.rb; neo4j-import --into wiki.db --nodes public/nodes.csv --relationships public/rels.csv
require 'csv'
@id = 0
@aloc = {}
@missing = {}
@nodes = CSV.open("public/nodes.csv", "wb")
@nodes << [':ID','title',':LABEL']
@rels = CSV.open("public/rels.csv", "wb")
@rels << [':START_ID',':END_ID',':TYPE']
def id label, title
key = "#{label}-#{title}"
return @aloc[key] unless @aloc[key].nil?
@id += 1
yield @id if block_given?
@aloc[key] = @id
end
def dotitle slug
id 'Title', slug do |here|
title = slug.gsub(/\w+/, &:capitalize).gsub(/-/,' ')
@nodes << [here, title, 'Title']
end
end
def dopage domain, slug
here = @id += 1
@nodes << [here, slug, 'Page']
there = dotitle slug
@rels << [here, there, 'IS']
links = "sites/#{domain}/pages/#{slug}/links.txt"
if File.exist?(links)
File.readlines(links).each do |link|
there = dotitle link.chomp
@rels << [here, there, 'LINK']
end
end
sites = "sites/#{domain}/pages/#{slug}/sites.txt"
if File.exist?(sites)
File.readlines(sites).each do |site|
there = id 'Site', site.chomp do |id|
@missing[site.chomp] = id
end
@rels << [here, there, 'KNOWS']
end
end
here
end
def dosite domain
pages = "sites/#{domain}/pages"
return unless File.exist?(pages)
here = id 'Site', domain
@nodes << [here, domain, 'Site']
Dir.entries(pages).each do |slug|
next if slug[0] == '.'
there = dopage domain, slug
@rels << [here, there, 'HAS']
end
@missing.delete domain
end
def dofederation
Dir.entries("sites").each do |domain|
next if domain[0] == '.'
dosite domain
end
@missing.each do |domain,id|
@nodes << [id, domain, 'Site']
end
end
dofederation
# dosite 'ward.asia.wiki.org'
# dosite 'forage.ward.fed.wiki.org'
@nodes.close
@rels.close