-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_persons.rb
169 lines (147 loc) · 4.32 KB
/
read_persons.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require 'json'
require './pnv_person.rb'
def find_property value
result = @properties[value.downcase]
return result if !result.nil?
value
end
def do_components elems,indent
name = Pnv_person.new
elems.each do |elem|
type = find_property elem['type']
value = elem['value']
name.add type,value
end
name.to_h.each do |name_part,value|
puts "#{get_indent indent}<pnv:#{name_part}>#{value}</pnv:#{name_part}>"
end
puts "#{get_indent indent}<pnv:literalName>#{name.get 'literalName'}</pnv:literalName>"
end
def floruit elem,indent
puts "#{get_indent indent}<schema:floruit rdf:parseType=\"Resource\">"
indent += 1
elem
text = elem[1..-2]
while md = text.match(/"([^^"]*)":"([^^"]*)",?/)
# STDERR.puts "#{md[1]}: #{md[2]}"
puts "#{get_indent indent}<schema:#{md[1]} rdf:datatype=\"http://www.w3.org/2001/XMLSchema#date\">#{md[2]}</schema:#{md[1]}>"
text = md.post_match
end
indent -= 1
puts "#{get_indent indent}</schema:floruit>"
end
def parse_elem k,v,indent=1
# STDERR.puts "#{k}: #{v} (#{v.class})" if k.eql?("floruit")
k = k[1..-1] if k[0..0].eql?("^")
k = k[1..-1] if k[0..0].eql?("@")
if v.class.eql?(Array)
puts "#{get_indent indent}<schema:#{k}>"
puts "#{get_indent(indent + 1)}<rdf:Seq>"
v.each do |va|
parse_elem k[0..-2],va,indent + 2
end
puts "#{get_indent(indent + 1)}</rdf:Seq>"
puts "#{get_indent indent}</schema:#{k}>"
elsif v.class.eql?(Hash)
if k.eql?("name")
puts "#{get_indent indent}<pnv:name rdf:parseType=\"Resource\">"
else
puts "#{get_indent indent}<schema:#{k} rdf:parseType=\"Resource\">"
end
v.each do |ka,va|
if ka.eql?("components")
do_components va,indent + 1
else
parse_elem ka,va,indent + 1
end
end
if k.eql?("name")
puts "#{get_indent indent}</pnv:name>"
else
puts "#{get_indent indent}</schema:#{k}>"
end
else
if k.eql?("floruit")
floruit v,indent
elsif k.downcase.match(/date$/)
# birthDate deathDate
puts "#{get_indent indent}<schema:#{k} rdf:datatype=\"http://www.w3.org/2001/XMLSchema#date\">#{v}</schema:#{k}>" unless k.eql?("@type")
else
# rdf:parseType=\"Literal\"
puts "#{get_indent indent}<schema:#{k}>#{v}</schema:#{k}>" unless k.eql?("@type")
if k.eql?("displayName")
puts "#{get_indent indent}<schema:name>#{v}</schema:name>"
end
end
end
end
def get_indent indent
" " * indent
end
def scrape_line line
result = Array.new
teller = 1
array = JSON.parse(line)
array.each do |obj|
# STDERR.puts JSON.pretty_generate(obj)
type =<<EOF
<rdf:Description rdf:about="https://resource.huygens.knaw.nl/ww/#{obj['@type']}/#{obj['_id']}">
<rdf:type rdf:resource="https://resource.huygens.knaw.nl/ww/#{obj['@type']}" />
EOF
puts type
obj.each do |k,v|
parse_elem k,v
end
puts "</rdf:Description>"
puts
teller += 1
# break if teller > 1
end
return !line.eql?("[]")
end
if __FILE__ == $0
@debug = false
@properties = {
"forename" => "givenName",
"name_link" => "surnamePrefix",
"surname" => "baseSurname",
"add_name" => "trailingPatronym",
"role_name" => "prefix",
"gen_name" => "givenNameSuffix"
}
inputfile = "persons.json"
begin
(0..(ARGV.size-1)).each do |i|
case ARGV[i]
when '--debug'
@debug = true
when '-f'
inputfile = ARGV[i+1]
when '-h'
STDERR.puts "use: ruby read_persons.rb [--debug] [-f inputfile]"
exit(1)
end
end
rescue => detail
STDERR.puts "#{detail}"
end
header =<<EOF
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:schema="http://schema.org/"
xmlns:pnv="http://www.purl.org/pnv/"
xmlns:ww="https://resource.huygens.knaw.nl/women_writers/ww"
xmlns:person="https://resource.huygens.knaw.nl/women_writers/person">
EOF
puts header
File.open(inputfile) do |file|
while line = file.gets
line.force_encoding(Encoding::UTF_8)
if !line.strip.empty?
scrape_line line
end
end
end
puts "</rdf:RDF>"
end