Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test and fixed bug #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ project(fuzzywuzzy LANGUAGES C CXX)

message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

if (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
Expand All @@ -46,7 +49,9 @@ set(CMAKE_MODULE_PATH
add_subdirectory(src)
add_subdirectory(test)

if (FMT_TEST)
enable_testing()
#add_subdirectory(test)
endif()
#if (FMT_TEST)
enable_testing()
add_test(main_test ${CMAKE_BINARY_DIR}/bin/main)
#add_subdirectory(test)
#endif()

26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ and been wrapped around some C++ code.
| `utils.{c,h}pp` | Utility functions, translated from the Python library's `utils.py`. |
| `levenshtein.{c,h}` | The underlaying C functions, copied verbatim. |

Build
-----

Build this project and the unit test

```
mkdir -p build
cd build
cmake ..
make -j$(nproc)
```

Test
-----

The unit test of C++ fuzzywuzzy is based on the [Catch2](https://github.com/catchorg/Catch2) framework. Install Catch2 into system path or place the single-header version into the include path as `catch2/catch.hpp`

To run the unit test:

```
cd build
./bin/main
```

Usage
-----
```cpp
Expand Down Expand Up @@ -39,4 +63,4 @@ fuzz::token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear"); // r
fuzz::token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear"); // returns 83 (this should be 84)

fuzz::token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear"); // returns 100
```
```
4 changes: 2 additions & 2 deletions include/fuzzywuzzy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace /* I'm in your mind... */ fuzz {
/* */

/* Calculates a Levenshtein simple ratio between the string. */
unsigned int ratio(const string &s1, const string &s2, const bool full_process = true);
unsigned int ratio(const string &s1, const string &s2, const bool full_process = false);

/*
* Return the ratio of the most similar substring
* as a number between 0 and 100.
*/
unsigned int partial_ratio(const string &s1, const string &s2, const bool full_process = true);
unsigned int partial_ratio(const string &s1, const string &s2, const bool full_process = false);

/* */
/* Advanced scoring functions. */
Expand Down
38 changes: 38 additions & 0 deletions src/fuzzywuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ unsigned int ratio(const string &s1, const string &s2, const bool full_process)
string p1 = full_process ? utils::full_process(s1) : s1;
string p2 = full_process ? utils::full_process(s2) : s2;

// handle empty string cases
if (p1.empty() && p2.empty())
return 100;
else if (p1.empty() || p2.empty())
return 0;

auto m = string_matcher(p1, p2);
return utils::percent_round(m.ratio());
}
Expand All @@ -22,6 +28,38 @@ unsigned int partial_ratio(const string &s1, const string &s2, const bool full_p
string p1 = full_process ? utils::full_process(s1) : s1;
string p2 = full_process ? utils::full_process(s2) : s2;

// handle empty string cases
if (p1.empty() && p2.empty())
return 100;
else if (p1.empty() || p2.empty())
return 0;

/**
* The core part of `partial_ratio` fails to handle the matching of two
* identical strings, such that:
*
* ``` partial_ratio('a, b', 'a, b') == 0;
* ```
*
* In this case, I found the root cause is that the `m.get_matching_blocks()`
* return zero block.
*
* ``` auto blocks = m.get_matching_blocks();
* blocks.empty() == true;
* ```
*
* Wheras in the orignal Python implementation, it returns a list of two
* elements, as following:
*
* ``` [Match(a=0, b=0, size=4), Match(a=4, b=4, size=0)]
* ```
*
* As a result, a temporary workaround is placed here below.
*
*/
if (p1 == p2)
return 100;

string shorter, longer;

if (p1.length() <= p2.length()) {
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ include_directories(${LIB_INCLUDE_DIRS})

file(GLOB_RECURSE SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/test *.c[p]*)
add_executable(main ${SOURCES})
target_link_libraries(main fuzzywuzzy)
target_link_libraries(main fuzzywuzzy)
12 changes: 2 additions & 10 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include <iostream>

#include "fuzzywuzzy.hpp"
#define CATCH_CONFIG_MAIN

int main()
{
const string a = "I'm in your mind", b = "I'm in your mind fuzz";
const string c = "fuzzy wuzzy was a bear", d = "wuzzy fuzzy was a bear";

std::cout << fuzz::ratio(a, b) << '\n';
std::cout << fuzz::partial_ratio(a, b) << '\n';
std::cout << fuzz::token_sort_ratio(c, d) << '\n';
}
#include "catch2/catch.hpp"
Loading