-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess
executable file
·71 lines (62 loc) · 2.23 KB
/
preprocess
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
#!/usr/bin/env ruby
############################################################################
## Preprocesses an input graph of undirected links to a format required by
## graph statistics programs; namely,
##
## 1) each undirected link is converted to a pair of directed links, and
## 2) links are grouped by the source node.
##
## Property 2 ensures that all outgoing links for a given node appear
## in consecutive lines in the output graph file. We don't actually
## have to sort the output lines to achieve property 2, but we do so
## anyway in case it proves useful.
##
## ---------------------------------------------------------------------
## Copyright (C) 2010 The Regents of the University of California.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
############################################################################
require 'ostruct'
require 'optparse'
$options = OpenStruct.new
$options.allow_self_loops = false
opts = OptionParser.new
opts.on("--[no]-self-loops", TrueClass,
"negate the selection criteria") do |v|
$options.allow_self_loops = v
end
begin
ARGV.replace opts.parse(*ARGV)
rescue OptionParser::ParseError
$stderr.puts "ERROR: " + $!.to_s
$stderr.puts opts
exit 1
end
IO.popen("sort -k 1,2n -k 2,3n", "w") do |sort|
ARGF.each do |line|
next if line =~ /^\s*$/ || line =~ /^\#/
if line =~ /^(\d+)\s+(\d+)\s*$/
s, d = $1, $2
if s == d && !$options.allow_self_loops
$stderr.printf "discarding self loop on node %d\n", s
next
end
sort.puts "#{s} #{d}\n#{d} #{s}"
else
$stderr.printf "ERROR: malformed line: %s\n", line
exit 1
end
end
end