intuit / fuzzy-matcher

A Java library to determine probability of objects being similar.
Apache License 2.0
226 stars 69 forks source link

How to use getScore in Element class? what is the matchingCount? #74

Closed Hagar-Usama closed 2 months ago

Hagar-Usama commented 5 months ago

Hello 👋,

I've got a look at the code snippet provided in Element class:

public double getScore(Integer matchingCount, Element other) {
    return (double)matchingCount / (double)this.getChildCount(other);
}

public long getChildCount(Matchable other) {
    if (other instanceof Element) {
        Element<T> o = (Element)other;
        return (long)Math.max(this.getTokens().size(), o.getTokens().size());
    } else {
        return 0L;
    }
}

I'm doing matching between two Documents (Rows) and aiming to obtain the score for each element in the match. I presume each document contains only one document match, which has the highest matching score (row-wise).

I primarily encounter two issues:

Thank you.

manishobhatia commented 5 months ago

Hi Hagar,

The output of the service return all matching documents. So if you want to match between a collection of 2 documents, this is the service that is most appropriate

The result matches every record in the source (documents) with the multiple target (matchWith). This is a good test to run locally. And to visualize the result, you can add this print statement at the end

       result.entrySet().forEach(entry -> {
            System.out.println("Key: " + entry.getKey().getKey());
            entry.getValue().forEach(match -> {
                System.out.println("Data: " + match.getData() + " Matched With: " + match.getMatchedWith() + " Score: " + match.getScore().getResult());
            });
        });

So the total score of a document rely on the matching elements. At an Element level there is no key, but all elements of the same ElementType undergo match comparisons. This comparison depends on how many tokens matched and this is represented by matchingCount For example when matching these 2 element addresses 123 new st. and 123 new Street the tokens are broken down to each word, and we can see 2 tokens 123 and new match.

But this is going into the internals of match algorithm. For most of the cases just using the key at a Document and the overall score in Match should suffice in finding similarities. If you can elaborate your use case with a few examples, that can help me understand what your are trying to solve.

Hagar-Usama commented 5 months ago

Hello @manishobhatia,

Thank you for your prompt response.

My current flow is as the test you mentioned. However, I wish to take an extra step due to the business logic requirements.

In addition to displaying the matched rows along with their scores, it is necessary to indicate the degree of similarity for each field compared to the corresponding field in the matched rows.

For instance:


Match:
- Row1: Field 1: value1, Field 2: value2, Field 3: value3
- Row2: Field 1: value4, Field 2: value5, Field 3: value6

Match Score = 60%

Field-wise score:
- Field 1 --> Score (25%)
- Field 2 --> Score (10%)
- Field 3 --> Score (50%)

And thank you for letting me know that the comparison is based on elementType rather than mapping elements. This will definitely help.