-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_convert.rb
More file actions
55 lines (41 loc) · 1.09 KB
/
data_convert.rb
File metadata and controls
55 lines (41 loc) · 1.09 KB
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
require 'nokogiri'
require 'pry'
require 'json'
require 'xmlsimple'
class DataConvert
def initialize
t = Time.now
refactored_hash = []
File.foreach("/media/dilum/96648732-125e-4b47-aa00-74d25da99130/Data/Posts.xml").with_index do |line, line_num|
noko = Nokogiri::XML(line)
if line.include? "Tags"
hash = {}
hash['Id'] = noko.at("row")['Id']
hash['CreationDate'] = noko.at("row")['CreationDate']
hash['Tags'] = noko.at("row")['Tags']
hash['AcceptedAnswerId'] = noko.at("row")['AcceptedAnswerId']
refactored_hash << hash
hash = nil
end
noko = nil
p line_num
end
puts Time.now - t
File.write('/media/dilum/96648732-125e-4b47-aa00-74d25da99130/Data/Posts.json', JSON.pretty_generate(refactored_hash))
puts Time.now - t
end
def removeAnswers(hash)
refactored_hash = []
puts hash["row"].size
hash["row"].each_with_index do |element, index|
if element.key?("Tags")
element.delete("Title")
element.delete("Body")
refactored_hash << element
end
end
puts refactored_hash.size
refactored_hash
end
end
data = DataConvert.new