0

I'm building a small app which auto translates boolean queries in Java.

This is the code to find if the query string contains a certain word and if so, it replaces it with the translated value.

int howmanytimes = originalValues.size();

for (int y = 0; y < howmanytimes; y++) {
    String originalWord = originalValues.get(y);
    System.out.println("original Word = " + originalWord);

    if (toReplace.contains(" " + originalWord.toLowerCase() + " ") 
         || toCheck.contains('"' + originalWord.toLowerCase() + '"')) {
        toReplace = toReplace.replace(originalWord, translatedValues.get(y).toLowerCase());
        System.out.println("replaced " + originalWord + " with " + translatedValues.get(y).toLowerCase());
    }

    System.out.println("to Replace inside loop " + toReplace);
}

The problem is when a query has, for example, '(mykeyword OR "blue mykeyword")' and the translated values are different, for example, mykeyword translates to elpalavra and "blue mykeyword" translates to "elpalavra azul". What happens in this case is that the result string will be '(elpalavra OR "blue elpalavra")' when it should be '(elpalavra OR "elpalavra azul")' . I understand that in the first loop it replaces all keywords and in the second it no longer contains the original value it should for translation. How can I fix this?

Thank you

6
  • One way would be to split it all in an array, and then loop through the array, if the keyword is found translate it, otherwise skip it. Commented Jul 28, 2017 at 11:26
  • Why would you do translation after forming a query, rather than before? Commented Jul 28, 2017 at 11:27
  • commons.apache.org/proper/commons-lang/apidocs/org/apache/… could be a nice way to do this Commented Jul 28, 2017 at 11:31
  • Just a tip, checking toReplace.contains(" " + originalWord.toLowerCase() + " ") but replacing originalWord without regard to case is bound to error. Commented Jul 28, 2017 at 11:34
  • @borisvankatwijk the app reads a file with queries in english, and returns a file with the queries translated Commented Jul 28, 2017 at 11:47

2 Answers 2

1

you can sort originalValues by size desc. And after that loop through them. This way you first replace "blue mykeyword" and only after you replace "mykeyword"

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

Comments

0

The "toCheck" variable is not explained what is for, and in any case the way it is used looks weird (to me at least).

Keeping that aside, one way to answer your request could be this (based only on the requirements you specified):

  1. sort your originalValues, so that the ones with more words are first. The ones that have same number of words, should be ordered from more length to less.

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.