# The Open Office Thesaurus object. Create a new object with idx file and # main database file name and search words with "lookup". # Works with OO Thesaurus files 2.x class OOThesaurus def initialize(idx,dat) @idx, @dat = idx,dat end def lookup(word) seek=-1 ret=[] if File::exist?(@idx) && File::exist?(@dat) open(@idx,"r").each { |f| f.each { |line| line=line.split("|") if line[0]==word seek=line[1].to_i break end } } ret=[] if seek!=-1 open(@dat,"r") { |a| a.seek(seek) data=a.gets.split("|") data[1].to_i.times { ret << a.gets.strip.split("|") } } end end ret end end # A simple GTK2 frontend require 'gtk2' def back if word=$history.pop then $box.text=word dosearch(false) end end def dosearch(add) if add then $history.push($word) end $word=$box.text $itemslist.clear rt=$the.lookup($word) if rt.length>0 rt.each { |ent| ent.each_with_index { |line,i| a=$itemslist.append a[0]=(i!=0 ? "\t" : "" )+ line } } else a=$itemslist.append a[0]="No hits." end end $word="" $the=OOThesaurus.new("th_it_IT.idx","th_it_IT.dat") $history=[] ($window = Gtk::Window.new).signal_connect("destroy") { Gtk.main_quit } $window.title="OOThesaurus" $window.set_default_size(100,200) $window.add(mainbox=Gtk::VBox.new(false,5)) mainbox.pack_start(sb=Gtk::HBox.new(false,5),false,false) sb.pack_start(Gtk::Label.new("Search"),false,false) sb.pack_start($box=Gtk::Entry.new,true,true) sb.pack_start(backbutton=Gtk::Button.new("Back"),false,false) backbutton.signal_connect("clicked") { back } $box.signal_connect("activate") { dosearch(true) } $items = Gtk::TreeView.new($itemslist = Gtk::ListStore.new(String)) $items.signal_connect("row-activated") { |view, path, column| if iter = view.model.get_iter(path) $box.text=iter[0].strip dosearch(true) end } $items.append_column( Gtk::TreeViewColumn.new("Results",Gtk::CellRendererText.new,:text=>0) ) mainbox.pack_start(Gtk::ScrolledWindow.new.add($items).set_hscrollbar_policy(Gtk::POLICY_AUTOMATIC),true,true) $window.show_all Gtk.main