ontology_parser 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ontology_parser.rb +86 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a77eb65a8027aefb9950a2d364fc4ed87a6e3982
|
4
|
+
data.tar.gz: d4ac96a3924f05e3037a7f970eaa2a4b7d10bc2b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2c047d01c5cee07f3c0d8ce7638abfbb123a0c058c031dca7e004fc5b5d56518d99b0a240243d8131b82a1fddd0ef873d6eac9d34abf02c5e2dc9503a63cec67
|
7
|
+
data.tar.gz: 6e52cabab33518788e3d29bb58e0d531e7a9a4b0dd058338479b2af2e8d96c377e7090e17abb06e5d2bef64f7b1356b24c059a7282765f65fc6814f1cead8acd
|
@@ -0,0 +1,86 @@
|
|
1
|
+
class OntologyParser
|
2
|
+
require 'rexml/document'
|
3
|
+
include REXML
|
4
|
+
attr_accessor :declaration
|
5
|
+
attr_accessor :subclassof
|
6
|
+
attr_accessor :filename
|
7
|
+
attr_accessor :xmldoc
|
8
|
+
def get_declarations
|
9
|
+
self.declaration=[]
|
10
|
+
self.xmldoc.elements.each('Ontology/Declaration/Class') do |declaration|
|
11
|
+
self.declaration<<declaration.attributes['IRI']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def recursively_get_tree(semi_root_hash_lis, subclassof_hash)
|
16
|
+
if semi_root_hash_lis.nil?
|
17
|
+
return;
|
18
|
+
end
|
19
|
+
for i in 0...semi_root_hash_lis.size
|
20
|
+
if !subclassof_hash[semi_root_hash_lis[i]['name']].nil?
|
21
|
+
semi_root_hash_lis[i]['children']=[]
|
22
|
+
subclassof_hash[semi_root_hash_lis[i]['name']].each do |ele|
|
23
|
+
semi_root_hash_lis[i]['children']<<{'name'=>ele, 'children'=>"",'parent'=>semi_root_hash_lis[i]['name']}
|
24
|
+
end
|
25
|
+
self.recursively_get_tree(semi_root_hash_lis[i]['children'],subclassof_hash)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return semi_root_hash_lis
|
29
|
+
end
|
30
|
+
|
31
|
+
def generate_engine(subclassof_hash)
|
32
|
+
#below will get {"c"=>[["sitea.com", "c"], ["siteb.com", "c"]], "b"=>[["sitec.com", "b"]]}
|
33
|
+
subclassof_hash=subclassof_hash.group_by {|k,v| v}
|
34
|
+
#zheng li yi xia
|
35
|
+
subclassof_hash.each do |k,v|
|
36
|
+
tmp=[]
|
37
|
+
v.each do |lis|
|
38
|
+
tmp<<lis[0] #abandon the 2th element
|
39
|
+
end
|
40
|
+
#make it as {"c"=>["sitea.com", "siteb.com"], "b"=>["sitec.com"]}
|
41
|
+
subclassof_hash[k]=tmp
|
42
|
+
end
|
43
|
+
semi_root_lis=self.find_semi_root()
|
44
|
+
semi_root_hash_list=[]
|
45
|
+
#make the semi_root as Hash List
|
46
|
+
semi_root_lis.each do |ele|
|
47
|
+
semi_root_hash_list<<{"name"=>ele, "parent"=>"null", "children"=>""}
|
48
|
+
end
|
49
|
+
res_without_semi_root=self.recursively_get_tree(semi_root_hash_list,subclassof_hash)
|
50
|
+
things={'name'=>'Things','parent'=>'null','children'=>''}
|
51
|
+
semi_lis=[]
|
52
|
+
res_without_semi_root.each do |ele|
|
53
|
+
semi_lis<<ele
|
54
|
+
end
|
55
|
+
things['children']=semi_lis
|
56
|
+
return [things]
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_subclassof
|
60
|
+
self.subclassof=Hash.new
|
61
|
+
list=[]
|
62
|
+
self.xmldoc.elements.each('Ontology/SubClassOf/Class') do |subclassof|
|
63
|
+
list<<subclassof.attributes['IRI']
|
64
|
+
end
|
65
|
+
list.each_slice(2).each do |each2|
|
66
|
+
self.subclassof[each2[0]]=each2[1]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def initialize(filename)
|
71
|
+
self.filename=filename
|
72
|
+
xmlfile=File.open(self.filename)
|
73
|
+
self.xmldoc=Document.new(xmlfile)
|
74
|
+
end
|
75
|
+
|
76
|
+
def find_semi_root
|
77
|
+
self.declaration-self.subclassof.keys
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_tree
|
81
|
+
self.get_declarations
|
82
|
+
self.get_subclassof
|
83
|
+
require 'json'
|
84
|
+
return JSON.generate(self.generate_engine(self.subclassof))
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ontology_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yu Song
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem that can parse *.owl files and construct a tree and let produce
|
14
|
+
a json string that can let d3js or other js libs to use it
|
15
|
+
email: terrorgeek@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/ontology_parser.rb
|
21
|
+
homepage: http://rubygems.org/gems/hola
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.0.14
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Hola!
|
45
|
+
test_files: []
|