Spell-Check: Find one-to-one token difference mapping between two strings

Viewed 91

I recently stumbled over this question on an internet archive and am having some difficulty wrapping my head around it. I want to find a desired mapping amongst the different tokens between two strings. The output should a String-to-String map.

For example:

String1: hewlottpackardenterprise helped american raleways in N Y

String2: hewlett packard enterprise helped american railways in NY

Output:
hewlottpackardenterprise -> hewlett packard enterprise

hewlott -> hewlett

raleways -> railways

N Y -> NY

Note: I have been able to write an edit-distance method, which finds all types of edits (segregated by types, like deletion, substitution etc.) and can convert the first string to second by a convert method

What have I tried so far?

Approach 1: I began with a naive approach of splitting both the strings by space, inserting the tokens of the first string into a hash map and comparing the tokens of the other string with this hashmap. However, this approach quickly fails as misses on relevant mappings.

Approach 2: I utilize my covert method to find the edit positions in the string, and type of edits. Using space edits, I'm able to create a mapping from hewlottpackardenterprise -> hewlett packardenterprise. However, the method just explodes as more and more things need to be splitted within the same word.

Appreciate any thoughts in this regard! Will clear any doubts in the comments.

public String returnWhiteSpaceEdittoken(EditDone e, List<String> testTokens) {
    int pos = e.pos, count=0, i=0;
    String resultToken = null;

    if (e.type.equals(DeleteEdit)) {
        for (i=0;i<testTokens.size();i++) {
            count+=testTokens.get(i).length();
            if (count==pos) {
                break;
            }
            if (i!=testTokens.size()-1) {
                count++;
            }
        }

        resultToken = testTokens.get(i) + " " + testTokens.get(i+1);
    } else if (e.type.equals(InsertEdit)) {
        for (i=0;i<testTokens.size();i++) {
            count+=testTokens.get(i).length();
            if (count>pos) {
                break;
            }
            if (i!=testTokens.size()-1) {
                count++; 
            }
        }
        String token = testTokens.get(i);
        resultToken = token.substring(count-token.length(), pos) + token.substring(pos, count);
    }
    return resultToken;
}
1 Answers
Related