replace a word in a row

How would you replace all occurrences of the word “car” with “bike”?

somecar --> somebike
carphone --> bikephone
this car is black --> this bike is black

This is somewhat like a spell check…

Thanks.

1 Like

Hi @sw1336 and welcome back to the KNIME community forum,

Thanks for creating a new topic for your question.

You can use the replace() function in the String Manipulation node like this:

replace($column1$, "car", "bike")

Or for more complex cases, you can use the regexReplace() function.

:blush:

3 Likes

That was exactly not my issue, but your response jogged a memory and I was able to implement it.

My Solution - have a simple two col table. Loop that against the table in question and replace the bad word with the good one using variables.

Thanks!

1 Like

Maybe if you provide a more complete explanation of your issue, I can provide you with a better solution.
I guess what you are doing can be done by using the Rule Engine (Dictionary) node instead of looping.

:blush:

I had used a String Replace (Dict) to find and replace the entire sentence with the correct sentence. This was bad and cumbersome.

This is what I did…

Annotation 2020-04-10 151545

Could you provide a sample input please?

SONORA PLOICE DEPARTMENT
BEAN STATATION POLICE
IBERIA PARSH GOVT
CITY OF PASADENA POLCE DEPT
SAN JOAQUIN VLLY
FIRE DIRSTRICT 3

And what should be replaced?

The words PLOICE, STATATION, PARSH, POLCE, VLLY, DIRSTRICT should be replaced with POLICE, STATION, PARISH, VALLEY, DISTRICT.

The spell checker in KNIME didn’t catch these and a lot of the others that I haven’t shown…

You can use Bing spell check API. Here is an example using my trial key (7 days remaining). Feel free to use it. You need to get your own key after the trial period:

22489-1-1.knwf (78.4 KB)

:blush:

2 Likes

Interesting!!

Many thanks, Armin…

Armin - why doesn’t this work? Can you please take a look?

Many thanks.

SpellCheck.knwf (13.0 KB)

The problem is that you have enclosed variable names in double quotations (String Manipulation).

If you insist to use your own list of “bad -> good” words, then how about this:

22489-1-2.knwf (78.6 KB)

:blush:

P.S. 2 points:

  • You can input the name of your txt column in the second String Manipulation column.
  • I have used regexReplace() function which is not very safe in this case. It’s better to use replace() function. So just replace “regexReplace” with “replace” in the first String Manipulation node if you like.
5 Likes

Fantastic Armin. Many, many thanks.

:ok_hand:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.