Created
October 2, 2010 20:45
-
-
Save alienrobotwizard/607973 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module GMLParser | |
class Graph < Struct.new( | |
:nodes, | |
:edges | |
) | |
def edge_list | |
self.edges.map{|e| [e[:source], e[:target], e[:value]]} | |
end | |
end | |
class Parser | |
def self.parse gml_data | |
graph = gml_data.gsub(/\n/, '').scan(rgxp[:graph]).inject([]) do |g, s| | |
g << s.scan(rgxp[:node]).map do |n| | |
{ | |
:id => n.match(rgxp[:id]).captures.first, | |
:label => n.match(rgxp[:label]).captures.first | |
} | |
end | |
g << s.scan(rgxp[:edge]).map do |e| | |
{ | |
:source => e.match(rgxp[:source]).captures.first, | |
:target => e.match(rgxp[:target]).captures.first, | |
:value => e.match(rgxp[:value]).captures.first | |
} | |
end | |
g | |
end | |
Graph.new(*graph) | |
end | |
def self.rgxp | |
{ | |
:graph => /graph.*\[[^\[\]]+\]/, | |
:edge => /edge\W+\[[^\[\]]+\]/, | |
:node => /node\W+\[[^\[\]]+\]/, | |
:id => /id\W+([A-Za-z0-9]+)/, | |
:label => /label\W+([A-Za-z0-9]+)/, | |
:source => /source\W+([A-Za-z0-9]+)/, | |
:target => /target\W+([A-Za-z0-9]+)/, | |
:value => /value\W+([A-Za-z0-9]+)/ | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment