Improves handling of non-fiction classification data.
[quanlib.git] / classify / classset.rb
1 require 'csv'
2
3 require 'bookclass'
4
5 class ClassSet
6   @@class_csv_file = 'class.csv'
7
8   def initialize
9     @entries = {}
10     load!(@@class_csv_file)
11   end
12
13   def add!(info)
14     key = construct_key(info.grouping, info.title)
15     @entries[key] = info
16   end
17
18   def construct_key(author_grouping, title)
19     author_grouping.to_s + '|' + title.to_s
20   end
21
22   def get(author_grouping, title)
23     key = construct_key(author_grouping, title)
24     if @entries.has_key?(key)
25       return @entries[key]
26     else
27       return nil
28     end
29   end
30
31   def has_key?(author_grouping, title)
32     @entries.has_key?(construct_key(author_grouping, title))
33   end
34
35   def ensure_contains!(info)
36     if ! has_key?(info.grouping, info.title)
37       add!(info)
38     end
39   end
40
41   def inspect 
42     data = []
43
44     if nil != @entries 
45       data.push('entries=' + @entries.inspect + '') 
46     end
47
48     return '(ClassSet:' + data.join(',') + ')'
49   end
50
51   def load!(file_name)
52     first = true
53     @entries = {}
54
55     if ! File.exist?(file_name)
56       puts 'WARNING:  file "' + file_name + '" not found.'
57       return 
58     end
59
60     File.open(file_name, 'r:UTF-8') do |fd|
61       csv = CSV.new(fd)
62       csv.to_a.each do |row|
63         if first
64           first = false
65         elsif row.length >= 6
66           ddc = row[0]
67           lcc = row[1]
68           grouping = row[2]
69           author = row[3]
70           filename = row[4]
71           title = row[5]
72           fast = []
73           if nil != row[6]
74             fast = row[6].split(';')
75           end
76   
77           bookclass = BookClass.new(grouping, title)
78           bookclass.ddc = ddc
79           bookclass.lcc = lcc
80           bookclass.author = author
81           bookclass.filename = filename
82   
83           fast.each do |id|
84             bookclass.add_fast(id)
85           end
86   
87           key = construct_key(grouping, title)
88           @entries[key] = bookclass
89
90           #puts 'LOADED[' + key.inspect + ']: ' + bookclass.inspect
91         end
92       end
93     end
94   end
95
96   def save(file_name)
97     CSV.open(file_name, 'w:UTF-8') do |csv|
98       csv << ['Dewey', 'LCC', 'Grouping', 'Author', 'Filename', 'Title', 'FAST']
99
100       @entries.keys.sort.each do |key|
101         info = @entries[key]
102
103         #puts 'SAVING[' + key.inspect + ']: ' + info.inspect
104
105         ddc = info.ddc
106         lcc = info.lcc
107         grouping = info.grouping
108         author = info.author
109         filename = info.filename
110         title = info.title
111         fast = info.fast.join(';')
112         
113         csv << [ ddc, lcc, grouping, author, filename, title, fast ]
114       end
115     end
116   end
117
118   def save_state
119     save(@@class_csv_file)
120   end
121 end
122