SOLVED - stripAccents from a string

Hi!

I’m toying with voice recognition right now…
I want to remove the diacritics and accented characters from the voice command string, but I don’t know how…
Surely, the first approach would be to replace all accented characters, but it’s weird.
One other way (Google helped of course) to use the stripAccents method of StringUtils.
But how to use that in a rule?
I have, for example:

import org.apache.commons.lang.StringUtils
................................
var String command = VoiceCommand.state.toString.toLowerCase
command = StringUtils.stripAccents(command)

I also tried:

command = org.apache.commons.lang.StringUtils::stripAccents(command)

but all of them throw this error:

14:26:28.160 [INFO ] [pse.smarthome.model.script.Voice.Rec] - VoiceCommand received hello
14:26:28.165 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'VoiceControl': An error occured during the script execution: The name '<XMemberFeatureCallImplCustom>::stripAccents(<XFeatureCallImplCustom>)' cannot be resolved to an item or type.

Any thoughts? I should add that I’m not very skilled at programming in general and java in particular, but I do have the basics :smile:

import org.apache.commons.lang.StringUtils
................................
var String command = VoiceCommand.state.toString.toLowerCase
command = StringUtils::stripAccents(command)

I think your first example was nearly correct. In the rule language, you have to use :: to call static methods.

Nope, same error, but thank you @watou
I might have tried that before putting the whole library name in front of “stripAccents”

14:36:59.384 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'VoiceControl': An error occured during the script execution: The name '<XFeatureCallImplCustom>::stripAccents(<XFeatureCallImplCustom>)' cannot be resolved to an item or type.

It may be that org.apache.commons.lang is not in the class path for the script engine. You might try using the source as an example to build your own version in the rule language.

Thank you very much for the tip!
Got it working based on the link you gave and some googling (stack overflow )

import java.text.Normalizer

.........................

var String command = VoiceCommand.state.toString.toLowerCase
command = Normalizer::normalize(command, Normalizer.Form.NFD)
command = command.replaceAll("\\p{M}", "")
1 Like