-1

I need to match two sets of strings that slightly differ from each other.

This is my data class:

public class AccountPair
{
    public string AccountNameA { get; set; }
    public string AccountNameB { get; set; }
    public int AccountNameB_ID {get; set;}
}

As a sample, AccountNameA is John Doe Ltd and AccountNameB is John Doe (PTY) Limited.

Using C#, I need to determine if AccountNameA is a match with AccountNameB and then output AccountNameA with the matched AccountNameB_ID. Is there a way that this can be done because a simple string match won't work with these.

The output would be formatted as:

public class AccountPair
{
    public string AccountNameA { get; set; }
    public int AccountNameB_ID {get; set;}
}

UPDATE

Some Clarification from my side, We have a CRM systems where the companies get created when a client signs up with us. After this, an employee will log onto our Web Application and recreate the Account there. Now when the CRM account gets created, a simple Company Trading name is used, but when the company gets created on our Web Application, the full company registered name is used, thus there is a difference in the company name between the two systems. I need to match the CRM company names with the WebApp company names and then store the WebApp CompanyID to the CRM Company profile so we can build further integrations between the CRM and the WebApp.

Hennericho
  • 23
  • 4
  • 3
    What have you tried? What rules are making a different string equal? – Tim Schmelter Aug 15 '23 at 07:47
  • 2
    You need to give more information about how you think you could match the names. – Enigmativity Aug 15 '23 at 07:49
  • Some Clarification from my side, We have a CRM systems where the companies get created when a client signs up with us. After this, an employee will log onto our Web Application and recreate the Account there. Now when the CRM account gets created, a simple Company Trading name is used, but when the company gets created on our Web Application, the full company registered name is used, thus there is a difference in the company name between the two systems. – Hennericho Aug 15 '23 at 07:55
  • I need to match the CRM company names with the WebApp company names and then store the WebApp CompanyID to the CRM Company profile so we can build further integrations between the CRM and the WebApp. – Hennericho Aug 15 '23 at 07:55
  • Can you edit your question and add these extra details. It makes it so much easier to read and understand what you're asking? – phuzi Aug 15 '23 at 07:57
  • @Hennericho: Would it be feasible to build a dictionary for the mapping between both systems? That would be the most efficient way. And if there is a new you could log this and fallback to simple string comparison. – Tim Schmelter Aug 15 '23 at 07:58
  • @TimSchmelter this would be a solution, the problem is that there is 28k companies on our WebApp and to build a Dictionary manually is very time consuming thus we would like to programmatically do this for us. – Hennericho Aug 15 '23 at 08:02
  • @Hennericho: You can easily create the code to build the dictionary programatically. But that is a different question than what you have asked and we don't have enough information to help you with that task. – Tim Schmelter Aug 15 '23 at 08:03
  • @TimSchmelter - Isn't that a catch-22 type situation? That's what the OP asking for, right? – Enigmativity Aug 15 '23 at 08:06
  • How many data entries are we talking here? It looks like you're doing a cartesian product, which is not optimal, but might be ok depending on the size of the input set. Generally you'll have to do fuzzy matching, e.g. with the Levenshtein distance. – Good Night Nerd Pride Aug 15 '23 at 08:17

2 Answers2

4

I came across an answer to a similar question, where they suggested to use a 3rd party library for it:

Please have a look at https://github.com/JakeBayer/FuzzySharp

You can do something like the following with the 3rd party NuGet package mentioned in the answer above:

if (Fuzz.Ratio(accountPair.AccountNameA, accountPair.AccountNameB) > 90)
{
   // You found a possible match.
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    Welcome to Stack Overflow! You know about the commenting privilege which you do not have, so well that you can even put it into words. You are aware of the rule https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead . In that situation please do not decide to misuse a different mechanism (an answer) for something it is not meant for and which you are not allowed yet to do. – Yunnosch Aug 15 '23 at 08:00
  • @Yunnosch what are you talking about? I wasn't trying to misuse any mechanisms. I was merely trying to help a fellow developer, and since I didn't have enough reputation to just add a comment with the other stackoverflow.com link that actually answered the question, I added a an answer with the link to the answer. – developer-partners Aug 15 '23 at 17:11
  • 1
    I am referring to this part of the original version of your post: "Sorry, I can't add comments yet due to not having enough reputation here on stackoverlow.com, so I have to add this as an answer. " Which to me reads like "I intentionally post this in place of a comment, specifically for the reason that I know not to have the commenting privilege." Please explain how you meant that instead. – Yunnosch Aug 15 '23 at 17:13
  • @Yunnosch if I had the privilege to post a comment, I would just share the link of the other stackoverflow.com answer that answered to this question. That's what I meant. Please DO NOT change my words to things that I didn't say. – developer-partners Aug 15 '23 at 17:16
  • 1
    The fact that you accidentally posted something as a comment, which meanwhile could be salvaged by a different user, by editing it into an answer which does not explicitly state that you mean it as a comment, does not change that you intended to knowingly ingore the rule which you are aware of. I am asking you to not do that again. – Yunnosch Aug 15 '23 at 17:17
  • @Yunnosch what did I accidentally post as a comment? – developer-partners Aug 15 '23 at 17:18
  • 1
    You accidentally posted something as a comment in the shape of an answer which could be edited by somebody else into an answer. You meant it to be a comment. You misused an answer post for something which you did not mean to be an answer. You also have now confirmed that you know that duplicates should be flagged/closed as such instead of being answered. I am quite pleased how well you know the rules. What I ask you to do is actually accepting that they also apply to you and play by those rules which you know. – Yunnosch Aug 15 '23 at 17:20
  • @Yunnosch I didn't accidentally post anything as comment. In fact, I can't post comments to questions yet, so I'm not sure what you are referring to. – developer-partners Aug 15 '23 at 17:24
  • 1
    You posted something as a comment in the shape of an answer. You clicked "Post Your Answer", the result was an answer post. You meant it to be a comment. You can't comment yet, so you decided to post your comment in the shape of an answer. Which is a mechanism exclusively for answering according to [answer]. You used that answering mechanism for something which you meant to be a comment. You explicitly wrote into the first version that you did so. "Sorry, I can't add comments yet due to not having enough reputation here on stackoverlow.com, so I have to add this as an answer. " – Yunnosch Aug 15 '23 at 17:26
  • 1
    The only "accidental" about that was that what you meant to be a comment could be salvaged by being edited into what amounts to be an answer. – Yunnosch Aug 15 '23 at 17:27
  • @Yunnosch yes, I couldn't add comment, so I answered the question. In fact, I think if I didn't mention that in my original post, this conversation probably wouldn't event start. It would be accepted as the correct answer like it is now, and all will be done. But I honestly shared that it could just be a simple comment with a link to the actual answer and that honesty got me into this argument. I guess it's better sometimes to omit certain sentences from answers to avoid situations like this. – developer-partners Aug 15 '23 at 17:35
  • 1
    Yes, that is true. In that case nobody would have known that you were intentionally ignoring rules you are aware of. Everbody would have mistaken for an answer what you meant as a comment. The only thing that remains, now that it was leaked, to make you aware that the rules which you know do also apply to you and to ask you, please, to not decide to misuse answer posts for commenting in the future. Your commenting privilege is not far now, it is important for you to know when to use it - or the flagging privilege for "duplicate". – Yunnosch Aug 15 '23 at 17:41
  • 1
    Welcome! You now can add comments. – Petəíŕd The Linux Wizard Aug 16 '23 at 07:18
0
var acc = new AccountPair() {
    AccountNameA = "John Doe Ltd",
    AccountNameB = "John Doe (PTY)",
    AccountNameB_ID = 1
};

Pair? pair = acc?.FindPair();

public class AccountPair
{
    public string AccountNameA { get; set; }
    public string AccountNameB { get; set; }
    public int AccountNameB_ID { get; set; }


    public Pair? FindPair()
    {
        List<string> words = new();

        foreach (var word in this.AccountNameA.Split(" "))
        {
            if (this.AccountNameB.Contains(word))
            {
                words.Add(word);
            }
        }

        string name = String.Join(" ", words);

        if(this.AccountNameA.Contains(name))
        {
            return new Pair { AccountNameA = this.AccountNameA, AccountNameB_ID = this.AccountNameB_ID };
        }
        return null;
    }
}

public class Pair
{
    public string AccountNameA { get; set; }
    public int AccountNameB_ID { get; set; }
}
Rahadur
  • 109
  • 1
  • 11