Java find partially matching string from list

370
September 10, 2017, at 09:49 AM

So Im creating an optical text reader using google camera api for OCR. When reaeding from the camera i want to filter the read strings by matching them to a list of strings and if the read string partially matches, the item from the list gets added and displayed.

Everything but the filtering works.

Say i have a list containing the string:

"BigSizeScrew   45**"

I want to match the read string:

"BigSizeScrew"

...to the one with the number and then add the string containing the number too.

The number and stars are for another reason but must be kept, the reason is irrelevant to the question.

tl;dr:

How do i match partially match a string to an existing string from a list?

I have the following but it exclusively searches for a 100% matching string, not if the checked string contains a substring of the read string.

for(int i =0;i<items.size();++i){
    if (list.contains(items.valueAt(i))) {
        TextBlock item = items.valueAt(i);
        stringBuilder.append(item.getValue());
        stringBuilder.append("\n");
    }
}

EDIT:

The list contains approx 200 different strings that should be matchable partially. But all in the same way; Partially matching the main string but not the numbers/stars.

Answer 1

You can do something like this by adding a if condition:

for(int i =0;i<items.size();++i){
    if (list.contains(items.valueAt(i))) {
        TextBlock item = items.valueAt(i);
        if(item.getValue().indexOf("Your String") > -1){
          stringBuilder.append(item.getValue());
          stringBuilder.append("\n");
        }
    }
}

In place of your string use the string value you want to search for.

Answer 2

Use indexOf()method. Index of method give index of matching string. So it its value is greater than -1, that means seach string exist in list and you can use that string.

Answer 3

You can use the startsWith method of String. If ocrString has value BigSizeScrew 45** then execute a for loop to the list with the strings to match, and if(ocrString.startsWith(list.get(i)) then ....

Answer 4

I think you may actually want a fuzzy sentence matcher. If letters are not captured by the optics you'll need to interpolate what they might be. It gets into NLP but might be the proper way to implement.

Here is a link to Smith-Waterman wikipedia: https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm

Leveshtein Distance: https://en.wikipedia.org/wiki/Levenshtein_distance

There are various implementations and libraries to do a single function call on each statement read. "Local sentence alignment" is the problem you are trying to solve. Forget exact match when screen readers are involved expect human and computer error.

Rent Charter Buses Company
READ ALSO
How to merge three web application of different region in single code base

How to merge three web application of different region in single code base

We have a Web applicationFor the same, we have three separate code bases for three different regions

237
Running java issue on Eclipse

Running java issue on Eclipse

Hi I am a beginning java learner, and need to run my code in EclipseHowever I encountered below error:

372
Android In-app purchase NullPointerException Purchase.getSku()

Android In-app purchase NullPointerException Purchase.getSku()

I am working on android in purchase projectI used this sample code as reference

259
Gradle compile dependency outside of project

Gradle compile dependency outside of project

I have a frontend and a backend repository for my appThe backend is written in Go and serves the API over gRPC

313