- @fname = File.join(@dir,"factoids.rbot")
- if File.exist?(@fname)
- @factoids = File.readlines(@fname)
- @factoids.each { |l| l.chomp! }
- else
- # A Set, maybe?
- @factoids = Array.new
+ @filename = "factoids.rbot"
+ @factoids = FactoidList.new
+ read_factfile
+ @changed = false
+ end
+
+ def read_factfile(name=@filename,dir=@dir)
+ fname = File.join(dir,name)
+ if File.exist?(fname)
+ factoids = File.readlines(fname)
+ return if factoids.empty?
+ firstline = factoids.shift
+ pattern = firstline.chomp.split(" | ")
+ if pattern.length == 1 and pattern.first != "fact"
+ factoids.unshift(firstline)
+ factoids.each { |f|
+ @factoids << Factoid.new( :fact => f.chomp )
+ }
+ else
+ pattern.map! { |p| p.intern }
+ raise ArgumentError, "fact must be the last field" unless pattern.last == :fact
+ factoids.each { |f|
+ ar = f.chomp.split(" | ", pattern.length)
+ @factoids << Factoid.new(Hash[*([pattern, ar].transpose.flatten)])
+ }
+ end