null+****@clear*****
null+****@clear*****
Wed Feb 22 17:54:29 JST 2012
SUZUKI Miho 2012-02-22 17:54:29 +0900 (Wed, 22 Feb 2012) New Revision: f49741f7e45f63f4ecf3c3cead9b5627b1191d8f Merged 65d6d6b: Merge pull request #50 from logaling/improve-lookup Log: improve lookup with '--dictionary' option Modified files: lib/logaling/command/application.rb lib/logaling/glossary_db.rb lib/logaling/repository.rb Modified: lib/logaling/command/application.rb (+9 -7) =================================================================== --- lib/logaling/command/application.rb 2012-02-22 10:31:04 +0900 (51782a5) +++ lib/logaling/command/application.rb 2012-02-22 17:54:29 +0900 (b475ef7) @@ -195,15 +195,16 @@ module Logaling::Command desc 'lookup [TERM]', 'Lookup terms.' method_option "output", type: :string, default: "terminal" method_option "no-pager", type: :boolean, default: false + method_option "dictionary", type: :boolean, default: false, aliases: "--dict" def lookup(source_term) @repository.index - terms =****@repos*****(source_term, glossary) + terms =****@repos*****(source_term, glossary, options["dictionary"]) unless terms.empty? max_str_size = terms.map{|term| term[:source_term].size}.sort.last run_pager terms.each_with_index do |term, i| - source_string = extract_source_string_and_coloring(term) - target_string = term[:target_term].bright + source_string = extract_keyword_and_coloring(term[:snipped_source_term], term[:source_term]) + target_string = extract_keyword_and_coloring(term[:snipped_target_term], term[:target_term]) note = term[:note].to_s unless term[:note].empty? glossary_name = "" if****@repos*****_counts > 1 @@ -337,12 +338,13 @@ module Logaling::Command exec pager rescue exec "/bin/sh", "-c", pager end - def extract_source_string_and_coloring(term) - source_string = term[:snipped_source_term].map do |word| + def extract_keyword_and_coloring(snipped_term, term) + return term if snipped_term.empty? + display_string = snipped_term.map do |word| word.is_a?(Hash) ? word[:keyword].bright : word end - source_string = source_string.join - source_string + display_string = display_string.join + display_string end def printer(source_string, target_string, note=nil, Modified: lib/logaling/glossary_db.rb (+50 -0) =================================================================== --- lib/logaling/glossary_db.rb 2012-02-22 10:31:04 +0900 (8b6e4e2) +++ lib/logaling/glossary_db.rb 2012-02-22 17:54:29 +0900 (bca8857) @@ -119,6 +119,7 @@ module Logaling :source_term => term.source_term, :snipped_source_term => struct_snipped_text(snipped_text), :target_term => term.target_term, + :snipped_target_term => [], :note => term.note || ''} end ensure @@ -127,6 +128,54 @@ module Logaling specified_glossary.expression.close if specified_glossary end + def lookup_dictionary(search_word) + records_selected_source = Groonga["translations"].select do |record| + target = record.match_target do |match_record| + match_record.source_term * 2 + end + target =~ search_word + end + completely_match = records_selected_source.select do |record| + record.source_term == search_word + end + completely_match.each do |record| + record.key._score += 10 + end + + records_selected_target = Groonga["translations"].select do |record| + record.target_term =~ search_word + end + + records_selected = records_selected_target.union!(records_selected_source) + records = records_selected.sort([ + {:key=>"_score", :order=>'descending'}, + {:key=>"source_term", :order=>'ascending'}, + {:key=>"target_term", :order=>'ascending'}]) + + options = {:width => 100, + :html_escape => true, + :normalize => true} + snippet = records_selected.expression.snippet(["<snippet>", "</snippet>"], options) + + snipped_source_term = [] + records.map do |record| + term = record.key + snipped_source_term = snippet.execute(term.source_term).join + snipped_target_term = snippet.execute(term.target_term).join + {:glossary_name => term.glossary.key, + :source_language => term.source_language, + :target_language => term.target_language, + :source_term => term.source_term, + :snipped_source_term => struct_snipped_text(snipped_source_term), + :target_term => term.target_term, + :snipped_target_term => struct_snipped_text(snipped_target_term), + :note => term.note || ''} + end + ensure + snippet.close if snippet + records_selected.expression.close if records_selected + end + def translation_list(glossary_source) records_raw = Groonga["translations"].select do |record| [ @@ -324,6 +373,7 @@ module Logaling :key_normalize => true, :default_tokenizer => "TokenBigram") do |table| table.index("translations.source_term") + table.index("translations.target_term") end end end Modified: lib/logaling/repository.rb (+6 -2) =================================================================== --- lib/logaling/repository.rb 2012-02-22 10:31:04 +0900 (4bb4a82) +++ lib/logaling/repository.rb 2012-02-22 17:54:29 +0900 (af0f5ad) @@ -50,12 +50,16 @@ module Logaling end end - def lookup(source_term, glossary_source) + def lookup(source_term, glossary_source, dictionary=false) raise GlossaryDBNotFound unless File.exist?(logaling_db_home) terms = [] Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db| - terms = db.lookup(source_term, glossary_source) + if dictionary + terms = db.lookup_dictionary(source_term) + else + terms = db.lookup(source_term, glossary_source) + end end terms end