2

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.

3
  • Use regex in java. java.util.regex Commented Sep 9, 2017 at 19:53
  • I edited my question, can i still use regex? Commented Sep 9, 2017 at 19:59
  • 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. Commented Sep 9, 2017 at 20:02

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 ....

Comments

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.

Comments

0

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.

Comments

0

A regex approach to the problem:

    List<String> testInput = Arrays.asList(new String[]{
            "BigSizeScrewA   45**",
            "BigSizeScrewB   45**",
            "BigSizeScrewC   45**",
            "BigSizeScrewD   45**",
            "BigSizeScrewE   45**",
    });

    Pattern pattern = Pattern.compile("\\w+");
    for (String test : testInput) {
        Matcher matcher = pattern.matcher(test);
        if (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

output:

BigSizeScrewA
BigSizeScrewB
BigSizeScrewC
BigSizeScrewD
BigSizeScrewE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.