diff --git a/CMakeLists.txt b/CMakeLists.txt index fbbe7d4..81d7a82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,6 +147,7 @@ if (BUILD_TESTS) add_sdcv_shell_test(t_only_data_dir) add_sdcv_shell_test(t_synonyms) add_sdcv_shell_test(t_json) + add_sdcv_shell_test(t_exact) add_sdcv_shell_test(t_interactive) add_sdcv_shell_test(t_utf8output) add_sdcv_shell_test(t_utf8input) diff --git a/src/libwrapper.cpp b/src/libwrapper.cpp index 948c883..80a43f5 100644 --- a/src/libwrapper.cpp +++ b/src/libwrapper.cpp @@ -350,7 +350,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force) break; case qtSIMPLE: SimpleLookup(get_impl(str), res_list); - if (res_list.empty()) + if (res_list.empty() && fuzzy_) LookupWithFuzzy(get_impl(str), res_list); break; case qtDATA: diff --git a/src/libwrapper.hpp b/src/libwrapper.hpp index 1809326..2a20309 100644 --- a/src/libwrapper.hpp +++ b/src/libwrapper.hpp @@ -25,9 +25,10 @@ typedef std::vector TSearchResultList; //of it class Library : public Libs { public: - Library(bool uinput, bool uoutput, bool colorize_output, bool use_json) + Library(bool uinput, bool uoutput, bool colorize_output, bool use_json, bool no_fuzzy) : utf8_input_(uinput), utf8_output_(uoutput), colorize_output_(colorize_output), json_(use_json) { setVerbose(!use_json); + setFuzzy(!no_fuzzy); } bool process_phrase(const char *loc_str, IReadLine &io, bool force = false); diff --git a/src/sdcv.cpp b/src/sdcv.cpp index b960c81..6573d58 100644 --- a/src/sdcv.cpp +++ b/src/sdcv.cpp @@ -76,6 +76,7 @@ int main(int argc, char *argv[]) try { glib::StrArr use_dict_list; gboolean non_interactive = FALSE; gboolean json_output = FALSE; + gboolean no_fuzzy = FALSE; gboolean utf8_output = FALSE; gboolean utf8_input = FALSE; glib::CharStr opt_data_dir; @@ -94,6 +95,8 @@ int main(int argc, char *argv[]) try { _("for use in scripts"), nullptr }, { "json-output", 'j', 0, G_OPTION_ARG_NONE, &json_output, _("print the result formatted as JSON."), nullptr }, + { "exact-search", 'e', 0, G_OPTION_ARG_NONE, &no_fuzzy, + _("do not fuzzy-search for similar words, only return exact matches."), nullptr }, { "utf8-output", '0', 0, G_OPTION_ARG_NONE, &utf8_output, _("output must be in utf8"), nullptr }, { "utf8-input", '1', 0, G_OPTION_ARG_NONE, &utf8_input, @@ -199,7 +202,7 @@ int main(int argc, char *argv[]) try { fprintf(stderr, _("g_mkdir failed: %s\n"), strerror(errno)); } - Library lib(utf8_input, utf8_output, colorize, json_output); + Library lib(utf8_input, utf8_output, colorize, json_output, no_fuzzy); lib.load(dicts_dir_list, order_list, disable_list); std::unique_ptr io(create_readline_object()); diff --git a/src/stardict_lib.cpp b/src/stardict_lib.cpp index 38de715..f3077b7 100644 --- a/src/stardict_lib.cpp +++ b/src/stardict_lib.cpp @@ -1404,7 +1404,7 @@ bool Libs::LookupSimilarWord(const gchar* sWord, glong & iWordIndex, int iLib) bool Libs::SimpleLookupWord(const gchar* sWord, glong & iWordIndex, int iLib) { bool bFound = oLib[iLib]->Lookup(sWord, iWordIndex); - if (!bFound) + if (!bFound && fuzzy_) bFound = LookupSimilarWord(sWord, iWordIndex, iLib); return bFound; } diff --git a/src/stardict_lib.hpp b/src/stardict_lib.hpp index 054c2f3..abcbe56 100644 --- a/src/stardict_lib.hpp +++ b/src/stardict_lib.hpp @@ -145,6 +145,7 @@ public: iMaxFuzzyDistance = MAX_FUZZY_DISTANCE; //need to read from cfg. } void setVerbose(bool verbose) { verbose_ = verbose; } + void setFuzzy(bool fuzzy) { fuzzy_ = fuzzy; } ~Libs(); Libs(const Libs&) = delete; Libs& operator=(const Libs&) = delete; @@ -178,6 +179,8 @@ public: bool LookupWithFuzzy(const gchar *sWord, gchar *reslist[], gint reslist_size); gint LookupWithRule(const gchar *sWord, gchar *reslist[]); bool LookupData(const gchar *sWord, std::vector *reslist); +protected: + bool fuzzy_; private: std::vector oLib; // word Libs. int iMaxFuzzyDistance; diff --git a/tests/stardict-test_synonyms-2.4.2/test.dict.dz b/tests/stardict-test_synonyms-2.4.2/test.dict.dz index c081b89..fea9f2e 100644 Binary files a/tests/stardict-test_synonyms-2.4.2/test.dict.dz and b/tests/stardict-test_synonyms-2.4.2/test.dict.dz differ diff --git a/tests/stardict-test_synonyms-2.4.2/test.idx b/tests/stardict-test_synonyms-2.4.2/test.idx index b412056..871c01e 100644 Binary files a/tests/stardict-test_synonyms-2.4.2/test.idx and b/tests/stardict-test_synonyms-2.4.2/test.idx differ diff --git a/tests/stardict-test_synonyms-2.4.2/test.ifo b/tests/stardict-test_synonyms-2.4.2/test.ifo index 130d89f..70f26e1 100644 --- a/tests/stardict-test_synonyms-2.4.2/test.ifo +++ b/tests/stardict-test_synonyms-2.4.2/test.ifo @@ -1,7 +1,7 @@ StarDict's dict ifo file version=2.4.2 bookname=Test synonyms -wordcount=1 +wordcount=2 synwordcount=2 -idxfilesize=13 +idxfilesize=32 sametypesequence=m diff --git a/tests/stardict-test_synonyms-2.4.2/test.xml b/tests/stardict-test_synonyms-2.4.2/test.xml index 999c1e3..ddad79a 100644 --- a/tests/stardict-test_synonyms-2.4.2/test.xml +++ b/tests/stardict-test_synonyms-2.4.2/test.xml @@ -15,4 +15,9 @@ +
testawordy + + + +
diff --git a/tests/t_exact b/tests/t_exact new file mode 100755 index 0000000..f4c11d2 --- /dev/null +++ b/tests/t_exact @@ -0,0 +1,24 @@ +#!/bin/sh + +set -e + +SDCV="$1" +TEST_DIR="$2" + +unset SDCV_PAGER + +test_word() { + WORD=$1 + EXPECTED=$2 + TAG=$3 + RES=$($SDCV -e -n --data-dir "$TEST_DIR" -u "Test synonyms" $WORD | grep "$TAG") + if [ "$EXPECTED" != "$RES" ]; then + echo "synonym for $WORD should be '$EXPECTED' but was '$RES'" + exit 1 + fi +} + +test_word testawordies "Nothing similar to testawordies, sorry :(" "Nothing similar" +test_word testawordy "word that ends in y to test with fuzzy search in -ied" "fuzzy" + +exit 0