Created
October 19, 2011 14:45
-
-
Save rogerbraun/1298499 to your computer and use it in GitHub Desktop.
Umwandeln von CoMon-HTML in Knoten und Kanten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "nokogiri" | |
require "pry" | |
class NodesAndEdges | |
def initialize(filename) | |
file = open(filename) | |
@doc = Nokogiri::HTML(file) | |
end | |
def nodes | |
@doc.css("td:nth-child(3)").map(&:text).join().gsub("---",";").split(";").map(&:strip).reject{|x| x == ""} << main_node | |
end | |
def nodes_with_index | |
Hash[nodes.map.with_index{|e, i| [e, i]}] | |
end | |
def main_node | |
@doc.css("div div tr:nth-child(7) td:nth-child(2)").text.strip | |
end | |
def edges | |
res = @doc.css("table:nth-child(4) tr") | |
nodes_t = self.nodes_with_index | |
res.shift | |
res = res.inject([]) do |res, el| | |
arr = el.children.map(&:text) | |
if arr[1].strip != "---" | |
arr[2].split(";").map(&:strip).reject{|x| x == ""}.push(main_node).combination(2).each do |left,right| | |
res << [nodes_t[left], nodes_t[right], arr[1], arr[0]] | |
end | |
end | |
res | |
end | |
end | |
def save_nodes_and_edges(n = "nodes.txt", e = "edges.txt") | |
open(n,"w") do |file| | |
file.puts("Label;Id") | |
self.nodes_with_index.each do |arr| | |
file.puts arr.join(";") | |
end | |
end | |
open(e,"w") do |file| | |
file.puts("Source;Target;Weight;Label;Type") | |
self.edges.each do |arr| | |
file.puts arr.join(";") + ";Undirected" | |
end | |
end | |
end | |
end | |
n = NodesAndEdges.new(ARGV[0]) | |
n.save_nodes_and_edges |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment