X-Git-Url: http://jaekl.net/gitweb/?p=quanlib.git;a=blobdiff_plain;f=classify%2Fclassset.rb;fp=classify%2Fclassset.rb;h=710db7a3c89633c1cc7afbd28839de91b3ecdd59;hp=0000000000000000000000000000000000000000;hb=2c6d69af97c152524366d3fefe1808dfb78f8f56;hpb=fcaeedd4d1c128ff84371c0a7db5d0af6751492a diff --git a/classify/classset.rb b/classify/classset.rb new file mode 100644 index 0000000..710db7a --- /dev/null +++ b/classify/classset.rb @@ -0,0 +1,120 @@ +require 'csv' + +require 'bookclass' + +class ClassSet + @@class_csv_file = 'class.csv' + + def initialize + @entries = {} + load!(@@class_csv_file) + end + + def add!(info) + key = construct_key(info.grouping, info.title) + @entries[key] = info + end + + def construct_key(author_grouping, title) + author_grouping.to_s + '|' + title.to_s + end + + def get(author_grouping, title) + key = construct_key(author_grouping, title) + if @entries.has_key?(key) + return @entries[key] + else + return nil + end + end + + def has_key?(author_grouping, title) + @entries.has_key?(construct_key(author_grouping, title)) + end + + def ensure_contains!(info) + if ! has_key?(info.grouping, info.title) + add!(info) + end + end + + def inspect + data = [] + + if nil != @entries + data.push('entries=' + @entries.inspect + '') + end + + return '(ClassSet:' + data.join(',') + ')' + end + + def load!(file_name) + first = true + @entries = {} + + if ! File.exist?(file_name) + puts 'WARNING: file "' + file_name + '" not found.' + return + end + + File.open(file_name, 'r:UTF-8') do |fd| + csv = CSV.new(fd) + csv.to_a.each do |row| + if first + first = false + elsif row.length >= 6 + ddc = row[0] + lcc = row[1] + grouping = row[2] + author = row[3] + title = row[4] + fast = [] + if nil != row[5] + fast = row[5].split(';') + end + + bookclass = BookClass.new(grouping, title) + bookclass.ddc = ddc + bookclass.lcc = lcc + bookclass.author = author + + fast.each do |id| + bookclass.add_fast(id) + end + + key = construct_key(grouping, title) + @entries[key] = bookclass + end + end + end + end + + def save(file_name) + CSV.open(file_name, 'w:UTF-8') do |csv| + csv << ['Dewey', 'LCC', 'Grouping', 'Author', 'Title', 'FAST'] + + @entries.keys.sort.each do |key| + info = @entries[key] + + ddc = info.ddc + lcc = info.lcc + grouping = info.grouping + author = info.author + title = info.title + fast_list = info.fast + fast_ids = [] + fast_list.each do |tuple| + fast_ids.push(tuple[0]) + end + fast = fast_ids.join(';') + + csv << [ ddc, lcc, grouping, author, title, fast ] + end + end + end + + def save_state + save(@@class_csv_file) + end +end +