fantasdic r341 - in trunk: . lib/fantasdic lib/fantasdic/sources test



Author: mblondel
Date: Mon Aug 25 18:02:01 2008
New Revision: 341
URL: http://svn.gnome.org/viewvc/fantasdic?rev=341&view=rev

Log:
    * lib/fantasdic/binary_search.rb: Fixed a bug.

    * lib/fantasdic/utils.rb: Added Enumerable#sum.
    * test/test_utils.rb: Added unit test for it.

    * lib/fantasdic/sources/virtual_dictionary.rb: Added Virtual Dictionary
    source. This is a source where databases are dictionaries defined in the
    settings. This is a convient solution to to group dictionaries together.
    * test/test_virtual_dictionary.rb: Added unit test for it.


Added:
   trunk/lib/fantasdic/sources/virtual_dictionary.rb
   trunk/test/test_virtual_dictionary.rb
Modified:
   trunk/ChangeLog
   trunk/lib/fantasdic/binary_search.rb
   trunk/lib/fantasdic/utils.rb
   trunk/test/test_utils.rb

Modified: trunk/lib/fantasdic/binary_search.rb
==============================================================================
--- trunk/lib/fantasdic/binary_search.rb	(original)
+++ trunk/lib/fantasdic/binary_search.rb	Mon Aug 25 18:02:01 2008
@@ -49,8 +49,10 @@
         case comp.call(curr_word, word)
             when 1 # greater than
                 high = get_prev_offset(mid)
+                return nil if high.nil?
             when -1 # less than
                 low = get_next_offset(mid)
+                return nil if low.nil?
             when 0 # equals
                 return start
         end

Added: trunk/lib/fantasdic/sources/virtual_dictionary.rb
==============================================================================
--- (empty file)
+++ trunk/lib/fantasdic/sources/virtual_dictionary.rb	Mon Aug 25 18:02:01 2008
@@ -0,0 +1,126 @@
+# Fantasdic
+# Copyright (C) 2008 Mathieu Blondel
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+#Âwith this program; if not, write to the Free Software Foundation, Inc.,
+#Â51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+module Fantasdic
+module Source
+
+class VirtualDictionaryBase < Base
+
+    STRATEGIES_DESC = {
+        "define" => "Results match with the word exactly.",
+        "prefix" => "Results match with the beginning of the word.",
+        "word" => "Results have one word that match with the word.",
+        "substring" => "Results have a portion that contains the word.",
+        "suffix" => "Results match with the end of the word."
+    }
+
+    def initialize(prefs, *args)
+        super(*args)
+        @prefs = prefs
+    end
+
+    def available_strategies
+        STRATEGIES_DESC
+    end
+
+    def available_databases
+        hsh = {}
+        authorized_databses.each { |d| hsh[d] = d }
+        hsh
+    end
+
+    def define(db, word)
+        virtual_dbs = if db == Source::Base::ALL_DATABASES
+            authorized_databses
+        else
+            [db]
+        end
+
+        virtual_dbs.map do |db|
+            src_class, config, dbs = get_source(db)
+            src = src_class.new(config)
+            src.open
+            definitions = src.cached_multiple_define(dbs, word)
+            src.close
+            definitions
+        end.sum
+    end
+
+    def match(db, strat, word)
+        dbs = if db == Source::Base::ALL_DATABASES
+            authorized_databses
+        else
+            [db]
+        end
+
+        matches = {}
+        dbs.each do |db|
+            src_class, config, dbs = get_source(db)
+            src = src_class.new(config)
+            src.open
+            m = src.cached_multiple_match(dbs, strat, word)
+            m.each_key do |found_db|
+                matches[found_db] ||= []
+                matches[found_db] += m[found_db]
+            end
+            src.close
+        end
+
+        matches
+    end
+
+    private
+
+    def is_virtual?(db)
+        @prefs.dictionaries_infos[db][:source] == "VirtualDictionary"
+    end
+
+    def authorized_databses
+        @prefs.dictionaries.find_all { |db| not is_virtual?(db) }
+    end
+
+    def get_source(db)
+        config = @prefs.dictionaries_infos[db]
+        dbs = if config[:all_dbs]
+            [Source::Base::ALL_DATABASES]
+        else
+            config[:sel_dbs]
+        end
+        src_class = Fantasdic::Source::Base.get_source(config[:source])
+        [src_class, config, dbs]
+    end
+
+end
+
+class VirtualDictionary < VirtualDictionaryBase
+
+    authors ["Mathieu Blondel"]
+    title  _("Virtual dictionary")
+    description _("Look up words in several dictionaries at once.")
+    license Fantasdic::GPL
+    copyright "Copyright (C) 2008 Mathieu Blondel" 
+
+    def initialize(*args)
+        super(Preferences.instance, *args)
+    end
+
+end
+
+end
+end
+
+Fantasdic::Source::Base.register_source(Fantasdic::Source::VirtualDictionary)

Modified: trunk/lib/fantasdic/utils.rb
==============================================================================
--- trunk/lib/fantasdic/utils.rb	(original)
+++ trunk/lib/fantasdic/utils.rb	Mon Aug 25 18:02:01 2008
@@ -145,3 +145,17 @@
         Proc.new { |*args| args.shift.send(self, *args) }
     end
 end
+
+module Enumerable
+
+    def sum(identity = 0, &block)
+        return identity unless size > 0
+
+        if block_given?
+            map(&block).sum
+        else
+            inject { |sum, element| sum + element }
+        end
+    end
+
+end
\ No newline at end of file

Modified: trunk/test/test_utils.rb
==============================================================================
--- trunk/test/test_utils.rb	(original)
+++ trunk/test/test_utils.rb	Mon Aug 25 18:02:01 2008
@@ -117,4 +117,10 @@
         assert_equal(File.which("pgmthatdoesntexist"), nil)
     end
 
+    def test_enumerable_sum
+        assert_equal([1,2,3,4].sum, 10)
+        assert_equal([[1],[2],[3,4]].sum, [1,2,3,4])
+        assert_equal([[1],[2],[3,4]].sum.sum, 10)
+    end
+
 end

Added: trunk/test/test_virtual_dictionary.rb
==============================================================================
--- (empty file)
+++ trunk/test/test_virtual_dictionary.rb	Mon Aug 25 18:02:01 2008
@@ -0,0 +1,59 @@
+# Fantasdic
+# Copyright (C) 2008 Mathieu Blondel
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+#Âwith this program; if not, write to the Free Software Foundation, Inc.,
+#Â51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+test_dir = File.expand_path(File.dirname(__FILE__))
+top_dir = File.expand_path(File.join(test_dir, ".."))
+lib_dir = File.expand_path(File.join(top_dir, "lib"))
+$config_file = File.expand_path(File.join(test_dir, "data", "config.yaml"))
+$test_data_dir = File.expand_path(File.join(test_dir, "data"))
+$edict_file = File.join($test_data_dir, "edict.utf8")
+$dict_index_file = File.join($test_data_dir, "freedict-wel-eng.index")
+$LOAD_PATH.unshift(lib_dir)
+
+require "test/unit"
+require "fantasdic"
+require "fantasdic/sources/virtual_dictionary"
+require "fantasdic/sources/dictd_file"
+require "fantasdic/sources/edict_file"
+
+class TestVirtualDictionarySource < Test::Unit::TestCase
+
+    def setup
+        prefs = Fantasdic::PreferencesBase.new($config_file)
+        prefs.dictionaries = ["edict", "dictdfile"]
+        prefs.dictionaries_infos["edict"] = {
+            :source => "EdictFile",
+            :filename => $edict_file,
+            :all_dbs => true,
+            :sel_dbs => [],
+            :encoding => "UTF-8"}
+        prefs.dictionaries_infos["dictdfile"] = {
+            :source => "DictdFile",
+            :all_dbs => true,
+            :sel_dbs => [],
+            :filename => $dict_index_file}
+
+        @source = Fantasdic::Source::VirtualDictionaryBase.new(prefs)
+    end
+
+    def test_define
+        defs = @source.define("*", "pedair")
+        assert_equal(defs.length, 1)
+        assert_equal(defs.first.body, "pedair\n   four")
+    end
+
+end



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]