Method: Pho::FileManagement::FileSplitter#split_file

Defined in:
lib/pho/upload.rb

#split_file(filename, format = :ntriples) ⇒ Object

Split a single file, in any parseable RDF format into smaller chunks of ntriples. Chunked files are stored in default temporary directory for this instance

filename:: name of the file to split
format:: input format, default is :ntriples


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
# File 'lib/pho/upload.rb', line 71

def split_file(filename, format=:ntriples)
  
  basename = File.basename(filename, ".#{filename.split(".").last}")
  count = 0
  stmts = []
  RDF::Reader.for(format).new(File.new(filename)) do |reader|
    reader.each_statement do |statement|            
      count += 1
      stmts << @handler.handle( statement ) 
      if count % @triples == 0
        RDF::Writer.open( File.join(@dir, "#{basename}_#{count}.nt") ) do |writer|
          stmts.each do |s|
            writer << s
          end
        end              
        stmts = []              
      end
    end
  end
  if !stmts.empty?
    RDF::Writer.open( File.join(@dir, "#{basename}_#{count}.nt") ) do |writer|
      stmts.each do |s|
        writer << s
      end
    end
  end
end