null+****@clear*****
null+****@clear*****
Thu Jan 12 17:50:02 JST 2012
SUZUKI Miho 2012-01-12 17:50:02 +0900 (Thu, 12 Jan 2012) New Revision: 29c1a7200ac9e4b8e575a132c479d65b96ac22fc Merged fd38da4: Merge pull request #17 from logaling/improve-lookup-order Log: [#11] improve order when lookup Modified files: lib/logaling/glossary_db.rb lib/logaling/repository.rb Modified: lib/logaling/glossary_db.rb (+15 -5) =================================================================== --- lib/logaling/glossary_db.rb 2012-01-10 10:15:40 +0900 (6a9119e) +++ lib/logaling/glossary_db.rb 2012-01-12 17:50:02 +0900 (52996db) @@ -65,11 +65,21 @@ module Logaling end end - def lookup(source_term) - records_raw = Groonga["glossaries"].select do |record| - record.source_term =~ source_term + def lookup(source_term, source_language, target_language, glossary) + records_selected = Groonga["glossaries"].select do |record| + conditions = [record.source_term =~ source_term] + conditions << (record.source_language =~ source_language) if source_language + conditions << (record.target_language =~ target_language) if target_language + conditions end - records = records_raw.sort([ + specified_glossary = records_selected.select do |record| + record.name == glossary + end + specified_glossary.each do |record| + record.key._score += 10 + end + records = records_selected.sort([ + {:key=>"_score", :order=>'descending'}, {:key=>"name", :order=>'ascending'}, {:key=>"source_term", :order=>'ascending'}, {:key=>"target_term", :order=>'ascending'}]) @@ -77,7 +87,7 @@ module Logaling options = {:width => 100, :html_escape => true, :normalize => true} - snippet = records_raw.expression.snippet(["<snippet>", "</snippet>"], options) + snippet = records_selected.expression.snippet(["<snippet>", "</snippet>"], options) snipped_source_term = [] records.map do |record| Modified: lib/logaling/repository.rb (+1 -9) =================================================================== --- lib/logaling/repository.rb 2012-01-10 10:15:40 +0900 (f430e77) +++ lib/logaling/repository.rb 2012-01-12 17:50:02 +0900 (2dc7a98) @@ -55,15 +55,7 @@ module Logaling terms = [] Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db| - terms = db.lookup(source_term) - terms.delete_if{|term| term[:source_language] != source_language } if source_language - terms.delete_if{|term| term[:target_language] != target_language } if target_language - if glossary && !terms.empty? - # order by glossary - specified = terms.select{|term| term[:name] == glossary} - other = terms.select{|term| term[:name] != glossary} - terms = specified.concat(other) - end + terms = db.lookup(source_term, source_language, target_language, glossary) end terms end