I’m trying to filter messages and setting condition that looks on the text and continue if it contains one of possible values.
I’ve tried using “Is In” and in the values specifying multiple values separated by comma, but it doesn’t work if the original text had other words beside of the filter ones.
Examples :
My text : I ate many candies
My text : who likes Banana ?
Filter : My Text Is in (Banana, candies , apples)
I would like both of these messages to be filter by this condition , but it would only filter if my text contain the exact word :
For examples the following text will be filtered
My text: candies
Thanks
Best answer by SamB
Hi and welcome to the Community @lirana! 👋
Perhaps you could use a code step for this? I did some playing around with our AI-powered code generator feature and was able to get a Code step to check for a match for the words in your example and return a value of either true or false:
Here’s the Python code I used for this:
# Define a function to check if a specific word is present in the textdefcheck_word(word):# Convert the text to lowercase and split it into words
words = input_data["text"].lower().split()
# Return True if the word is in the list of words, otherwise return Falsereturn word in words
# Define a function to check if any of the given words are present in the textdefcheck_words():# Define a list of words to check
words_to_check = ["Banana", "candies", "apples"]
# Convert all words in words_to_check to lowercase for consistency
words_to_check = [word.lower() for word in words_to_check]
# Iterate over the list of words to checkfor word in words_to_check:
# If the current word is present in the text, return Trueif check_word(word):
returnTrue# If none of the words are present in the text, return FalsereturnFalse# Call the function to check if any of the words are present in the text
result = check_words()
# Print the result
print(result)
# Define the output variable as a dictionary with the result
output = {"result": result}
Then the filter could be used to check the output from the Code action to see if it was true - meaning it found a match.
Hope that helps. If you run into any issues on that do let us know - happy to help further! 🙂
Updated January 7, 2025: Added additional code to convert list of words to check against to lowercase.
Perhaps you could use a code step for this? I did some playing around with our AI-powered code generator feature and was able to get a Code step to check for a match for the words in your example and return a value of either true or false:
Here’s the Python code I used for this:
# Define a function to check if a specific word is present in the textdefcheck_word(word):# Convert the text to lowercase and split it into words
words = input_data["text"].lower().split()
# Return True if the word is in the list of words, otherwise return Falsereturn word in words
# Define a function to check if any of the given words are present in the textdefcheck_words():# Define a list of words to check
words_to_check = ["Banana", "candies", "apples"]
# Convert all words in words_to_check to lowercase for consistency
words_to_check = [word.lower() for word in words_to_check]
# Iterate over the list of words to checkfor word in words_to_check:
# If the current word is present in the text, return Trueif check_word(word):
returnTrue# If none of the words are present in the text, return FalsereturnFalse# Call the function to check if any of the words are present in the text
result = check_words()
# Print the result
print(result)
# Define the output variable as a dictionary with the result
output = {"result": result}
Then the filter could be used to check the output from the Code action to see if it was true - meaning it found a match.
Hope that helps. If you run into any issues on that do let us know - happy to help further! 🙂
Updated January 7, 2025: Added additional code to convert list of words to check against to lowercase.
Perhaps you could use a code step for this? I did some playing around with our AI-powered code generator feature and was able to get a Code step to check for a match for the words in your example and return a value of either true or false:
Here’s the Python code I used for this:
# Define a function to check if a specific word is present in the textdefcheck_word(word):# Convert the text to lowercase and split it into words
words = input_data["text"].lower().split()
# Return True if the word is in the list of words, otherwise return Falsereturn word in words
# Define a function to check if any of the given words are present in the textdefcheck_words():# Define a list of words to check
words_to_check = ["banana", "candies", "apples"]
# Iterate over the list of words to checkfor word in words_to_check:
# If the current word is present in the text, return Trueif check_word(word):
returnTrue# If none of the words are present in the text, return FalsereturnFalse# Call the function to check if any of the words are present in the text
result = check_words()
# Print the result
print(result)
# Define the output variable as a dictionary with the result
output = {"result": result}
Then the filter could be used to check the output from the Code action to see if it was true - meaning it found a match.
Hope that helps. If you run into any issues on that do let us know - happy to help further! 🙂
There is one small typo.
check_word function should return word.lower() check, otherwise capitalized keywords might be ignored
The check_word function should already account for capitalization by converting all the text to lowercase in this line:
words = input_data["text"].lower().split()
I’ve tested the code with capitalized input as well, and it still worked as expected. Can you share an example case where it’s not working? I’d be happy to do some further testing on my end and adjust the code if needed.
The check_word function should already account for capitalization by converting all the text to lowercase in this line:
words = input_data["text"].lower().split()
I’ve tested the code with capitalized input as well, and it still worked as expected. Can you share an example case where it’s not working? I’d be happy to do some further testing on my end and adjust the code if needed.
Looking forward to hearing from you!
Yep, that is true, but it does not do that for the user-inputted keywords (Banana, Candies, apples, etc).
There could be an instruction mentioning that all keywords in the code should be lowercase
Or to bypass potential user error, just to lower-case of all of user-inputted keywords
Ah, yes I understand what you mean now, @vbaranov—thanks for clarifying!
I’ve updated the code example and corresponding screenshot to help avoid that happening with the user-inputted list of words. Thanks for flagging this and for helping improve the suggested code! 🧡