Sha256: 2d260658646aeb7b34403dbe01555dc29436db3fa975fa455f9472229b8933ed

Contents?: true

Size: 1.18 KB

Versions: 11

Compression:

Stored size: 1.18 KB

Contents

require 'ankusa/memory_storage'

module Ankusa

  class FileSystemStorage < MemoryStorage

    def initialize(file)
      @file = file
      init_tables
    end

    def reset
      @freqs = {}
      @total_word_counts = Hash.new(0)
      @total_doc_counts = Hash.new(0)
      @klass_word_counts = {}
      @klass_doc_counts = {}
    end
    
    def drop_tables
      File.delete(@file) rescue Errno::ENOENT
      reset
    end

    def init_tables
      data = {}
      begin
        File.open(@file) do |f|
          data = Marshal.load(f)
        end
        @freqs = data[:freqs]
        @total_word_counts = data[:total_word_counts]
        @total_doc_counts = data[:total_doc_counts]
        @klass_word_counts = data[:klass_word_counts]
        @klass_doc_counts = data[:klass_word_counts]
      rescue Errno::ENOENT
        reset
      end
    end

    def save(file = nil)
      file ||= @file
      data = { 	:freqs => @freqs,
		:total_word_counts => @total_word_counts,
		:total_doc_counts => @total_doc_counts,
		:klass_word_counts => @klass_word_counts,
		:klass_doc_counts => @klass_doc_counts }
      File.open(file, 'w+') do |f|
        Marshal.dump(data, f)
      end
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ankusa-0.1.1 lib/ankusa/file_system_storage.rb
ankusa-0.1.0 lib/ankusa/file_system_storage.rb
ankusa-0.0.16 lib/ankusa/file_system_storage.rb
ankusa-0.0.15 lib/ankusa/file_system_storage.rb
ankusa-0.0.14 lib/ankusa/file_system_storage.rb
ankusa-0.0.13 lib/ankusa/file_system_storage.rb
ankusa-0.0.12 lib/ankusa/file_system_storage.rb
ankusa-0.0.11 lib/ankusa/file_system_storage.rb
ankusa-0.0.10 lib/ankusa/file_system_storage.rb
ankusa-0.0.9 lib/ankusa/file_system_storage.rb
ankusa-0.0.8 lib/ankusa/file_system_storage.rb