Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3490404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:30:44+00:00 2026-05-18T11:30:44+00:00

I have come across a problem of matching a string in an OCR recognized

  • 0

I have come across a problem of matching a string in an OCR recognized text and find the position of it considering there can be arbitrary tolerance of wrong, missing or extra characters. The result should be a best match position, possibly (not necessarily) with length of matching substring.

For example:

String: 9912, 1.What is your name?
Substring: 1. What is your name?
Tolerance: 1
Result: match on character 7

String: Where is our caat if any?
Substring: your cat
Tolerance: 2
Result: match on character 10

String: Tolerance is t0o h1gh.
Substring: Tolerance is too high;
Tolerance: 1
Result: no match

I have tried to adapt Levenstein algorithm, but it doesn’t work properly for substrings and doesn’t return position.

Algorithm in Delphi would be preferred, yet any implementation or pseudo logic would do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:30:45+00:00Added an answer on May 18, 2026 at 11:30 am

    Here’s a recursive implementation that works, but might not be fast enough. The worst case scenario is when a match can’t be found, and all but the last char in “What” gets matched at every index in Where. In that case the algorithm will make Length(What)-1 + Tolerance comparasions for each char in Where, plus one recursive call per Tolerance. Since both Tolerance and the length of What are constnats, I’d say the algorithm is O(n). It’s performance will degrade linearly with the length of both “What” and “Where”.

    function BrouteFindFirst(What, Where:string; Tolerance:Integer; out AtIndex, OfLength:Integer):Boolean;
      var i:Integer;
          aLen:Integer;
          WhatLen, WhereLen:Integer;
    
        function BrouteCompare(wherePos, whatPos, Tolerance:Integer; out Len:Integer):Boolean;
        var aLen:Integer;
            aRecursiveLen:Integer;
        begin
          // Skip perfect match characters
          aLen := 0;
          while (whatPos <= WhatLen) and (wherePos <= WhereLen) and (What[whatPos] = Where[wherePos]) do
          begin
            Inc(aLen);
            Inc(wherePos);
            Inc(whatPos);
          end;
          // Did we find a match?
          if (whatPos > WhatLen) then
            begin
              Result := True;
              Len := aLen;
            end
          else if Tolerance = 0 then
            Result := False // No match and no more "wild cards"
          else
            begin
              // We'll make an recursive call to BrouteCompare, allowing for some tolerance in the string
              // matching algorithm.
              Dec(Tolerance); // use up one "wildcard"
              Inc(whatPos); // consider the current char matched
              if BrouteCompare(wherePos, whatPos, Tolerance, aRecursiveLen) then
                begin
                  Len := aLen + aRecursiveLen;
                  Result := True;
                end
              else if BrouteCompare(wherePos + 1, whatPos, Tolerance, aRecursiveLen) then
                begin
                  Len := aLen + aRecursiveLen;
                  Result := True;
                end
              else
                Result := False; // no luck!
            end;
        end;
    
      begin
    
        WhatLen := Length(What);
        WhereLen := Length(Where);
    
        for i:=1 to Length(Where) do
        begin
          if BrouteCompare(i, 1, Tolerance, aLen) then
          begin
            AtIndex := i;
            OfLength := aLen;
            Result := True;
            Exit;
          end;
        end;
    
        // No match found!
        Result := False;
    
      end;
    

    I’ve used the following code to test the function:

    procedure TForm18.Button1Click(Sender: TObject);
    var AtIndex, OfLength:Integer;
    begin
      if BrouteFindFirst(Edit2.Text, Edit1.Text, ComboBox1.ItemIndex, AtIndex, OfLength) then
        Label3.Caption := 'Found @' + IntToStr(AtIndex) + ', of length ' + IntToStr(OfLength)
      else
        Label3.Caption := 'Not found';
    end;
    

    For case:

    String: Where is our caat if any?
    Substring: your cat
    Tolerance: 2
    Result: match on character 10
    

    it shows a match on character 9, of length 6. For the other two examples it gives the expected result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across a problem I just can't solve. I am using autocomplete
Really hope this can be done but I have come across a problem I
I'm experimenting with WCF Services, and have come across a problem with passing Interfaces.
I have come across what must be a common problem. When I have an
One problem that I come across regularly and yet don't have a solution to
I have come across this problem before and I have never understood the reasoning
I have come across a problem with binding to a PasswordBox . It seems
I have come across this problem in a calculation I do in my code,
I'm trying to learn dependency injection and have come across a problem, when unit
One of the problems I have come across having complex tasks on the browser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.